/    Sign up×
Community /Pin to ProfileBookmark

Summarize from query result

after exec sql command (using php), i need to summarize the result. example like this :

result query

Name Debit

John 1000
John 2000
John 3000
Rudy 2000
Rudy 2000
Daniel 1000
Daniel 1000

Php Command get result like this
Name Debit
Daniel 2000
John 6000
Rudy 4000

How php script to get it ??

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@scragarFeb 11.2008 — [code=php]$rs = mysql_query("SELECT name, num FROM table");
$n = array();
while($row = mysql_fetch_assoc($rs)){
if(!isset($n[$row['name']]))
$n[$row['name']] = $row['num'];
else
$n[$row['name']] += $row['num'];
};
foreach($n as $key=>$val){
echo "<p>$key ... $val</p>n";
};[/code]
Copy linkTweet thisAlerts:
@irfaniauthorFeb 11.2008 — thx, the script is running well.

if more than 1 field that i want to summarize, how ??
Copy linkTweet thisAlerts:
@scragarFeb 11.2008 — [code=php]$rs = mysql_query("SELECT name, num1, num2 FROM table");
$n = array();
while($row = mysql_fetch_assoc($rs)){
if(!isset($n[$row['name']])){
$n[$row['name']] = array($row['num1'], $row['num2']);
}else{
$n[$row['name']][0] += $row['num1'];
$n[$row['name']][1] += $row['num2'];
};
};
foreach($n as $key=>$val){
echo "<p>$key ... {$val[0]} ... {$val[1]}</p>n";
};[/code]
Copy linkTweet thisAlerts:
@MrCoderFeb 11.2008 — Why not just use mysql SUM()?
Copy linkTweet thisAlerts:
@scragarFeb 11.2008 — he wanted a PHP script to do it, I assumed he must want to learn from it or some such.
Copy linkTweet thisAlerts:
@MrCoderFeb 11.2008 — Or he didn't know how to do it using mysql?
Copy linkTweet thisAlerts:
@irfaniauthorFeb 12.2008 — my table too large if using group with sql command and need time more then 10 minutes to get result. with script from mr scragar only 5-10 seconds. tx

one more, if i want count record by range debit and get result like this

Name ,Debit ,debit(500-1500), debit(1500-2500), debit(2500-3500)

Daniel ,2000,2,0,0

John ,6000,1,1,1

Rudy ,4000,0,2,0

how in php script (continue with isset)

tx
Copy linkTweet thisAlerts:
@scragarFeb 12.2008 — [code=php]$rs = mysql_query("SELECT name, num1, num2 FROM table");
$n = array();
while($row = mysql_fetch_assoc($rs)){
if(!isset($n[$row['name']])){
$n[$row['name']] = array($row['num1'], $row['num2'], 0,0,0);
}else{
$n[$row['name']][0] += $row['num1'];
$n[$row['name']][1] += $row['num2'];
};
if($row['num1'] >= 500 && $row['num1'] < 1500)
$n[2]++;
if($row['num1'] >= 1500 && $row['num1'] < 2500)
$n[3]++;
if($row['num1'] >= 2500 && $row['num1'] < 3500)
$n[4]++;
};
echo '<p>name ... debit ... num2 ... 500-1500 ... 1500-2500 ... 2500-3500</p>';
foreach($n as $key=>$val){
echo "<p>$key ... {$val[0]} ... {$val[1]} ... {$val[2]} ... {$val[3]} ... {$val[4]}</p>n";
};[/code]
Copy linkTweet thisAlerts:
@irfaniauthorFeb 13.2008 — try to running the script, but counter $n[2]++; $n[3]++; $n[4]++; still 0
Copy linkTweet thisAlerts:
@MrCoderFeb 13.2008 — my table too large if using group with sql command and need time more then 10 minutes to get result. with script from mr scragar only 5-10 seconds. tx

one more, if i want count record by range debit and get result like this

Name ,Debit ,debit(500-1500), debit(1500-2500), debit(2500-3500)

Daniel ,2000,2,0,0

John ,6000,1,1,1

Rudy ,4000,0,2,0

how in php script (continue with isset)

tx[/QUOTE]


Show us the SQL query you were using.
×

Success!

Help @irfani spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...