Jump to content


Photo

alternating row colors


  • Please log in to reply
7 replies to this topic

#1 headcutter

headcutter
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 09 May 2006 - 04:29 PM

Hi, I am trying to make alternating row colors work reading this tutorial:
[a href=\"http://www.geekpedia.com/prog_ttrls.php?id=10\" target=\"_blank\"]http://www.geekpedia.com/prog_ttrls.php?id=10[/a]

Anyway it is not working, so I am wondering what I did wrong or if there is an easier way to do it since the code is quite long.

Here is what I have done so far:

<?php
$data = mysql_query("SELECT * FROM friends ORDER BY name")
or die(mysql_error());
$i = 0;
Print "<table border=0 width=100%>";
Print "<tr>";
Print "<th>Name:</th>";
Print "<th>Pet:</th> </tr>";
while($row = mysql_fetch_array( $data ))
{
if($i%2 == 0)
{
Print "<tr bgcolor=#FFFFFF><td><a href=\"" . $row['pet'] . ".html\">" . $row['pet'] . "</a></td>";
Print "<td><a href=\"pets.html\">".$row['pet'] . "</a></td></tr>";
}
else
{
Print "<tr bgcolor=#CCCCCC><td><a href=\"" . $row['pet'] . ".html\">" . $row['pet'] . "</a></td>";
Print "<td><a href=\"pets.html\">".$row['pet'] . "</a></td></tr>";
$i++;
}
}
Print "</table>";
mysql_close();
?>

Any suggestions?


#2 aeafisme23

aeafisme23
  • Members
  • PipPipPip
  • Advanced Member
  • 121 posts
  • LocationUSA

Posted 09 May 2006 - 04:46 PM

Heres how i did mine: (not using your code but something similar)
<table width="798" cellpadding="0" cellspacing="0" border="1" bordercolor="black" align="center">
    <tr>
        <td valign="top" bgcolor="#ffffcc">
          <table class="events" cellpadding="2" cellspacing="0" align="center" bgcolor="#ffffcc"> 
              <tr>
                <td colspan="7>
                <p>events<br></p></td>
            </tr>
            <tr>
                <td class="date"><p class="header">Date</p></td>
                <td class="citystate"><p class="header">City/State</p></td>
                <td class="event"><p class="header">Event</p></td>
                <td class="address"><p class="header">Address</p></td>
                <td class="sponsor"><p class="header">Sponsor</p></td>
                <td class="mediaco"><p class="header">Media Co. </p></td>
                <td class="time"><p class="header">Time</p></td>
            </tr>
     
<?php 
   //Setup connection to the database 
   $connect = mysql_pconnect("www.domain.com", "user", "pass") 
   or die(mysql_error()); 
    
   //Connect to the database 
    mysql_select_db("dbname", $connect) or die(mysql_error());    
    
   // Perform an SQL query on the Address table    
    $sql_address = mysql_query("SELECT Date, City, Event, Address, Sponsor, Media , Time from events order by date, city") 
   or die (mysql_error()); 
       
    // Define the colours for the alternating rows 
    $colour_odd = "#ffffcc"; 
    $colour_even = "#FFF2BF"; 
   $row_count = 0;  //To keep track of row number 
    
   // Fetch the array of records and Loop through them 
    while($results = mysql_fetch_array($sql_address)) 
   { 
     // Decide which colours to alternate for the rows 
     // If Remainder of $row_count divided by 2 == 0. 
     $row_color = (($row_count % 2) == 0) ? $colour_even : $colour_odd; 
     
     /* Echo the table row and table data that needs to be looped Check All  /  Uncheck All
     <td><a href="' . $results['state'] . '">' .$results
     With selected: over until the end of the recordset*/     
     echo ' 
     <tr bgcolor="' . $row_color . '"> 
      <td><p class=info>' .$results['Date'] . '</p></td> 
      <td><p class=info>' .$results['City'] . '</a></p></td> 
      <td><p class=info>' .$results['Event'] . '</a></p></td>
      <td><p class=info>' .$results['Address'] . '</a></p></td>
      <td><p class=info>' .$results['Sponsor'] . '</a></p></td>
      <td><p class=info>' .$results['Media'] . '</a></p></td>
      <td><p class=info>' .$results['Time'] . '</a></p></td>
      </tr>'; 
     
     // Increment the row count 
     $row_count++; 
   } 
   // Free the MySQL resource 
   mysql_free_result($sql_address); 
   // Close the database connection 
   mysql_close($connect); 
   ?> 
  <!--Close out the table structure--> 
          </table> <br><br>
        </td>
  </tr>
  <tr bgcolor="ffffcc"> 
    <td height="62"> 
      <div align="center"><font size="2">etc....</font></div></td>
  </tr>
</table>


#3 mort

mort
  • Members
  • PipPipPip
  • Advanced Member
  • 82 posts

Posted 09 May 2006 - 05:10 PM

all you need to do is find out how many rows are in the query with mysql_num_rows

then


for($i = 0; $i < $numquery; $i++) {

you array and variables here

        if($i % 2) { //this means if there is a remainder
            echo "<TR class=\"tstyle_rowa\">\n";
        } else { //if there isn't a remainder we will do the else
            echo "<TR class=\"tstyle_rowb\">\n";
        }

}


and there you have it, thats just the code for the table row, but you can put the rest in yourself

#4 cmgmyr

cmgmyr
  • Members
  • PipPipPip
  • Advanced Member
  • 1,278 posts
  • LocationUSA

Posted 09 May 2006 - 06:13 PM

Thats how I did one of my sites. It works out pretty well, it's fast and pretty simple.

#5 JRS

JRS
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts

Posted 09 May 2006 - 06:15 PM

[!--quoteo(post=372645:date=May 9 2006, 12:29 PM:name=headcutter)--][div class=\'quotetop\']QUOTE(headcutter @ May 9 2006, 12:29 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Hi, I am trying to make alternating row colors work reading this tutorial:
[a href=\"http://www.geekpedia.com/prog_ttrls.php?id=10\" target=\"_blank\"]http://www.geekpedia.com/prog_ttrls.php?id=10[/a]

Anyway it is not working, so I am wondering what I did wrong or if there is an easier way to do it since the code is quite long.

Here is what I have done so far:

<?php
$data = mysql_query("SELECT * FROM friends ORDER BY name")
or die(mysql_error());
$i = 0;
Print "<table border=0 width=100%>";
Print "<tr>";
Print "<th>Name:</th>";
Print "<th>Pet:</th> </tr>";
while($row = mysql_fetch_array( $data ))
{
if($i%2 == 0)
{
Print "<tr bgcolor=#FFFFFF><td><a href=\"" . $row['pet'] . ".html\">" . $row['pet'] . "</a></td>";
Print "<td><a href=\"pets.html\">".$row['pet'] . "</a></td></tr>";
}
else
{
Print "<tr bgcolor=#CCCCCC><td><a href=\"" . $row['pet'] . ".html\">" . $row['pet'] . "</a></td>";
Print "<td><a href=\"pets.html\">".$row['pet'] . "</a></td></tr>";
$i++;
}
}
Print "</table>";
mysql_close();
?>

Any suggestions?
[/quote]
HC,
Your code is not working because $i is not getting incremented - you need to move the $i++ outside the else statement and it should work.
Good luck
JRS

#6 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 10 May 2006 - 12:27 AM

well this is a much more flexible way
<? $colors = array('FFFFFF','CCCCCC');
while( ... )
{
   echo "<tr bgcolor=#".$colors[$i%2]."></tr>";
   $i++;
}
?>

if you want more colors
<? $colors = array('FFFFFF','CCCCCC','EAEAFA');
$num = count($colors);
while( ... )
{
   echo "<tr bgcolor=#".$colors[$i%$num]."></tr>";
   $i++;
}
?>

Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#7 Brandon Jaeger

Brandon Jaeger
  • Members
  • PipPipPip
  • Advanced Member
  • 155 posts
  • LocationPA
  • Age:25

Posted 10 May 2006 - 07:03 AM

This is how I do it:
$num = 0;

while($row = mysql_fetch_assoc($result)) {
  if(!$num) {
    $color = "#FFFFFF";
    $num++;
  } else {
    $color = "#F0F0F0";
    $num--;
  }
  echo "<tr><td bgcolor=\"" . $color . "\">" . $content . "</td></tr>";
}
This method is a bit more redundant than the other ones provided.
PHP is my friend (=

#8 benwhitmore

benwhitmore
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 10 May 2006 - 12:56 PM

set a variable, lets use bg....

$bg='#ffffff';

now, in your while loop put in your array... and then set the background colour of your table


while($row = mysql_fetch_array( $data )){
$bg=($bg=='#ffffff'?'#FFFFCC':'#ffffff');

Print "<tr bgcolor=$bg><td><a href=\"" . $row['pet'] . ".html\">" . $row['pet'] . "</a></td>";
Print "<td><a href=\"pets.html\">".$row['pet'] . "</a></td></tr>";
} 


simple hey?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users