Hi
I'm trying to create a page to display the results of an mysql query spread across two columns (or vertical halves of the page to be more visually accurate as technically with all the cells there are lots of columns).
In visual terms I effectively want a vertical line down the middle of the page, with the first record output to appear to the left of the line, the second to the right, the third to the left and so on.
The end result is that if there are say 100 records returned then 50 will be in column 1 and 50 in column 2
The code below is what I have used to display ok in one column. I did try repeating the code in the second column (ie area to the right of my vertical line) using a different search criteria, but that didn't work.
Anyone know what direction I should be looking?
Many thanks
Steve
<!--Start of first column database retrieval-->
<?php
//Connect to Database
require_once ('./mysql_connect.php');
//Make the query
$query = "SELECT id, url, logo, info, display FROM happenings";
//Only displays if field display=Y
$query .= "WHERE display='Y'";
//Sets random order of presentation of entries
$query .= "ORDER BY RAND()";
//Obtain result of query
$result = @mysql_query ($query);
//Display query result
if ($result) {
echo '<TABLE WIDTH=440 BORDER=0 CELLSPACING = "0" CELLPADDING = "2" >';
//spacer row
echo '<tr><td><br><br></td></tr>';
//Display rows showing clients
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "<tr >
<td width=450 class=client> <A HREF=\"http://$row[url]\" target=_blank><IMG SRC=\"./logos/$row[logo]\"></a></td>
</tr>
<tr>
<td class=client>$row[info]</td>
</tr>
<tr><td><br></td></tr>
\n";
}
echo "</table>";
//Free up table query
mysql_free_result ($result);
//Complete the conditional
} else {//if it did not run OK
echo '<p>The client logos could not be displayed due to a system error.</p>
<p>'. mysql_error(). '</p>';
}
//Close database connection
mysql_close();
//End of first column of results
?>
NOW RESOLVED
Since posting this I had a look at the comments on the PHP manual and found one that answers my question and I have used his suggestions successfully.
Code from PHP manual comments below:
john at skem9 dot com
21-Jan-2006 05:13
my main purpose was to show the fetched array into a table, showing the results side by side instead of underneath each other, and heres what I've come up with.
just change the $display number to however many columns you would like to have, just dont change the $cols number or you might run into some problems.
<?php
$display = 4;
$cols = 0;
echo "<table>";
while($fetched = mysql_fetch_array($result)){
if($cols == 0){
echo "<tr>\n";
}
// put what you would like to display within each cell here
echo "<td>".$fetched['id']."<br />".$fetched['name']."</td>\n";
$cols++;
if($cols == $display){
echo "</tr>\n";
$cols = 0;
}
}
// added the following so it would display the correct html
if($cols != $display && $cols != 0){
$neededtds = $display - $cols;
for($i=0;$i<$neededtds;$i++){
echo "<td></td>\n";
}
echo "</tr></table>";
} else {
echo "</table>";
}
?>
Hopefully this will save some of you a lot of searching.
any kind of improvements on this would be awesome!