sandrob57 Posted July 26, 2007 Share Posted July 26, 2007 So I have an array, technically it looks like this: $data = Array ( [0] => Array ( [gold] => 150 [food] => 23 [wood] => 27 [stone] => 11 ) [1] => Array ( [gold] => 170 [food] => 33 [wood] => 17 [stone] => 31 ) ) simpler terms: data[0] = [gold] 50 [wood] 40 [food] 30 [stone] 25 data[1] = [gold] 30 [wood] 50 [food] 20 [stone] 65 So what I am trying to do is make graphs for each unit type, so: GOLD Day 1 - 50 Day 2 - 30 Wood Day 1 - 40 Day 2 - 50 etc. The only problem is, I don't know how to set these up into a foreach() by each resource category. So how would i go about doing that? I tried this and just told it to get GOLD but it isnt working: $income = dbquery("SELECT * FROM fusion_income WHERE user_id='".$userdata['user_id']."' ORDER BY id DESC LIMIT 0,10"); while($income_data = dbarray($income)){ $data[] = unserialize($income_data['amount']); } foreach($data[]['gold'] as $cat => $amount){ echo "$cat - $amount<br />"; } The error i get is: Warning: Invalid argument supplied for foreach() in /home/galava/public_html/page/graphs.php on line 43 sigh... Quote Link to comment Share on other sites More sharing options...
tapos Posted July 26, 2007 Share Posted July 26, 2007 send your full code. -- Tapos Pal Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 26, 2007 Share Posted July 26, 2007 I'd use a for loop: <?php for($x=0;$x<count($data);$x++){ echo 'Gold:'.$data[$x]['gold'].'<br />'; } The reason why you get your error is because you are trying to use the foreach on something which is not an array. Quote Link to comment Share on other sites More sharing options...
sandrob57 Posted July 26, 2007 Author Share Posted July 26, 2007 Well, I finally found a way that works. Take a look if ur interested (thanks for the help, although I did find another way): $income = dbquery("SELECT * FROM fusion_income WHERE user_id='".$userdata['user_id']."' ORDER BY id DESC LIMIT 0,10"); while($income_data = dbarray($income)){ $data[] = unserialize($income_data['amount']); } foreach($data as $cat => $amount){ foreach($data[$cat] as $cat1 => $amount1){ $supply[$cat1][] = $amount1; } } //Income Graphs echo "<table align='center' cellpadding='0' cellspacing='1' width='1%' class='tbl-border'>"; foreach($supply as $cat => $array){ $supply[$cat] = array_reverse ($supply[$cat]); array_reverse ($supply); $max[$cat] = 120 / max($supply[$cat]) * 1.1; echo "<tr><td class='tbl2' width='1%' style='white-space: nowrap;' align='left' rowspan='2'> "; icon("".$cat.".gif", ""); echo " </td>"; foreach($supply[$cat] as $day_number => $value){ echo" <td class='tbl1' style='white-space: nowrap;' align='left' valign='bottom'> <img src='".THEME."images/pollbar.gif' height='".(($value * $max[$cat]) + 10)."' width='12' align='bottom' style='border:1px #444 solid;'> </td> "; } echo "</tr><tr>"; foreach($supply[$cat] as $day_number => $value){ echo" <td class='tbl1' style='white-space: nowrap;' align='left' valign='bottom'> <span class='small2'>".$value."</span> </td> "; } echo "</tr>"; } echo "</table>"; Quote Link to comment Share on other sites More sharing options...
Barand Posted July 26, 2007 Share Posted July 26, 2007 see http://www.phpfreaks.com/forums/index.php/topic,150863.msg650758.html#msg650758 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.