Mr Chris Posted January 10, 2008 Share Posted January 10, 2008 Hi Guys, I have a query whereby I select all entries in a database using a while statement: <?php $SQL = "select * from blah"; $result = mysql_query($SQL) OR die(mysql_error()); $bg = ($bg=='white_div' ? 'grey_div' : 'white_div'); while ($row = mysql_fetch_assoc($result)) { echo '<div class="$bg">' . "\n"; echo '<span class="comments_text">'. $row['opening_paragraph'] .'</span>' . "\n"; echo '</div>' . "\n"; } ?> However, what I want to do is make each row outputted an alternative colour in the background, hence declaring the variable $bg in the code which holds names of styles in my CSS design. Could anyone help, as I don't quite understand how this is done? Thanks Chris Link to comment https://forums.phpfreaks.com/topic/85370-solved-using-php-css-for-alternate-colours/ Share on other sites More sharing options...
nikefido Posted January 10, 2008 Share Posted January 10, 2008 <?php $bg = 'white_div'; $SQL = "select * from blah"; $result = mysql_query($SQL) OR die(mysql_error()); //$bg = ($bg=='white_div' ? 'grey_div' : 'white_div'); --- i know i'm making an equivalent code here: if($bg ==='white_div') { $bg = 'grey_div'; } else { $bg = 'white_div'; } while ($row = mysql_fetch_assoc($result)) { echo '<div class="$bg">' . "\n"; echo '<span class="comments_text">'. $row['opening_paragraph'] .'</span>' . "\n"; echo '</div>' . "\n"; } ?> the trick here is to define $bg prior to your conditional statement - note that your first color is going to be grey rather than white - you can switch the original $bg definition to change that to be white first if you want. Link to comment https://forums.phpfreaks.com/topic/85370-solved-using-php-css-for-alternate-colours/#findComment-435553 Share on other sites More sharing options...
Mr Chris Posted January 10, 2008 Author Share Posted January 10, 2008 Thanks, But when I view the source of the page it shows: <div class="$bg"> For each element? Thanks Chris Link to comment https://forums.phpfreaks.com/topic/85370-solved-using-php-css-for-alternate-colours/#findComment-435562 Share on other sites More sharing options...
foreverhex Posted January 10, 2008 Share Posted January 10, 2008 all you have to do is change this: echo '<div class="$bg">' . "\n"; with this: echo '<div class="' . $bg . '">' . "\n"; Link to comment https://forums.phpfreaks.com/topic/85370-solved-using-php-css-for-alternate-colours/#findComment-435564 Share on other sites More sharing options...
nikefido Posted January 10, 2008 Share Posted January 10, 2008 you didn't escape your quotes: <?php $bg = 'white_div'; $SQL = "select * from blah"; $result = mysql_query($SQL) OR die(mysql_error()); //$bg = ($bg=='white_div' ? 'grey_div' : 'white_div'); --- i know i'm making an equivalent code here: if($bg ==='white_div') { $bg = 'grey_div'; } else { $bg = 'white_div'; } while ($row = mysql_fetch_assoc($result)) { echo '<div class=\"{$bg}\">' . "\n"; echo '<span class=\"comments_text\">'. $row['opening_paragraph'] .'</span>' . "\n"; echo '</div>' . "\n"; } ?> Link to comment https://forums.phpfreaks.com/topic/85370-solved-using-php-css-for-alternate-colours/#findComment-435565 Share on other sites More sharing options...
Mr Chris Posted January 10, 2008 Author Share Posted January 10, 2008 Thanks Guys, Sorry to be a pain, but it just makes all of the elements outputted grey_div not doing alternatives? Thanks Chris Link to comment https://forums.phpfreaks.com/topic/85370-solved-using-php-css-for-alternate-colours/#findComment-435578 Share on other sites More sharing options...
DyslexicDog Posted January 10, 2008 Share Posted January 10, 2008 <?php $bg = 'white_div'; $SQL = "select * from blah"; while ($row = mysql_fetch_assoc($result)) { $result = mysql_query($SQL) OR die(mysql_error()); if($bg ==='white_div') { $bg = 'grey_div'; } else { $bg = 'white_div'; } echo "<div class=\"$bg\"> \n"; echo '<span class="comments_text">'. $row['opening_paragraph'] .'</span>' . "\n"; echo '</div>' . "\n"; } ?> There was a logic problem in your code. If you want the color to alternate you must include your if statement in the loop so it can flip flop. Link to comment https://forums.phpfreaks.com/topic/85370-solved-using-php-css-for-alternate-colours/#findComment-435585 Share on other sites More sharing options...
kenrbnsn Posted January 10, 2008 Share Posted January 10, 2008 The "if" statement needs to be within the "while" statement: <?php $bg = 'white_div'; $SQL = "select * from blah"; $result = mysql_query($SQL) OR die(mysql_error()); //$bg = ($bg=='white_div' ? 'grey_div' : 'white_div'); --- i know i'm making an equivalent code here: while ($row = mysql_fetch_assoc($result)) { $bg = ($bg == 'white_div')?'grey_div':'white_div'; echo '<div class="' . $bg . '">' . "\n"; echo '<span class="comments_text">'. $row['opening_paragraph'] .'</span>' . "\n"; echo '</div>' . "\n"; } ?> Ken Link to comment https://forums.phpfreaks.com/topic/85370-solved-using-php-css-for-alternate-colours/#findComment-435586 Share on other sites More sharing options...
Mr Chris Posted January 10, 2008 Author Share Posted January 10, 2008 Thanks all! Link to comment https://forums.phpfreaks.com/topic/85370-solved-using-php-css-for-alternate-colours/#findComment-435598 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.