Jump to content

[SOLVED] Using php & css for alternate colours?


Mr Chris

Recommended Posts

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

<?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.

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";
     }
?>

 

<?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.

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

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.