PHP simple : PHP tutorial, MySQL tutorial
PHP Tutorial - Free PHP Script

 
Home | Tutorials | PHP & MySQL Resources | Web Tools

Alternating Table Row Colors


Tutorials > Alternating Table Row Colors

Make a very simple alternating table row colors in short codings.

This tutorial require 1 PHP file and 1 table of mySQL database.

  1. alternating_colors.php
  2. Database "tutorial" and table "name_list" with 2 fields: id(auto_increment), name(varchar, 50) and put some records about 20 - 30 records into this table. (directly by phpMyAdmin)


alternating_color.php

Source Code
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head><body>
<?
// Connect database.
mysql_connect("localhost","","");
mysql_select_db("tutorial");

// Get data records from table.
$result=mysql_query("select * from name_list order by id asc");
?>

<table border="1">
<tr>
<td bgcolor="#FF9900"><strong>No.</strong></td>
<td bgcolor="#FF9900"><strong>Company</strong></td>
</tr>
<?
// Make a variable "$num" with a value "1".
$num=1;

// Do while loop for out put records.
while($row=mysql_fetch_assoc($result)){

// Plus 1 at $num.
$num++;

// Use modulus by 2 in $num value and set the value of "$bg" if result equal 0 or not.
if(($num%2)!=0){
$bg="#FFCC00";
}else{
$bg="#FFFF00";
}
?>

<tr bgcolor="<? echo $bg; ?>">
<td><? echo $row['id']; ?></td>
<td><? echo $row['name']; ?></td>
</tr>
<?
// End while loop.
}

// Close database connection.

mysql_close();
?>

</table>
</body>
</html>

Advertise
Put Ebay RSS Feeds onto your website
SEO Elite Software
Domain Dashboard CPanel & Seo Manager
Work at Home Ideas and Opportunities
contact lenses : Lens.com offers a huge online inventory of brand name contact lenses with reasonably priced prices. Services include online order and shipping straight to you.
Introduction & Get Start
Using Form
PHP
PHP Tags
PHP Variables
PHP Control Structures
MySQL
Create Database & Table
Database Connection
Insert Record
Select Record
Update Record
Delete Record

PHPsimple.net
info@phpsimple.net