[advanced search]
31 members in the live chat room. Join Chat!
Register Rules & FAQ NP$ Store Active Threads Mark Forums Read
Go Back   NamePros.Com > Design and Development > Webmaster Tutorials
User Name
Password

Old 04-08-2006, 02:32 AM   · #1
asgsoft
NamePros Regular
 
asgsoft's Avatar
 
Location: At Home
Trader Rating: (36)
Join Date: Sep 2005
Posts: 805
NP$: 1406.10 (Donate)
asgsoft is just really niceasgsoft is just really niceasgsoft is just really niceasgsoft is just really niceasgsoft is just really nice
Alternating row colours

Have you ever wanted to make a table with alternating row colours?

Well I did it for a site and thought it would be great to share it. Here

PHP Code:
<?php
//connect to db and select table
$db1 = mysql_connect("host", "user", "pass");
mysql_select_db("$db",$db1);
$table = "tablename"; //table name


$list = mysql_query("SELECT * FROM `$table` ORDER BY `coulmn_name` ASC "); // the SQL of the table you will show
echo"<h3><p align=center> $table <p></h3>"; //title for the page
echo('<table width="500px" height="110px" style="font-family: Verdana; border: 1px solid black;" align="center" cellpadding="2" cellspacing="1" border="0">'); //add the table
echo('<tr align="center"><td><b><u>Date</u></b></td><td><b><u>description</u></b></td></tr>'); // top row with the titles
while($get=mysql_fetch_array($list))
{
//chooses colous
if ($color == "CCFFCC")
{
$color = "FFFFFF";//change the code
}
else
{
$color = "CCFFCC"; //change the code
}

echo(
'<tr bgcolor=#'.$color.' align="center">  <td>'. $get['Date'] .'</td><td>'. $get['description'] .'</td></tr>');  //add the contents
}
echo(
"</table>"); //finish the table

?>


See it on action at: http://www.asgsoft.net/mosque/view%...20a%20month.php


Please register or log-in into NamePros to hide ads
asgsoft is offline   Reply With Quote
Old 04-09-2006, 05:09 PM   · #2
Epic
Senior Member
 
Epic's Avatar
 
Trader Rating: (88)
Join Date: Sep 2005
Posts: 3,534
NP$: 251.25 (Donate)
Epic has a brilliant futureEpic has a brilliant futureEpic has a brilliant futureEpic has a brilliant futureEpic has a brilliant futureEpic has a brilliant futureEpic has a brilliant futureEpic has a brilliant futureEpic has a brilliant futureEpic has a brilliant futureEpic has a brilliant future
Thank you!!!! This is exactly what I've been looking for lately...I'll try this out as soon as I can get my monitor working

But thank you so much!
Epic is offline   Reply With Quote
Old 04-09-2006, 10:07 PM   · #3
wackyjoe
NamePros Regular
 
Trader Rating: (4)
Join Date: Dec 2005
Posts: 206
NP$: 270.00 (Donate)
wackyjoe is on a distinguished road
Nice Tutorial ... another way would be:

PHP Code:
echo "<table>";
$color1 = "#EEEEEE";
$color2 = "#FFFFFF";
$rowc = 0;

$sql = mysql_query("SELECT * FROM places ORDER BY id LIMIT 5");

while (
$row = mysql_fetch_array($sql) {

$row_color = ($rowc % 2) ? $color1 : $color2;  //basicly like a if()

echo "
   <tr>
    <td width="
110" bgcolor="$row_color" nowrap>
    $article_date</td>
    <td bgcolor="
$row_color">
    <a href="
whteva.php?cmd=full_article&article_id=$article_id">$article_title</a></td>
    </tr>"
;
   
   

    
$rowc++; //increment by 1
}
echo
"</table>";
wackyjoe is offline   Reply With Quote
Old 06-13-2006, 07:53 AM   · #4
apexad
NamePros Regular
 
apexad's Avatar
 
Location: www.restaurantselector.com
Trader Rating: (6)
Join Date: Jul 2005
Posts: 733
NP$: 0.00 (Donate)
apexad has a spectacular aura aboutapexad has a spectacular aura about
I used something similar to this on my site (in my signature) it works really well. Tables can be hard to read, but alternating colors adds just enough of a difference that you can navigate through them.

rep points added to you guys!
apexad is offline   Reply With Quote
Old 06-14-2006, 12:38 AM   · #5
psalzmann
NamePros Regular
 
Trader Rating: (19)
Join Date: Feb 2006
Posts: 483
NP$: 360.80 (Donate)
psalzmann is just really nicepsalzmann is just really nicepsalzmann is just really nicepsalzmann is just really nice
Originally Posted by wackyjoe
Nice Tutorial ... another way would be:

PHP Code:
echo "<table>";
$color1 = "#EEEEEE";
$color2 = "#FFFFFF";
$rowc = 0;

$sql = mysql_query("SELECT * FROM places ORDER BY id LIMIT 5");

while (
$row = mysql_fetch_array($sql) {

$row_color = ($rowc % 2) ? $color1 : $color2;  //basicly like a if()

echo "
   <tr>
    <td width="
110" bgcolor="$row_color" nowrap>
    $article_date</td>
    <td bgcolor="
$row_color">
    <a href="
whteva.php?cmd=full_article&article_id=$article_id">$article_title</a></td>
    </tr>"
;
   
   

    
$rowc++; //increment by 1
}
echo
"</table>";



Yes, this is the best way I found. I've been using this way ever since
psalzmann is offline   Reply With Quote
Old 06-14-2006, 09:33 AM   · #6
rmal
New Member
 
Name: Ryan Malesevich
Location: whitewater, wi
Trader Rating: (0)
Join Date: Jun 2006
Posts: 16
NP$: 0.00 (Donate)
rmal is an unknown quantity at this point
The second way is usually the method that I use, just much simpler putting the if on one line.
__________________
~Ryan
Ryan's Life my personal blog, it's not about anything
rmal is offline   Reply With Quote
Closed Thread

NamePros is a revenue sharing forum.

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


Site Sponsors
http://www.internetinvestments.com/ Thousand Dollar Profits Build your NameBrand
Advertise your business at NamePros
All times are GMT -7. The time now is 03:09 PM.


Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 2.4.0