Jump to content

ANother quick (and probably easy) Question


liam1412

Recommended Posts

I need to have 10 rows in one of my divs in order for the formating to remain in line.  If I query my database and there are only 7 results I want to echo 3 blank rows to pad it out. I have come up with this but it doesn't work. Can anyone tell me why.

[code]<?php
$query = "blah";
$result = mysql_query($query);
$var1 = $result['field1'];
$var2 = $result['field2'];

$numrows = mysql_num_rows($result)
?>

<table>

<?php
while($rows = mysql_fetch_array($result)){
?>

<tr>
<td>$var1</td>
<td>$var2</td>
</tr>

<?php
if{$numrows != 10{
?>

<tr>
<td><br /></td>
<td><br /></td>
</tr>

<?php
$numrows = ($numrows + 1);
}
}
?>[/code]


Link to comment
https://forums.phpfreaks.com/topic/35568-another-quick-and-probably-easy-question/
Share on other sites

Try this

[code]<table border="0" CELLSPACING="0" CELLPADDING="10" align="center">

<?php
//set 3 to 4 of you want 4 columns. Set it to 5 if you want 5, etc
$numcols = 2; // how many columns to display
$numcolsprinted = 0; // no of columns so far
$num_rows = mysql_num_rows($result)

// get each row
while($result = mysql_fetch_array($result)){
{

//get data - eg, reading fields 0 and 1
$var1 = $result['field1'];
$var2 = $result['field2'];

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

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

// bump up row counter
$numcolsprinted++;

} // end while loop

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

}
for($i=0; $i<10-$num_rows; $i++){
if ($numcolsprinted == $numcols) {
// Output blank row
print "</tr>\n<tr>\n";
$numcolsprinted = 0;
}
// output blank column
echo "<td>&nbsp;</td>\n";
// bump up row counter
$numcolsprinted++;
}
?>
</tr>
</table>[/code]

Ray

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.