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

 

 

Link to comment
Share on other sites

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.)

Link to comment
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.