note 57619 added to function.mysql-fetch-assoc

From: Date: Sun, 09 Oct 2005 02:03:19 +0000
Subject: note 57619 added to function.mysql-fetch-assoc
Groups: php.notes 
Request: Send a blank email to php-notes+get-96705@lists.php.net to get a copy of this message
This is a useful script for displaying MySQL results in an HTML table. <? function array2table($arr,$width) { $count = count($arr); if($count > 0){ reset($arr); $num = count(current($arr)); echo "<table align=\"center\" border=\"1\"cellpadding=\"5\" cellspacing=\"0\" width=\"$width\">\n"; echo "<tr>\n"; foreach(current($arr) as $key => $value){ echo "<th>"; echo $key."&nbsp;"; echo "</th>\n"; } echo "</tr>\n"; while ($curr_row = current($arr)) { echo "<tr>\n"; $col = 1; while ($curr_field = current($curr_row)) { echo "<td>"; echo $curr_field."&nbsp;"; echo "</td>\n"; next($curr_row); $col++; } while($col <= $num){ echo "<td>&nbsp;</td>\n"; $col++; } echo "</tr>\n"; next($arr); } echo "</table>\n"; } } ?> <? // Add DB connection script here $query = "SELECT * FROM mytable"; $result = mysql_query($query); while($row = mysql_fetch_assoc($result)){ $array[] = $row; } array2table($array,600); // Will output a table of 600px width ?> ---- Manual Page -- http://www.php.net/manual/en/function.mysql-fetch-assoc.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+57619 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57619&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57619&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57619&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57619&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57619&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57619&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57619&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+57619&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#96705) next »