Depiction- Webmaster Resources
Stock Image


Contributors: Advertise Here  |  Advertise Here  |  Advertise Here


Misc Links: Submit Tutorials (Earn $$) l Advertising l Privacy Policy | Site Map

Tutorials > PHP > Alternating Backgrounds

 

Alternate the backgrounds of table rows displayed from a MySQL database.


Allright, now lets say that you have a database full of information needed to be displayed on your site. One of the tables contains some short headlines needed on the front page. Now, you could just display them, but they would be hard to determine which is which, so, we'll alternate the background. Using a while loop we will be able to add and subtract from one variable that contains the number of which style to use.


Granted, you have connected to the database, and you have two seperate CSS Styles called "style1" and "style2", take look at this.

Code:
------------------------------------------------------------------------
$sql = mysql_query("SELECT * FROM headlines ORDER BY date LIMIT 5"); //Will select the 5 latest entries from the table "Headlines"

$i = 0; //The Variable that will hold which will determine the style.
while ($row = mysql_fetch_array($sql, MYSQL_ASSOC) {

//The important if statement
if ($i == "0") {$style = "style1"; $i++ } else { $style = "style2"; $i--; }

$style = "style1"; //Since the number is "0", the style is "style1"

$i++ //Add one to the style selector.
$content = $row['content'];
echo '<div class="'.$style.'">$content</div>';
}

------------------------------------------------------------------------

The variable "$i" at first, contains the integer 0. But each the time the while loops goes through the SQL query, it adds or subtracts the number. The if statement does the selecting for us. It in our language says:

If the variable $i is set to "0", set $style to "style1" then add one to $i.

If it is not set to "0", then set $style> to "style2" and then subtract one from $i.


Ok, thats it. Expand your skills in the PHP and webdeisgn using this tutorial.

Heres the full code for reference:

Code:
------------------------------------------------------------------------
<?PHP
$cnx = mysql_connect("localhost", "root", ""0) or die("Error Connecting to Database"); // Connect to Database, saving it as a variable for later uses. If there is an error, it will end or, "die"
mysql_select_db("database", $cnx) or die("Error in selecting database"); //Select the Database. Error will make the script "die" and send an error message.

$sql = mysql_query("SELECT * FROM headlines ORDER BY date LIMIT 5"); //Will select the 5 latest entries from the table "Headlines"

$i = 0; //The Variable that will hold which will determine the style.
while ($row = mysql_fetch_array($sql, MYSQL_ASSOC) {

//The important if statement
if ($i == "0") {$style = &quot;style1&quot;; $i++ } else { $style = &quot;style2&quot;; $i--; }

$content = $row['content'];
echo '&lt;div class=&quot;'.$style.'&quot;&gt;$content&lt;/div&gt;';
mysql_close($cnx);
?>

------------------------------------------------------------------------


Tutorial Written By: Clayton

 
 

Affiliates

Adobe Tutorialz Photoshop Tutorials
Tutorial Man Wallpaper Stock  More

Resources