Jump to content

[SOLVED] Alternating colors on database output


Dada78

Recommended Posts

Ok this is the last thing I need to adjust on this page and it will be done. I am trying to have it where each td class on each row is a different color like I had it before instead of just one color all the way down like it is now. I am stuck on how to finish this. I have done this before but I have forgot how I did it and this is as much of the code as I could remember.

 

You can see the page I am talking about. You will notice the rows are all one color. Well the classes are set by td column not by row because of style information.

 

http://mesquitechristmas.com/local/browse.php

 

Here is the code I am working with

 

<?php

include ('db_connect.php');
function show_data(){
   $id = array();
   $imgurl = array();
   $address = array();
   $city = array();
   $state = array();
   $postal = array();
   $country = array();
   $id = mysql_real_escape_string($_GET['id']);
   $i=0;
   if (isset($id)) {
      $sql = "SELECT * FROM users";
      if($result = mysql_query($sql)) {

$color1 = '#DDDDDD';
$color2 = '#EEEEEE';

$i = 0;
         while ($row = mysql_fetch_array ($result)) {

$color = (($i%2 == 0 || $i == 0) ? $color1 : $color2);

            $id[$i] = $row["id"];
            $imgurl[$i] = $row["imgurl"];
            $address[$i] = $row["address"];
            $city[$i] = $row["city"];
            $state[$i] = $row["state"];
            $postal[$i] = $row["postal"];
            $country[$i] = $row["country"];

            print"<tr><td class='bg2'width=110><a href='/local/display.php?id= $id[$i]'><img src='$imgurl[$i]' class='border' height'75' width='100'></a></td><td class='bg2'>$address[$i]<br>$city[$i] $state[$i] $postal[$i]<br>$country[$i]</td><td class='bg2' style='vertical-align: middle'>> <a href='display.php?id=$id[$i]'><b>Learn more about Display</b></a><br><br>> <a href='http://www.mapquest.com/maps/map.adp?searchtype=address&formtype=search&countryid=US&addtohistory=&country=US&address=$address[$i]&city=$city[$i]&zipcode=$postal[$i]&historyid=&submit=Get+Map' target='BLANK'><b>Get Directions</b></a></td></tr>"; ++$i;
         }
      }
   }
}
?> 

 

Can someone help me out with this or maybe know of an easier way to do this or know how to get this done?

 

-Thanks

 

 

Link to comment
Share on other sites

<?php
include ('db_connect.php');
function show_data(){
   $id = array();
   $imgurl = array();
   $address = array();
   $city = array();
   $state = array();
   $postal = array();
   $country = array();
   $id = mysql_real_escape_string($_GET['id']);
   $i=0;
   if(isset($id)) {
      $sql = "SELECT * FROM users";
      if($result = mysql_query($sql)) {
        $i = 0;
         while ($row = mysql_fetch_array ($result)) {
            $color = ($i%2== 0 || $i == 0)?'#DDDDDD':'#EEEEEE';
            $id[$i] = $row["id"];
            $imgurl[$i] = $row["imgurl"];
            $address[$i] = $row["address"];
            $city[$i] = $row["city"];
            $state[$i] = $row["state"];
            $postal[$i] = $row["postal"];
            $country[$i] = $row["country"];

            print"<tr><td class='bg2'width=110><a href='/local/display.php?id= $id[$i]'><img src='$imgurl[$i]' class='border' height'75' width='100'></a></td><td class='bg2'>$address[$i]<br>$city[$i] $state[$i] $postal[$i]<br>$country[$i]</td><td class='bg2' style='vertical-align: middle'>> 
              <a href='display.php?id=$id[$i]'><b>Learn more about Display</b></a><br><br>> <a href='http://www.mapquest.com/maps/map.adp?searchtype=address&formtype=search&countryid=US&addtohistory=&country=US&address=$address[$i]&city=$city[$i]&zipcode=$postal[$i]&historyid=&submit=Get+Map' target='BLANK'>
              <b>Get Directions</b></a></td></tr>"; 
              ++$i;
         }
      }
   }
}
?> 

 

try

Link to comment
Share on other sites

<?php
include ('db_connect.php');
function show_data(){
   $id = array();
   $imgurl = array();
   $address = array();
   $city = array();
   $state = array();
   $postal = array();
   $country = array();
   $id = mysql_real_escape_string($_GET['id']);
   $i=0;
   if(isset($id)) {
      $sql = "SELECT * FROM users";
      if($result = mysql_query($sql)) {
        $i = 0;
         while ($row = mysql_fetch_array ($result)) {
            $color = ($i%2== 0 || $i == 0)?'#DDDDDD':'#EEEEEE';
            $id[$i] = $row["id"];
            $imgurl[$i] = $row["imgurl"];
            $address[$i] = $row["address"];
            $city[$i] = $row["city"];
            $state[$i] = $row["state"];
            $postal[$i] = $row["postal"];
            $country[$i] = $row["country"];

            print"<tr>
                    <td bgcolor='".$color."'width=110>
                      <a href='/local/display.php?id= $id[$i]'>
                        <img src='$imgurl[$i]' class='border' height'75' width='100'>
                      </a>
                    </td>
                    <td bgcolor='".$color."'>$address[$i]<br>$city[$i] $state[$i] $postal[$i]<br>$country[$i]</td>
                    <td bgcolor='".$color."' style='vertical-align: middle'>> 
                      <a href='display.php?id=$id[$i]'><b>Learn more about Display</b></a><br><br>> 
                      <a href='http://www.mapquest.com/maps/map.adp?searchtype=address&formtype=search&countryid=US&addtohistory=&country=US&address=$address[$i]&city=$city[$i]&zipcode=$postal[$i]&historyid=&submit=Get+Map' target='BLANK'>
                      <b>Get Directions</b>
                      </a>
                   </td>
            </tr>"; 
              ++$i;
         }
      }
   }
}

?> 

 

try

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.