Jump to content

Recommended Posts

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?
Link to comment
https://forums.phpfreaks.com/topic/9408-alternating-row-colors/
Share on other sites

Heres how i did mine: (not using your code but something similar)
[code]
<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>[/code]
Link to comment
https://forums.phpfreaks.com/topic/9408-alternating-row-colors/#findComment-34658
Share on other sites

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

then

[code]

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";
        }

}

[/code]

and there you have it, thats just the code for the table row, but you can put the rest in yourself
Link to comment
https://forums.phpfreaks.com/topic/9408-alternating-row-colors/#findComment-34666
Share on other sites

[!--quoteo(post=372645:date=May 9 2006, 12:29 PM:name=headcutter)--][div class=\'quotetop\']QUOTE(headcutter @ May 9 2006, 12:29 PM) [snapback]372645[/snapback][/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
Link to comment
https://forums.phpfreaks.com/topic/9408-alternating-row-colors/#findComment-34681
Share on other sites

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

if you want more colors
[code]<? $colors = array('FFFFFF','CCCCCC','EAEAFA');
$num = count($colors);
while( ... )
{
   echo "<tr bgcolor=#".$colors[$i%$num]."></tr>";
   $i++;
}
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/9408-alternating-row-colors/#findComment-34786
Share on other sites

This is how I do it:
[code]$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>";
}[/code]
This method is a bit more redundant than the other ones provided.
Link to comment
https://forums.phpfreaks.com/topic/9408-alternating-row-colors/#findComment-34838
Share on other sites

set a variable, lets use bg....

[code]
$bg='#ffffff';
[/code]

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

[code]

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>";
}

[/code]

simple hey?
Link to comment
https://forums.phpfreaks.com/topic/9408-alternating-row-colors/#findComment-34902
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.