Page 1 of 2

Zebra-style Tables

Posted: Fri Nov 17, 2006 12:13 pm
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,

Posted: Fri Nov 17, 2006 12:46 pm
by wtf
here's a good start


viewtopic.php?t=57702

Posted: Fri Nov 17, 2006 1:40 pm
by phpflixnewbie
wtf, thx for the reply. but how/where would i integrate any of those examples into my code?

Posted: Fri Nov 17, 2006 1:45 pm
by feyd
Try it yourself first.

Posted: Fri Nov 17, 2006 2:03 pm
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.

Posted: Fri Nov 17, 2006 2:16 pm
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.)

Posted: Fri Nov 17, 2006 2:17 pm
by Ollie Saunders
Read the errors and fix them :)
I don't see $red, $blue or $i defined in your code anywhere.

Posted: Fri Nov 17, 2006 3:14 pm
by phpflixnewbie
Also tried:

Code: Select all

echo ($i++ & 1) ? 'FFFFCC' : '33FFFF';
but it has no affect, please help!!! :cry:

Posted: Fri Nov 17, 2006 4:12 pm
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...

Posted: Sat Nov 18, 2006 9:23 am
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.

Posted: Sat Nov 18, 2006 10:47 am
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?

Posted: Sat Nov 18, 2006 10:50 am
by phpflixnewbie
Yes some of it i didnt understand, the code was derived from the php manual.

Posted: Wed Dec 13, 2006 7:45 am
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;
  }

My $0.02

Posted: Wed Dec 13, 2006 9:39 am
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.

Re: My $0.02

Posted: Wed Dec 13, 2006 1:26 pm
by onion2k