Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-28-2010, 08:38 PM   PM User | #1
Inersha
New Coder

 
Join Date: Jun 2008
Posts: 50
Thanks: 18
Thanked 0 Times in 0 Posts
Inersha is on a distinguished road
First 3 In While Loop

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:

PHP Code:
$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?
Inersha is offline   Reply With Quote
Old 06-28-2010, 08:44 PM   PM User | #2
Keleth
Senior Coder

 
Join Date: Jun 2008
Location: New Jersey
Posts: 2,354
Thanks: 45
Thanked 247 Times in 244 Posts
Keleth is on a distinguished road
PHP Code:
$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++


Keleth is offline   Reply With Quote
Users who have thanked Keleth for this post:
Inersha (06-28-2010)
Old 06-28-2010, 08:58 PM   PM User | #3
Inersha
New Coder

 
Join Date: Jun 2008
Posts: 50
Thanks: 18
Thanked 0 Times in 0 Posts
Inersha is on a distinguished road
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)?
Inersha is offline   Reply With Quote
Old 06-28-2010, 10:45 PM   PM User | #4
Keleth
Senior Coder

 
Join Date: Jun 2008
Location: New Jersey
Posts: 2,354
Thanks: 45
Thanked 247 Times in 244 Posts
Keleth is on a distinguished road
PHP Code:
echo '<tr'.(($count <= 3)?' class="'.$count.'"':'').'>'
is the same as

PHP Code:
echo '<tr';
if (
$count <= 3) echo ' class="'.$count.'"'
echo '>'
The else would be to display nothing, so its not necessary.

It formats as

PHP Code:
(conditional)?if true:else 
Keleth is offline   Reply With Quote
Users who have thanked Keleth for this post:
Inersha (06-29-2010)
Old 06-29-2010, 07:44 AM   PM User | #5
Beagle
Senior Coder

 
Join Date: Jul 2005
Location: New York, NY
Posts: 1,084
Thanks: 4
Thanked 19 Times in 19 Posts
Beagle is an unknown quantity at this point
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.
Beagle is offline   Reply With Quote
Old 06-29-2010, 03:47 PM   PM User | #6
Keleth
Senior Coder

 
Join Date: Jun 2008
Location: New Jersey
Posts: 2,354
Thanks: 45
Thanked 247 Times in 244 Posts
Keleth is on a distinguished road
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.
Keleth is offline   Reply With Quote
Old 06-29-2010, 04:43 PM   PM User | #7
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,046
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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());




.
mlseim is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:48 AM.


Advertisement
Log in to turn off these ads.