Alternating colours

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Alternating colours

Post by mohson »

Can anyone see why this alternating colors table doesnt work - when i eco my results it formats the table but doesnt present the colors - any help??

Code: Select all

// Define your colors for the alternating rows

$color1 = "#ADD8E6";
$color2 = "#E0FFFF";
$color = $color2;



echo "<table>";
while ($row = mysql_fetch_object($sql)) {
($color==$color2)? $color = $color1 : $color = $color2;

echo '<tr backgroundcolor="$color"><td>'.$count . '</td><td> ' . $row->person_id .'</td><td>'. 
        $row->salutation .'</td><td>'. 
        $row->firstname .'</td><td>'. 
        $row->surname .'</td><td>'. 
        $row->organisation .'</td><td>'.
        $row->role.'</td><td>'.
        $row->address1 .'</td><td>'.
        $row->address2 .'</td><td>'.
        $row->city .'</td><td>'.
        $row->postcode .'</td><td>'.
        $row->telephone .'</td><td>'.
        $row->mobile .'</td><td>'.
        $row->fax .'</td><td>'.
        $row->dateoflastcontact.'</td><td>'.
        $row->datecontactagain  .'</td><td>'.
        $row->email .'</td><td>'.
	$row->notes .'</td></tr>';
$count += 1;
}
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Code: Select all

$color =  ($color == $color2) ? $color1 : $color2;
or

Code: Select all

if ($color == $color2)
{
  $color = $color1;
}
else
{
  $color = $color2;
}
whatever floats your boat..........
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

still doesnt work - tim

Code: Select all

// Define your colors for the alternating rows

$color1 = "#ADD8E6";
$color2 = "#E0FFFF";
$color = $color2;



echo "<table>";
while ($row = mysql_fetch_object($sql)) {
if ($color == $color2)
{
  $color = $color1;
}
else
{
  $color = $color2;
}
echo '<tr backgroundcolor="$color"><td>'.$count . '</td><td> ' . $row->person_id .'</td><td>'. 
        $row->salutation .'</td><td>'. 
        $row->firstname .'</td><td>'. 
        $row->surname .'</td><td>'. 
        $row->organisation .'</td><td>'.
        $row->role.'</td><td>'.
        $row->address1 .'</td><td>'.
        $row->address2 .'</td><td>'.
        $row->city .'</td><td>'.
        $row->postcode .'</td><td>'.
        $row->telephone .'</td><td>'.
        $row->mobile .'</td><td>'.
        $row->fax .'</td><td>'.
        $row->dateoflastcontact.'</td><td>'.
        $row->datecontactagain  .'</td><td>'.
        $row->email .'</td><td>'.
	$row->notes .'</td></tr>';
 
$count += 1;
}
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

the code works..... but the generated html is invalid ;)


the attribute is bgcolor (instead of backgroundcolor)





imho the most elegant way to do is like this


style.css

Code: Select all

.even &#123; background-color: blue &#125;
.odd &#123; background-color: red &#125;
then in your code (instead of color)

Code: Select all

if (($counter % 2) ==0)
   echo "<td class="even">...........</td>";
else
  echo  "<td class="odd">........</td>";
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

thanks tim Ill try that tomorrow form University Last question I have a footer on my webpage which is automatically generated from another table and is standard for all my pages - for some reason my table has shifted underneath my footer so the footer is half way up the page - i was told this is because I havent closed my <td>s properly what do you think because I cant see any errors?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

i don't spot it at first sight... meaby they are talking about the other table?

computers are better in finding errors like this, therefore i always had following links on pages i was building:

http://validator.w3.org/check/referer
http://jigsaw.w3.org/css-validator/check/referer


nowadays i use the firefox webdev extension which allows me to do the same ;)
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Still not working

Post by mohson »

Guys I have put in bgcolor but it still doenst work at one point all I got was black boxes now I am getting nothing any ideas

Code: Select all

$color1 = "#ADD8E6";
$color2 = "#E0FFFF";
$color = $color2;

echo 
"<table>";while ($row = mysql_fetch_object($sql)) 
{($color==$color2)? $color = $color1 : $color = $color2;

echo '<tr bgcolor="$color"><td>'.$count . '</td><td> ' . $row->person_id .'</td><td>'.        
	$row->salutation .'</td><td>'.         
	$row->firstname .'</td><td>'.         
	$row->surname .'</td><td>'.        
 	$row->organisation .'</td><td>'.        
	$row->role.'</td><td>'.       
 	$row->address1 .'</td><td>'.        
	$row->address2 .'</td><td>'.       
 	$row->city .'</td><td>'.       
 	$row->postcode .'</td><td>'.        
	$row->telephone .'</td><td>'.        
	$row->mobile .'</td><td>'.        
	$row->fax .'</td><td>'.        
	$row->dateoflastcontact.'</td><td>'.        
	$row->datecontactagain  .'</td><td>'.       
 	$row->email .'</td><td>'.    
	$row->notes .'</td></tr>';

$count += 1;
}
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

$color isnt being parsed, you used single quotes.


it helps to take a look at the html source that you script outputs when your having problems.
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

Code: Select all

echo '<tr bgcolor="$color"><td>'.$count .
you used single quotes
Im sorry if im getting confused but isnt that double quotes?[/quote]
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

everything you wanted to know about php strings http://be2.php.net/manual/en/language.types.string.php
User avatar
harsha
Forum Contributor
Posts: 103
Joined: Thu Jul 11, 2002 1:35 am
Location: Bengaluru (Bangalore) > Karnataka > India

Post by harsha »

<?
$color1 = "#ADD8E6";
$color2 = "#E0FFFF";
$color = $color2;



echo "<table>";
$i=10;
while ($i-- > 0) {
($color==$color2)? $color = $color1 : $color = $color2;

//Correction here--> echo "<tr><td bgcolor=\"{$color}\">";
//use {$Color} while using a variable in echo or use
echo ($color==$color2)? "One for me" : "One for you";
echo "</td></tr>";
}
echo "</table>";
?>

this works perfectly; you have left </table> tag and in the
ibizconsultants
Forum Commoner
Posts: 35
Joined: Tue Sep 07, 2004 12:07 pm

Post by ibizconsultants »

Hi

In the code below

Code: Select all

echo '<tr bgcolor="$color"><td>'
substitute single quotes with double quotes. If you use single quotes variable substitution does not happen within a string. Its a concept from *NIX platforms. If you want to validate, execute that page and check the source code for the page. This should contain the $color as a text rather than the actual color itself.

Hope this helps.

iBizConsultants
http://www.ibizconsultants.com
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

Thanks guys problem solved
Post Reply