...

View Full Version : First 3 In While Loop



Inersha
06-28-2010, 09:38 PM
I'm populating a HTML table after retrieving data from a mysql table. In this html table that will end up being about 10 or so rows long, I would like the first 3 rows to have a bronze, silver and gold background color.

This will be done by giving the first 3 rows that are printed from this loop a specific CSS class, while the remaining rows in the table will be given no class.

Here is the PHP at the moment:



$result = mysql_query("SELECT * FROM table") or die(mysql_error());

while ($row = mysql_fetch_assoc($result)) {

// Echo out the table

echo '<tr>';
echo '<td>Stuff</td>';
echo '<td>Stuff</td>';
echo '</tr>';

}


However, all the table rows will be exactly the same. I would like for this to happen to the rows in succession from the start:

First loop: <tr class="1">
Second loop: <tr class="2">
Third loop: <tr class="3">
Fourth loop: <tr>
Fifth loop: <tr>
etc...

I've been messing with if statements in and out of the while loop to no avail. I know that there must be a fairly straightforward solution but I just can't figure it out. Can anyone help?

Keleth
06-28-2010, 09:44 PM
$result = mysql_query("SELECT * FROM table") or die(mysql_error());

$count = 1;
while ($row = mysql_fetch_assoc($result)) {

// Echo out the table

echo '<tr'.(($count <= 3)?' class="'.$count.'"':'').'>';
echo '<td>Stuff</td>';
echo '<td>Stuff</td>';
echo '</tr>';
$count++

}

Inersha
06-28-2010, 09:58 PM
Thankyou. That's the ternary operator right?

Just so I can get my head around it, could you show me how the code could work by using if-else statments (if that's possible)?

Keleth
06-28-2010, 11:45 PM
echo '<tr'.(($count <= 3)?' class="'.$count.'"':'').'>';

is the same as


echo '<tr';
if ($count <= 3) echo ' class="'.$count.'"'
echo '>';

The else would be to display nothing, so its not necessary.

It formats as


(conditional)?if true:else

Beagle
06-29-2010, 08:44 AM
Call me crazy, but I would actually avoid using conditionals because the comparison has to be run each time. Instead, I would simply add a class to every row, and only define the first 3.

Keleth
06-29-2010, 04:47 PM
Hm... that's actually a pretty good method I didn't think of... assign an incremental class and define 3 of them. Or just an incremental ID. But I guess if you do or don't use a conditional (for the fact its checked each time) depends on the number of rows.

mlseim
06-29-2010, 05:43 PM
I would alter the query itself, to reduce the number of rows ...

You have this:
$result = mysql_query("SELECT * FROM table") or die(mysql_error());
So basically, your result is the entire database table (all rows).

If you can pick a column to narrow it down, you might be able to end-up with the rows you wish to display.

Example (making up my own column/variable names) ...
$result = mysql_query("SELECT * FROM table WHERE win='Y' ORDER by id DESC") or die(mysql_error());




.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum