By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,278 Members | 1,358 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,278 IT Pros & Developers. It's quick & easy.

Stumped by Array problem!!

P: 18
Hi, hope I can explain this clearly. I have the following query and results, no problems here:

[PHP]
$query = 'SELECT SUM(ed) AS ed, SUM(ted) AS ted, SUM(tom) AS tom FROM week ';
$result = mysql_query($query) or die(mysql_error());

$values['ed']['sum'] = mysql_result($result, 0, "ed");
$values['ted']['sum'] = mysql_result($result, 0, "ted");
$values['tom']['sum'] = mysql_result($result, 0, "tom");

$values['ed']['name'] = mysql_field_name($result, 0);
$values['ted']['name'] = mysql_field_name($result, 1);
$values['tom']['name'] = mysql_field_name($result, 2);[/PHP]

I then have this code which puts the results of the "sum" data into an array and sorts it. This works fine as well.

[PHP]
$edsum = $values['ed']['sum'];
$tedsum = $values['ted']['sum'];
$tomsum = $values['tom']['sum'];

$edname = $values['ed']['name'];
$tedname = $values['ted']['name'];
$tomname = $values['tom']['name'];

$temp = array("$edsum", "$tedsum", "$tomsum");
array_multisort($temp, SORT_NUMERIC, SORT_DESC);[/PHP]

What I CAN'T figure out is how to get the "name" data into the array as well. I need to output the "name" and "sum" data together sorted. Any ideas would be greatly appreciated!!!!!

Thanks, Tom
Feb 19 '07 #1
Share this Question
Share on Google+
3 Replies


cassbiz
100+
P: 202
Hi, hope I can explain this clearly. I have the following query and results, no problems here:

[PHP]
$query = 'SELECT SUM(ed) AS ed, SUM(ted) AS ted, SUM(tom) AS tom FROM week ';
$result = mysql_query($query) or die(mysql_error());

$values['ed']['sum'] = mysql_result($result, 0, "ed");
$values['ted']['sum'] = mysql_result($result, 0, "ted");
$values['tom']['sum'] = mysql_result($result, 0, "tom");

$values['ed']['name'] = mysql_field_name($result, 0);
$values['ted']['name'] = mysql_field_name($result, 1);
$values['tom']['name'] = mysql_field_name($result, 2);[/PHP]

I then have this code which puts the results of the "sum" data into an array and sorts it. This works fine as well.

[PHP]
$edsum = $values['ed']['sum'];
$tedsum = $values['ted']['sum'];
$tomsum = $values['tom']['sum'];

$edname = $values['ed']['name'];
$tedname = $values['ted']['name'];
$tomname = $values['tom']['name'];

$temp = array("$edsum", "$tedsum", "$tomsum");
array_multisort($temp, SORT_NUMERIC, SORT_DESC);[/PHP]

What I CAN'T figure out is how to get the "name" data into the array as well. I need to output the "name" and "sum" data together sorted. Any ideas would be greatly appreciated!!!!!

Thanks, Tom

Hope I can help here

in the line
Expand|Select|Wrap|Line Numbers
  1. $temp = array("$edsum", "$tedsum","$tomsum");
  2.  
shouldn't it read

Expand|Select|Wrap|Line Numbers
  1. $temp = array("$edname","$edsum","$tedname", "$tedsum","$tomname","$tomsum");
  2.  
I am still somewhat a noob, but I noticed that the *name wasn't in your output.

Good luck
Feb 19 '07 #2

P: 18
Hope I can help here

in the line
Expand|Select|Wrap|Line Numbers
  1. $temp = array("$edsum", "$tedsum","$tomsum");
  2.  
shouldn't it read

Expand|Select|Wrap|Line Numbers
  1. $temp = array("$edname","$edsum","$tedname", "$tedsum","$tomname","$tomsum");
  2.  
I am still somewhat a noob, but I noticed that the *name wasn't in your output.

Good luck
Hi! Well, I've tried that, and some other variations with no luck. What that code gives me is the numbers first, sorted, then the names. I'm thinking I need to put each name and its' corresponding sum into one variable if I could but haven't had any luck yet. But thanks for your time!
Feb 19 '07 #3

Atli
Expert 5K+
P: 5,058
You could set up your array like this.

[PHP]
$myArra = array(
"Ed" => array("Name" => $name1, "Sum" => $sum1),
"Bob" => array("Name" => $name2, "Sum" => $sum2),
"Bill" => array("Name" => $name3, "Sum" => $sum3)
// Yes i forgot the names :P
);

// Print Ed's info
echo "Name: ". $myArr['Ed']['Name'] ." - Sum: ". $myArr['Ed']['Sum'];
[/php]

Hope this helps.

Edit:
When I look back I think your initial $values array is set up like this.
Feb 19 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.