Zebra-style Tables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

phpflixnewbie
Forum Contributor
Posts: 132
Joined: Fri Nov 17, 2006 11:46 am

Zebra-style Tables

Post by phpflixnewbie »

Hi guys i apologize for asking something about a topic which has been regularly posted here, but as i am a complete beginner i need some code that i can just input into the code i already have written. I basically want to alternate the colours of the rows (using just 2 colours). My php code so far is below:

Code: Select all

$Host = "localhost"; //you can use IP address instead of localhost
$User = "my_username";
$Password = "my_password";
$Database = "my_database";

$Link_ID=mysql_pconnect($Host, $User, $Password);
     if (!$Link_ID)
     {
        echo "Failed to connect to Server=".$Host;
          return 0;
     }
     else
     {
         echo "<B>Successfully to connected to Server  </B>" .$Host;
     }


     if (!@mysql_select_db($Database,$Link_ID))
     {
         echo "<br>Cannot use database=  " .$Database;
      }
      else
     {
          echo "<br> Successfully connected to database= " .$Database;
      }
// Performing SQL query
$query = 'select dvd_title, avg(rating), dvd_rlsdate from dvd_ratings, dvd_titles where dvd_titles.dvd_id=dvd_ratings.dvd_id group by dvd_ratings.dvd_id limit 10';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

// Printing results in HTML
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
   echo "\t<tr>\n";
   foreach ($line as $col_value) {
       echo "\t\t<td>$col_value</td>\n";
   }
   echo "\t</tr>\n";
}
echo "</table>\n";


// Free resultset
mysql_free_result($result);

// Closing connection
mysql_close($Link_ID);

I have a call the a table style class in the html/css which specifies its position, font and background colour:

Code: Select all

table  {color: #000060; border-collapse: seperate; border-spacing:2px; margin-top:150px; margin-left:390px; text-align: left; vertical-align: baseline; font-family:Verdana; font-size:11px; font-weight: bold; background: #fffff5;}
Ive tried adding css tr.even and odd classes but it either generates errors or nothing takes affect at all, so a php method would be much appreciated.

regards,
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

here's a good start


viewtopic.php?t=57702
phpflixnewbie
Forum Contributor
Posts: 132
Joined: Fri Nov 17, 2006 11:46 am

Post by phpflixnewbie »

wtf, thx for the reply. but how/where would i integrate any of those examples into my code?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Try it yourself first.
phpflixnewbie
Forum Contributor
Posts: 132
Joined: Fri Nov 17, 2006 11:46 am

Post by phpflixnewbie »

I've tried using the code below in several places in my code, but i just get errors.

Code: Select all

$class = ( $i % 2 == 0 ) ? $red : $blue;

<tr class="<?php echo $class; ?>" ...
Please advise me.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If you're going to use the modulo variant, then the general concept is thus:

You create a counter, which counts the number of iterations. Using this counter you are able to switch (typically) between two choices, as you've found.

Seek out the other threads that have referenced zebra for further examples (usually more working than conceptual, as the previously linked thread is.)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Read the errors and fix them :)
I don't see $red, $blue or $i defined in your code anywhere.
phpflixnewbie
Forum Contributor
Posts: 132
Joined: Fri Nov 17, 2006 11:46 am

Post by phpflixnewbie »

Also tried:

Code: Select all

echo ($i++ & 1) ? 'FFFFCC' : '33FFFF';
but it has no affect, please help!!! :cry:
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

I'll post it again, just in case you missed it the first time...

READ: viewtopic.php?t=57702

It will solve your problems... try it your self, play, look at the errors and think why they may be occurring... Stripping tables like this is a very simple and common thing to do...
phpflixnewbie
Forum Contributor
Posts: 132
Joined: Fri Nov 17, 2006 11:46 am

Post by phpflixnewbie »

I decided to use this code someone gave me:

Code: Select all

$odd = true;
echo "<table>\n";
echo "<tr><th>Title</th><th>Rating</th><th>DVD Release Date</th><tr>";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
   if ($odd) echo "\t<tr bgcolor=\"F2F9FF\">\n";
   else echo "\t<tr bgcolor=\"FFFFEA\">\n";
   foreach ($line as $col_value) {
       echo "\t\t<td>$col_value</td>\n";
   }
   echo "\t</tr>\n";
   $odd = !$odd;
}
echo "</table>\n";
The problem with the other examples wasnt not being able to understand the code, but what i needed to replace in MY code, thankfully someone showed me what to replace and im pleased with the result.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

phpflixnewbie wrote: The problem with the other examples wasnt not being able to understand the code, but what i needed to replace in MY code
To me it appears that you're saying: i understand the examples, but i don't understand my own code?
phpflixnewbie
Forum Contributor
Posts: 132
Joined: Fri Nov 17, 2006 11:46 am

Post by phpflixnewbie »

Yes some of it i didnt understand, the code was derived from the php manual.
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

look at the 1st post of yours on this topic
Replace

Code: Select all

echo "\t\t<td>$col_value</td>\n";
Of That Post
With

Code: Select all

$clr[]="red";
$clr[]="green";
$num=0;
if($num==0)
  {
    echo "\t\t<td bgcolor=\".$clr[0].\">$col_value</td>\n";
    $num = 1;
  }
else
  {
    echo "\t\t<td bgcolor=\".$clr[1].\">$col_value</td>\n";
    $num = 0;
  }
timclaason
Forum Commoner
Posts: 77
Joined: Tue Dec 16, 2003 9:06 am
Location: WI

My $0.02

Post by timclaason »

Completely irrelevant now, but I thought I'd add my $0.02.

For alternating color rows, I usually do it like this:

Code: Select all

function rowColor($rowNum) {
   if($rowNum%2) {
          $this->color = "#EEEEEE";
   }
   else {
           $this->color = "#FFFFFF";
   }
   return $this->color;
}
Then, in the HTML, I do this:

Code: Select all

$count = 0;
   $SQL = mysql_query("SELECT...", $dblink);
   while($row = mysql_fetch_array($SQL)) {
          print("<TR BGCOLOR='".rowColor($count)."'><TD>HTML CODE</TD></TR>");
          $count++;
   }
I find it most simple, but then again, I'm a simple guy.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: My $0.02

Post by onion2k »

Post Reply