You'll probably get as many approaches as replies. How about the following;
after you have connected to mysql and selected the database;
$query = "SELECT * FROM table";
$result = mysql_query ($query)
or die ("Query <b>$query</b> failed. The error message was
".mysql_error ());
// The following wipes out any trace of a memory table in the working $table
element
$table = '';
while ($rows = mysql_fetch_row ($result)) {
$table[] = $rows[0]; // add the row to a working table
}
mysql_free_result($result) // always do this as soon as you can
$top = sizeof($table);
$halfway = ceil($top/2); // round up if odd
$col2 = $halfway;
echo "<TABLE>";
for($col1 = 0; $col1 < $halfway; $col1++) {
echo "<TR><TD>$table[$col1]</TD>";
if($col2 < $top) echo "<TD>$table[$col2++]</TD></TR>";
else echo "<TD> </TD></TR>"; // this avoids that hole in your grid
}
echo "</TABLE>";
-----Original Message-----
From: Zak Greant [mailto:[EMAIL PROTECTED]]
Sent: Saturday, June 23, 2001 1:22 PM
To: McShen
Cc: Php-General
Subject: Re: [PHP] newbie algorithm help!!!!!
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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]