note 60271 deleted from ref.array by felipe

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
Note Submitter: ryan1_00 at hotmail dot com 

----

The following will take a query result and create a dynamic table for it. Using the Key() function to get the table headers and the current() function for the actual values.

$result = mysql_query($qry) or die ("<center> ERROR: ".mysql_error()."</center>");
  		
  		$x = 0;

  		echo "<table border=\"1\" align=\"center\">";  	
  		while($row = mysql_fetch_assoc($result))
		{
			if ($x == 0) // so that table headers are only created on the first pass
			{
				$col = (count($row)); // counts number of elements in the array
  				$x=1;
				echo "<tr>";
				for ($y=0; $y<$col; $y++)
				{
					echo "<th>";
					echo key($row); // gets the names of the fields
					echo "</th>";
					next($row);
				}
				echo "</tr>";
			}
			reset($row);
			echo "<tr>";
			for ($y=0; $y<$col; $y++)
			{
				echo "<td valign=\"top\" align=\"left\">";
				echo current($row);
				echo"</td>";
				next($row);
			}
			echo "</tr>";
		}  // end of while loop
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.