Jump to content

[SOLVED] re-arrange array data


sandrob57

Recommended Posts

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...  :-[

Link to comment
Share on other sites

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>";	

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.