jch02140 Posted June 2, 2011 Share Posted June 2, 2011 Hi,I have 2 problems which I am not sure how to fix since I am just beginning in coding PHP...The first problem is that when I try to print an array with a table I got the follow:For some reason the right side of the table is wider...Here is the code: <table border="1"> <tbody> <tr> <td> <p align="center">Name</p> </td> <td> <p align="center">Age</p> </td> <td> <p align="center">######</p> </td> </tr><?php echo "<p><u>Multi-dimensional array example:</u></p>"; $uid = array( 0 => array("Peter",23,"M"), 1 => array("Jason",26,"M"), 2 => array("Linsay",21,"F"), 3 => array("Megan",18,"F") ); for ($i=0;$i<count($uid);$i++) { for ($j=0;$j<count($uid, $j);$j++) { echo $uid[$i][$j]; echo "<br />"; } } echo "<p><u>With table decoration</u></p>"; for ($i=0;$i<count($uid);$i++) { echo "<tr>"; for ($j=0;$j<count($uid, $j);$j++) { echo "<td>"; echo $uid[$i][$j]; echo "</td>"; } echo "</tr>"; } echo "</tbody></table>"; echo "<br />";?> Second problem is that I am trying to get an average of two numbers from each person from an array but all I got are zeros...Here is the code for the second problem... <?php $uid = array( array("name" => "Peter", "chi" => 23, "eng" => 28), array("name" => "Jason", "chi" => 26, "eng" => 30), array("name" => "Linsay", "chi" => 21, "eng" => 35), array("name" => "Megan", "chi" => 18, "eng" => 24) );?> <table border="1"> <tr><td>Name</td><td>Average</td></tr><?php for ($i=0; $i<sizeof($uid); $i++) { $average = ($student[$i]["chi"] + $student[$i]["eng"]) / 2; echo "<tr><td>" . $uid[$i]["name"] . "</td>\n"; echo "<td>" .$average. "</td></tr>\n"; }?> How do I fix them?...Thanks. Link to comment Share on other sites More sharing options...
dsonesuk Posted June 2, 2011 Share Posted June 2, 2011 count($uid, $j) does not give the true total of values within the array, it gives 4, if you echo out count($uid, 1), you would get 16, so you are gaining a extra empty columns.to get the true total you would use $total_multi_dim_value = (count($uid,COUNT_RECURSIVE)-count($uid,0))/count($uid); $total_multi_dim_value = (count($uid,COUNT_RECURSIVE)-count($uid,0))/count($uid); for ($i=0;$i<count($uid);$i++) { for ($j=0;$j<$total_multi_dim_value;$j++) { echo $uid[$i][$j]; echo "<br />"; } } echo "<p><u>With table decoration</u></p>"; for ($i=0;$i<count($uid);$i++) { echo "<tr>"; for ($j=0;$j<$total_multi_dim_value;$j++) { echo "<td>"; echo $uid[$i][$j]; echo "</td>"; } echo "</tr>"; } Link to comment Share on other sites More sharing options...
dsonesuk Posted June 2, 2011 Share Posted June 2, 2011 the second problem is that $student is undefined it should be $uid for ($i=0; $i<sizeof($uid); $i++) { $average = ($uid[$i]["chi"] + $uid[$i]["eng"]) / 2; echo "<tr><td>" . $uid[$i]["name"] . "</td>\n"; echo "<td>" .$average. "</td></tr>\n"; } IF you have php settings for displaying errors set to 'ON' it would show you this. Link to comment Share on other sites More sharing options...
jch02140 Posted June 2, 2011 Author Share Posted June 2, 2011 Thanks for the help :)I can see how the number of values within the array are calculated now.I admit the second part is my carelessness on the variables names... >_< Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.