PHP while($row = mysql_fetch_array( $data ))

Discussion in 'PHP' started by quad_design, Oct 21, 2009.

  1. #1
    Hi,

    I'm new to php so I hope this makes sense, thanks in advance for any help.

    I have setup a simple php database which prints out the data into a table. Is there away I can print out the data so each line in the table is alternative colours? example here of what I mean:

    [​IMG]

    My code so far,

    $data = mysql_query("SELECT * FROM repertoire")
    or die(mysql_error()); ?>

    <table width="685" border="0" cellpadding="5" cellspacing="0" class="reportoire">
    <tr class="reportoireheader">
    <td width="316" bgcolor="#F27072"><strong>Song</strong></td>
    <td width="347" bgcolor="#F27072"><strong>Original Artist</strong><strong></strong></td>
    </tr>

    <?

    while($row = mysql_fetch_array( $data ))

    print "<tr class='darkred'><td>".$row['song']."</td>"."<td>".$row['artist']."</td></tr>";

    ?>

    </table>
     
    quad_design, Oct 21, 2009 IP
  2. tguillea

    tguillea Active Member

    Messages:
    229
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    90
    #2
    Yeah here's what I do for Dollar Hauler

    
    <?php
    $result = mysql_query("SELECT * FROM repertoire")
    $x = 0;
    while($row = mysql_fetch_array($result)){
        if ($x == 0){
            $class = 'dark';
            $x = 1;
            }elseif ($x == 1){
            $x = 0;
            $class='light';
            }
        echo '<tr><td [COLOR="Red"]class="'.$class.'"[/COLOR]>'.$row['song'].'</td><td [COLOR="Red"]class="'.$class.'"[/COLOR]>'.$row['artist'].'</td></tr>';
        }
    ?>
    
    PHP:
    Naturally, you'll have to make a PHP class for dark and light, something like
    
    .light{
        background-color: #FFFFFF;
        }
    .dark{
        background-color: #000000;
        }
    
    Code (markup):
    Also, watch your HTML - it isn't proper to put attributes in single quotes as far as I know

    Oh, and the code you have for the table header won't work - TR can't have any attributes - if you want to use a class for a row in a table, you have to use it on each TD individually.
     
    tguillea, Oct 21, 2009 IP
  3. ThomasTwen

    ThomasTwen Peon

    Messages:
    113
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Your code:
    while($row = mysql_fetch_array( $data ))
    
    print "<tr class='darkred'><td>".$row['song']."</td>"."<td>".$row['artist']."</td></tr>";
    
    Code (markup):
    New code:
    
    $x=0;
    while($row = mysql_fetch_array( $data )) {
    
    if ($x % 2 == 0) {
    $color = "darkred";
    } else {
    $color = "lightred";
    }
    print "<tr class='" . $color . "'><td>".$row['song']."</td>"."<td>".$row['artist']."</td></tr>";
    $x++;
    }
    
    Code (markup):
    This script checks if x is even, and returns the color depending on the result, and then increases x by one so that you have a different result next time. Please note that I use a css class called "lightred" that you do probably not have defined in your css code. Create one with the color you want.
     
    ThomasTwen, Oct 21, 2009 IP
  4. quad_design

    quad_design Peon

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi, thank you for the response, it worked great.
     
    quad_design, Oct 22, 2009 IP
  5. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You can shorten that code down somewhat tguillea

    $result = mysql_query("SELECT * FROM repertoire")
    $x = 0;
    while($row = mysql_fetch_array($result)){
        $class = ($x++ % 2 == 0) ? 'light' : 'dark';
        echo '<tr><td class="'.$class.'">'.$row['song'].'</td><td class="'.$class.'">'.$row['artist'].'</td></tr>';
    }
    PHP:
     
    JAY6390, Oct 22, 2009 IP
    heavydev likes this.
  6. LOD

    LOD Member

    Messages:
    319
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    35
    #6
    that also helped me.. thx
     
    LOD, Oct 23, 2009 IP