Adding alternating styles to rows?

Discussion in 'PHP' started by Mr.Bill, Feb 16, 2008.

  1. #1
    I have this piece of code and am trying to make the output use alternating styles for each of the rows.
    <?php
    require("config/config.php");
    mysql_connect($db_addr, $db_user, $db_pass) or die(mysql_error());
    mysql_select_db($db_name) or die(mysql_error());
    $query = mysql_query("SELECT * FROM `advertisers` WHERE `status` = 'online'") or die(mysql_error());
    
    
    while ($row = mysql_fetch_array( $query ))
    {
    
    $descr = str_replace("\n", "<br />", $row['description']);
    $descr = str_replace("[b]", "<b>", $descr);
    $descr = str_replace("[/b]", "</b>", $descr);
    $descr = str_replace("[/link]", "</a>", $descr);
    $descr = str_replace('[link url="http://', '<a href="http://', $descr);
    $descr = str_replace('[link url="', '<a href="http://', $descr);
    $descr = str_replace('"]', '">', $descr);
    echo '<div class="news">'."\n";
    echo '<div class="ntitle"><a href="http://'.$row['url'].'">'.$row['name'].'</a></div>'."\n";
    echo $descr."\n";
    echo '</div>'."\n";
    $counter++;
    }
    ?>
    PHP:
    I tried to add this and it dint yield the results I was looking to achieve

    					$counter = 1;
    					while ($row = mysql_fetch_array( $query )) 
    					{
    						echo '<tr class="'; 
    						if ($counter%2 == 1) { echo "c2"; } else { echo "c1"; } 
    PHP:
    Is anyone able to piece these together for me so it would work.
     
    Mr.Bill, Feb 16, 2008 IP
  2. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #2
    This might be easier.
    
    $bgclass = 'foo';
    while (.....) {
    if($bgclass=='foo'){$bgclass='bar;}
    else{$bgclass='foo';}
    echo '<tr class="' . $bgclass ;
    
    ............
    }
    
    Code (markup):
    Assign foo & bar, better class names.
    Taken from here(+modified):
    http://www.plus2net.com/php_tutorial/php_paging2.php
     
    shallowink, Feb 16, 2008 IP
  3. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #3
    EDIT: Nevermind.. misread something.

    Though, that last example you posted that you tried, should have worked perfectly. What about it didn't you like?
     
    zerxer, Feb 16, 2008 IP
  4. Mr.Bill

    Mr.Bill Well-Known Member

    Messages:
    2,818
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    160
    #4
    Alright thank you I got it :)

    <?php
    require("config/config.php");
    mysql_connect($db_addr, $db_user, $db_pass) or die(mysql_error());
    mysql_select_db($db_name) or die(mysql_error());
    $query = mysql_query("SELECT * FROM `advertisers` WHERE `status` = 'online'") or die(mysql_error());
    $counter = 1;
    while ($row = mysql_fetch_array( $query ))
    {
    echo '<div class="';
    if ($counter%2 == 1) { echo "c1"; } else { echo "c2"; }
    echo '">'."\n";
    
    $descr = str_replace("\n", "<br />", $row['description']);
    $descr = str_replace("[b]", "<b>", $descr);
    $descr = str_replace("[/b]", "</b>", $descr);
    $descr = str_replace("[/link]", "</a>", $descr);
    $descr = str_replace('[link url="http://', '<a href="http://', $descr);
    $descr = str_replace('[link url="', '<a href="http://', $descr);
    $descr = str_replace('"]', '">', $descr);
    echo '<div class="ntitle"><a href="http://'.$row['url'].'">'.$row['name'].'</a></div>'."\n";
    echo $descr."\n";
    echo '</div>'."\n";
    $counter++;
    }
    ?>
    PHP:
    Was a problem with this

    echo '<td class="';
    PHP:
    changed to

    echo '<div class="';
    PHP:
    then it wouldnt alternate the colors so viewing the source code I saw the div tags were not being closed :( so I had to add this

    echo '">'."\n";
    PHP:
    All is well in my living hell :p
     
    Mr.Bill, Feb 16, 2008 IP