Jump to content

Echoing 2 sepearate rows from the same database query


liam1412

Recommended Posts

Can use this. Just change the query and set the number of rows you want.
Not sure if you want to go down then across, or across then down.
This will go down then across.
[code]
<table width=300 border=0 align=center>
<?php

//set 3 to 4 of you want 4 columns. Set it to 5 if you want 5, etc
$numcols = 5; // how many columns to display
$numcolsprinted = 0; // no of columns so far

// get the results to be displayed
$query = "SELECT * FROM my_table ORDER BY my_field ASC LIMIT 10";
$mysql_result = mysql_query($query);

// get each row
while($myrow = mysql_fetch_assoc($mysql_result))
{

// set rows you want to view here
$id = $myrow['id'];

if ($numcolsprinted == $numcols) {
print "</tr>\n<tr>\n";
$numcolsprinted = 0;
}

// output row from database
echo "<td>$id</td>\n";

// bump up row counter
$numcolsprinted++;

} // end while loop

$colstobalance = $numcols - $numcolsprinted;
for ($i=1; $i<=$colstobalance; $i++) {

}
print "<TD></TD>\n";
?>
</table>[/code]

Ray
set a counter in the while loop as well, then run an "if" statement to see if the count is equal to 5, if so then dont echo out the next line just do a carriage return. is that what you mean?
[code]
<?php
$query = "Blah";
$result = mysql_query($query) or die ("unable to run query". mysql_error());
$count = 1;
while ($row = mysql_fetch_assoc($result)
{
    if ($count == 5)
{
    echo "<BR>";
}
else
{
    echo $row['whatever'];
}
$count++;
}
?>
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.