Web Hosting Talk







View Full Version : [php] Print HTML row while printing from database


mobilebadboy
02-08-2006, 06:23 PM
I've got 39 rows in a database table that I'm echoing out into an HTML table using a while:

select * from table.....
while ($blah =....) {
// echo each line <td>, etc.
}

What I want to do, though, is after 29 database rows have printed, echo a non-database table row, and then echo out the remaining 10 database rows. An example is how the table looks here (http://www.nascar.com/races/cup/2006/data/schedule.html), that's what I'm trying to achieve.

Is this something simple that I'm not thinking of? It's funny how I can figure out complicated stuff but not what is seemingly simple.

webadpro
02-08-2006, 06:31 PM
hello mobilebadboy,

What I would suggest you is using an "IF" in your function.

Let's say.. if($blah = 29){
echo "What you want" }

Dan L
02-08-2006, 07:17 PM
<?php
$i = 0;
$query = 'SELECT * FROM `table`';
while($rs = mysql_fetch_assoc($query)) {
if($i < 5) {
echo '<td>...</td>';
$i = $i + 1;
} else {
echo '<td>---</td>';
$i = 0;
}
}
?>

error404
02-08-2006, 07:35 PM
define('ROWS_PER_SECTION', 30);
$i = 0;
while ($r = mysql_fetch_assoc($result))
{
echo "<tr><td></td></tr>"; // echo the line
if ($i % ROWS_PER_SECTION == 0)
echo "<tr></tr>"; // echo the spacer
}

mobilebadboy
02-08-2006, 09:13 PM
Thanks guys. Keenan, I couldn't figure yours out.

Dan, I've got yours working....almost. I changed if($i < 5) { to if($i < 29) { and it prints out 29 rows, but then overwrites the 30th row with the divider, then prints out the remaining 9 rows.

So I'm getting:

29 rows
----------
9 rows

When I need:

29 rows
----------
10 rows

You can see the result here (degafan.com/nextel-cup-schedule/test.php). Sorry for the lack of a link, I can't edit my post later and I don't want the URL being crawled somehow. Anyway, it's replacing where "Sylvania 300" should be with the divider, instead of printing the divider and then continuing from Sylvania 300 through the rest.

Dan L
02-08-2006, 09:18 PM
<?php
$i = 0;
$query = 'SELECT * FROM `table`';
while($rs = mysql_fetch_assoc($query)) {
echo '<td>...</td>';
if($i == 5) {
echo '<td>---</td>';
$i = 0;
} else {
$i = $i + 1;
}
}
?>


Keenan's is very similar. What % does is find the remainder after division. So 5 % 2 would be .5. In this case, he takes the current number and divides it by the total number. If it can be divided evenly, then it is the right row to output a divider at. If not, it only outputs the original line.

mobilebadboy
02-08-2006, 11:23 PM
That did the trick. I appreciate it Dan. And for the explanation of Keenan's code.