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.
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
EDIT: Nevermind.. misread something. Though, that last example you posted that you tried, should have worked perfectly. What about it didn't you like?
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