Hey, this is my first attempt at using arrays in PHP, so apologies if this turns out to be ridiculously simple!

I have a SQL select statement, and a while statement to pull the results from it:

$sql = 'SELECT nickname AS name, count( * ) AS played, sum( score ) AS agg, max( score ) AS highest, min( score ) AS lowest, avg( score ) AS average'
		 . ' FROM stats_players, stats_scoreslist, stats_matches'
		 . ' WHERE stats_players.playerid = stats_scoreslist.playerid AND stats_scoreslist.matchid = stats_matches.matchid AND stats_matches.season = '.$Season
		 . ' GROUP BY nickname'
		 . ' ORDER BY average DESC , name ASC'; 


$result=mysql_query($sql) or die(mysql_error());
	
	$rows="";
	$position=1;
	
	while ($row=mysql_fetch_array($result))
	{	
		$playername=$row["name"];
		$played=$row["played"];
		$agg=$row["agg"];
		$high=$row["highest"];
		$low=$row["lowest"];
		$avg=$row["average"];
		$rows.="<tr><td><center>".$position.'</td><td><center>'.$playername.'</td><td><center>'.$played.'</td><td><center>'.$agg.'</td><td><center>'.$high.'</td><td><center>'.$low.'</td><td><center>'.$avg.'</td></tr>';

		$position++;
	}

As you can see, this builds up an HTML table in the variable $rows, and this is then echo'd out later on where I need to use it. My requirement is now to add another column onto the resulting table, and that column to be the result of calculations on the other variables in the table, on other rows - $avg and $played specifically.

This is where I arrive at using arrays. I'm thinking of writing the table values into an array, and then I can manipulate it and calculate it all I like using PHP. But I don't know how to get this into an array, using a loop, or even if using an array is the way to do it!

I can manipulate simple one-dimensional arrays, but can anyone help on getting all that into a two-dimensional array - or whatever array is needed!?

Thanks!

You can calculate them in the while loop itself , and store that result value in variable give that to table by creating one more column. If you want to check the rows use the check for $position variable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.