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

Link to comment
Share on other sites

<?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
Share on other sites

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
Share on other sites

 

<?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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.