couplebb Posted September 20, 2007 Share Posted September 20, 2007 I'm trying to display the entries in a table of two columns, one entry per table cell - in other words, two entries per row, number of rows based on number of entries in the database. But it still doesn't seem to work. It displays one entry per row... I don't know where the problem is. Here's my code: echo "<table width='490' border='0' cellspacing='10' cellpadding='10'>"; $num = mysql_num_rows($result); $numrows = round($num/2); while ($row=mysql_fetch_array($result)) { extract($row); $count=1; while ($count<$numrows){ echo "<tr>"; $count2=0; while($count2<1){ echo "<td width='245' align='center' valign='top'><span class='content2 style2'>"; echo "<img src='$picaddy' alt='$firstname $lastname'><br>$lastname<br>$firstname</span></td>"; $count2=$count2+1; } $count=$count+1; echo "</tr>"; } } echo "</table>"; Quote Link to comment https://forums.phpfreaks.com/topic/69991-solved-output-database-entries-in-a-table/ Share on other sites More sharing options...
tr33b1rd Posted September 20, 2007 Share Posted September 20, 2007 Try this. You don't need to know the number of rows, "while ($row=mysql_fetch_array($result))" will loop through every row selected. Unless there is another part that I'm not seeing. You only have 1 column in your table, if you want to have a multi column table you will have to throw in some <td></td> tags. echo "<table width='490' border='0' cellspacing='10' cellpadding='10'>"; $query = mysql_query(YOUR QUERY); while ($array = mysql_fetch_array($query)){ $firstname = $array["firstname"]; $lastname = $array["lastname"]; echo("<tr><td width='245' align='center' valign='top' class='content2 style2'><img src='".$picaddy."' alt='".$firstname." ".$lastname."'>".$lastname." ".$firstname."</td></tr>"); } echo "</table>"; Quote Link to comment https://forums.phpfreaks.com/topic/69991-solved-output-database-entries-in-a-table/#findComment-351580 Share on other sites More sharing options...
sasa Posted September 20, 2007 Share Posted September 20, 2007 try echo "<table width='490' border='0' cellspacing='10' cellpadding='10'>"; //$num = mysql_num_rows($result); //$numrows = round($num/2); while ($row=mysql_fetch_array($result)) { extract($row); //$count=1; //while ($count<$numrows){ echo "<tr>"; //$count2=0; //while($count2<1){ echo "<td width='245' align='center' valign='top'><span class='content2 style2'>"; echo "<img src='$picaddy' alt='$firstname $lastname'> $lastname $firstname</span></td>"; //$count2=$count2+1; if ($row = mysql_fetch_array($result)){ extract($row); echo "<td width='245' align='center' valign='top'><span class='content2 style2'>"; echo "<img src='$picaddy' alt='$firstname $lastname'> $lastname $firstname</span></td>"; } else echo '<td> </td>'; //} //$count=$count+1; echo "</tr>"; //} } echo "</table>"; Quote Link to comment https://forums.phpfreaks.com/topic/69991-solved-output-database-entries-in-a-table/#findComment-351776 Share on other sites More sharing options...
dbo Posted September 20, 2007 Share Posted September 20, 2007 Here's a more general example. P.S. I like using for loops when I can. I guess I'm too structured, lol. function printTable($mysql_result) { echo "<table>\n"; $fields = mysql_num_fields($mysql_result); $rows = mysql_num_rows($mysql_result); for( $i = 0; $i < $rows; ++$i ) { echo "<tr>\n"; $row = mysql_fetch_row($mysql_result); for( $j = 0; $j < $fields; ++$j ) { echo "<td>" . $row[$j] . "</td>\n"; } echo "</tr>\n"; } echo "</table>\n"; } So then usage would be something like: $result = mysql_query("SELECT * FROM table"); printTable($result); Quote Link to comment https://forums.phpfreaks.com/topic/69991-solved-output-database-entries-in-a-table/#findComment-351784 Share on other sites More sharing options...
couplebb Posted September 21, 2007 Author Share Posted September 21, 2007 Thank you everyone! It works great! Quote Link to comment https://forums.phpfreaks.com/topic/69991-solved-output-database-entries-in-a-table/#findComment-352144 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.