Jump to content

[SOLVED] Alternating Row Colors


netpants

Recommended Posts

Ok guys I have read the tutorial on how to do this but it was not working correctly with the way I have things displayed. Can anyone let me know on how to do this with the way I have my code setup. To see the sciprt in action www.onlineproxysite.com/coords/search.html County: Kendall , City: Aurora , Street: Canyon Creek , Suffix: Court . Thanks for your help in advance.

 

<?php 
    
    

// connect and select the database 
$conn = mysql_connect($host, $user, $password) or die(mysql_error()); 
$db = mysql_select_db($dbName, $conn) or die(mysql_error()); 
$county = $_POST['county'];
$city = $_POST['city'];
$street = $_POST['street'];
$zip= $_POST['zip'];
$cp = $_POST['cp'];
$sfx = $_POST['sfx'];


// Insert a row of information into the table "example"
$result = mysql_query("SELECT * FROM coords WHERE street LIKE '$street%' AND city='$city' AND county='$county' AND sfx='$sfx'") 
or die(mysql_error());   

echo "<table border='1'>";
echo "<tr> <th>County</th><th>City</th><th>Block #</th><th>Address</th><th></th><th>Coordinates</th> </tr>";

// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {

// Print out the contents of each row into a table 
echo "<tr><td>"; 
echo $row['county'];
echo "</td><td>"; 
echo $row['city'];
                echo "</td><td>"; 
echo $row['number'];
echo "</td><td>";
                echo $row['street'];
echo "</td><td>";
                echo $row['sfx'];
echo "</td><td>";
                echo $row['coordinates'];
echo "</td></tr>";  

}

?>

Link to comment
https://forums.phpfreaks.com/topic/75594-solved-alternating-row-colors/
Share on other sites

try this, for Alternating Row Colors..

 

<?php
while($row = mysql_fetch_array( $result )) {
$color = ($color="#FF0000")?"#006600":"#FF0000"; //ADDED
// Print out the contents of each row into a table 
echo "<tr bgcolor='$color'><td>";  //UPDATED
echo $row['county'];
echo "</td><td>"; 
echo $row['city'];
echo "</td><td>"; 
echo $row['number'];
echo "</td><td>";
echo $row['street'];
echo "</td><td>";
echo $row['sfx'];
echo "</td><td>";
echo $row['coordinates'];
echo "</td></tr>";  
}
?>

 

 

Here's the explanation of how it works, look at the code, and below, I'll explain the parts that make it work.

 

<?php 
    
// connect and select the database 
$conn = mysql_connect($host, $user, $password) or die(mysql_error()); 
$db = mysql_select_db($dbName, $conn) or die(mysql_error()); 
$county = $_POST['county'];
$city = $_POST['city'];
$street = $_POST['street'];
$zip= $_POST['zip'];
$cp = $_POST['cp'];
$sfx = $_POST['sfx'];


// Insert a row of information into the table "example"
$result = mysql_query("SELECT * FROM coords WHERE street LIKE '$street%' AND city='$city' AND county='$county' AND sfx='$sfx'") 
or die(mysql_error());   

echo "<table border='1'>";
echo "<tr> <th>County</th><th>City</th><th>Block #</th><th>Address</th><th></th><th>Coordinates</th> </tr>";

$color1 = "#FFFFFF";//white, can be changed
$color2 = "#CCCCCC";//gray, can be changed

$color = $color1;//set a default color initially

// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {

if($color == $color1){$color = $color2;} else {$color = $color1;}

// Print out the contents of each row into a table 
echo "<tr style='background-color:$color;'><td>"; 
echo $row['county'];
echo "</td><td>"; 
echo $row['city'];
echo "</td><td>"; 
echo $row['number'];
echo "</td><td>";
echo $row['street'];
echo "</td><td>";
echo $row['sfx'];
echo "</td><td>";
echo $row['coordinates'];
echo "</td></tr>";  

}

?>

 

Explanation

 

$color1 = "#FFFFFF";//white, can be changed
$color2 = "#CCCCCC";//gray, can be changed

$color = $color1;//set a default color initially

// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {

if($color == $color1){$color = $color2;} else {$color = $color1;}

//do stuff here

}

 

As we see above, we have our two colors, let's set $color to $color1. When we start the while statement, we run through the "if" part. Since $color does equal $color1, we set it to $color2. When we re-run the while again (since it loops until there are no more rows), it goes "Hey, $color is equal to $color2, not $color1, so we set it equal to $color1" and vice-versa. Tadaa, alternating colors... (you can even do triple with extra if-else checks.)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.