Jump to content

Solved: PHP array and table troubles


jch02140

Recommended Posts

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:x0z8dk.jpgFor 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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...