Re: newbie algorithm help!!!!!

From: Date: Sat, 23 Jun 2001 20:22:01 +0000
Subject: Re: newbie algorithm help!!!!!
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-54989@lists.php.net to get a copy of this message
McShen wrote: > > hi > > i have a mysql table with approx. 30 entries. > > > > I wanna get them(i know how to do that) and list them in a 2-column table. > I > > have been trying to use a loop to do it. But it will only produce a > 1-column > > table. it's like > > > > entry 1 > > entey 2 > > entry 3 > > entry 4 > > etc > > > > Please help me so that i can get this : > > > > entry1 entry2 > > entry3 entry4 The code is easier to understand than the explanation! :) $query = "SELECT * FROM table"; $result = mysql_query ($query) or die ("Query <b>$query</b> failed. The error message was " . mysql_error ()); // Initialize a value to stored the table rows $table = ''; // Initialize a counter for the column number $column = 0; // Loop through the query results while ($rows = mysql_fetch_row ($result)) { // Put the contents of the field into a cell $cell = "<td>$rows[0]</td>"; // If we are in column 0 if (0 == $column) { // Start the row $table .= "<tr>$cell"; } else { // End the row $table .= "$cell</tr>\n"; } // Make the column number equal to // The whole number remainder left over // from dividing the value of $column // plus one by two $column = ($column + 1) % 2; } // Handle dangling rows if (1 == $column) { $table .= "<td></td></tr>\n"; } echo "<table>$table</table>"; Good Luck! --zak

« previous php.general (#54989) next »