Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jun 2008
    Posts
    50
    Thanks
    18
    Thanked 0 Times in 0 Posts

    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?

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts
    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++



  • Users who have thanked Keleth for this post:

    Inersha (06-28-2010)

  • #3
    New Coder
    Join Date
    Jun 2008
    Posts
    50
    Thanks
    18
    Thanked 0 Times in 0 Posts
    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)?

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts
    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 

  • Users who have thanked Keleth for this post:

    Inersha (06-29-2010)

  • #5
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    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.

  • #6
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts
    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.

  • #7
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,464
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    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());




    .


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •