PHP is open source scripting language. It\'s widely used to develop web applications.  Home Web Programming PHP Alternating Row Colors
Your Ad Here

Alternating Row Colors

Browse Pages: 2  3  > >>

Alternating Row ColorsThere's two different ways this can be done. The first is by getting results from a mysql query, the second is going through the values of an array. I'll go over both. First the full files (ill break it up into two files, one using mysql, and one using an array)

First using mysql

<?

//db vars
$db_host = "host"; //change to your host
$db_user = "user"; //change to your user
$db_pass = "pass"; //change to your password
$db = "db"; //change to your db

//set a GLOBALS var to connect to the database
$GLOBALS['connection'] = mysql_connect($db_host, $db_user, $db_pass)
or die( "Unable to connect to SQL server");
mysql_select_db($db)
or die( "Unable to select database");

echo<<<EOF
<!DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

<html>
<head>
<title>Title Here</title>
<style type="text/css">
td.RowOne {
background-color: #bfd9ff;
border: 1px solid #000000;
}
td.RowTwo {
background-color: #e8e8e8;
border: 1px solid #000000;
}
</style>
EOF;

//Set the Rows
$class1 = "RowOne";
$class2 = "RowTwo";
$class = $class1;

//Start the table
$table = "<table>";

//Get data from mysql table
$db_qur = "select * from TABLE";
$db_res = mysql_query($db_qur, $GLOBALS['connection']);
while($db_row = mysql_fetch_assoc($db_res)){
$table .= "<tr><td class="$class">$db_row[RowName]</td></tr>";
if($class == $class1){
$class = $class2;
} else {
$class = $class1;
}
}
$table .= "</table>";

//echo the table
echo $table;
?>

Next the file using an array

<?
echo<<<EOF
<!DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

<html>
<head>
<title>Title Here</title>
<style type="text/css">
td.RowOne {
background-color: #bfd9ff;
border: 1px solid #000000;
}
td.RowTwo {
background-color: #e8e8e8;
border: 1px solid #000000;
}
</style>
EOF;

//Build the array
$array = array("value1", "value2", "value3", "value4", "value5");

//Set the Rows
$class1 = "RowOne";
$class2 = "RowTwo";
$class = $class1;

//Start the table
$table = "<table>";

//Loop through the array
foreach($array as $arr_value){
$table .= "<tr><td class="$class">$arr_value</td></tr>";
if($class == $class1){
$class = $class2;
} else {
$class = $class1;
}
}
$table .= "</table>";

//echo the table
echo $table;
?>

So theres the two different ways to do this. On the next pages I'll break everything down explaining whats going on.



Author's URL: Nathanael Mitchell
Thank you for voting.
Rate this Materials:
Bad 
1 2 3 4 5 Excellent
Browse Pages: 2  3  > >>
print this page subscribe to newsletter subscribe to rss

Web programming � everything from the basics of visual design and architecture to the specifics of applications, graphics, and scripting. More Web Programming: Most Popular Materials | Fresh Materials | More PHP Tutorials at LearnPHP.org

Add comments to "Alternating Row Colors"

Only registered users can write comment

Reader's comments
comments Elangi November 16, 2005 says:
Alternating Row Colors
Hai,
Ths s elango to all.When i m applying the Alternating rows colors it dosen't change the colors.I m eagerly copy and pate it in my project but it s not working .Could u plz solve my query..
i send my copy of that code here.i m waiting for u r early and postive reply for u.
with regards,

Elango

code: Here
------------





<%

Dim rstemp
Dim SqlTemp
Dim oddsevens
oddsevens = 1

'Set Conn = Server.CreateObject("ADODB.Connection")
Set rstemp = Server.CreateObject("ADODB.recordset")
'Conn.connectionstring =
' "Provider=Microsoft.Jet.OLEDB.4.0;Data
' Source=" & Server.MapPath("test.mdb")& ";"
'Conn.Open
SQLTemp = "SELECT nickname FROM winnerall"
set rstemp=Con.execute(SQLTemp)
%>


<%= rstemp.Fields("nickname").Value %>


<%
Else
%>

<%= rstemp.Fields("nickname").Value %>
<% End If %>


<%
intNum = intNum + 1
rstemp.MoveNext
Loop
%>


<%
Do While Not rstemp.EOF
If (oddsevens MOD 2 = 0) Then
%>


comments Elangi November 16, 2005 says:
Alternating Row Colors
Hai,
Ths s elango to all.When i m applying the Alternating rows colors it dosen't change the colors.I m eagerly copy and pate it in my project but it s not working .Could u plz solve my query..
i send my copy of that code here.i m waiting for u r early and postive reply for u.
with regards,

Elango

code: Here
------------





<%

Dim rstemp
Dim SqlTemp
Dim oddsevens
oddsevens = 1

'Set Conn = Server.CreateObject("ADODB.Connection")
Set rstemp = Server.CreateObject("ADODB.recordset")
'Conn.connectionstring =
' "Provider=Microsoft.Jet.OLEDB.4.0;Data
' Source=" & Server.MapPath("test.mdb")& ";"
'Conn.Open
SQLTemp = "SELECT nickname FROM winnerall"
set rstemp=Con.execute(SQLTemp)
%>


<%= rstemp.Fields("nickname").Value %>


<%
Else
%>

<%= rstemp.Fields("nickname").Value %>
<% End If %>


<%
intNum = intNum + 1
rstemp.MoveNext
Loop
%>


<%
Do While Not rstemp.EOF
If (oddsevens MOD 2 = 0) Then
%>