Page 1 of 1

Alternating colours

Posted: Thu Dec 16, 2004 10:41 am
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;
}

Posted: Thu Dec 16, 2004 10:53 am
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..........

Posted: Thu Dec 16, 2004 11:13 am
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;
}

Posted: Thu Dec 16, 2004 11:31 am
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>";

Posted: Thu Dec 16, 2004 4:27 pm
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?

Posted: Thu Dec 16, 2004 4:45 pm
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 ;)

Still not working

Posted: Fri Dec 17, 2004 6:50 am
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;
}

Posted: Fri Dec 17, 2004 12:31 pm
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.

Posted: Fri Dec 17, 2004 4:13 pm
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]

Posted: Fri Dec 17, 2004 6:19 pm
by timvw
everything you wanted to know about php strings http://be2.php.net/manual/en/language.types.string.php

Posted: Fri Dec 17, 2004 10:24 pm
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

Posted: Sat Dec 18, 2004 3:40 am
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

Posted: Mon Dec 20, 2004 5:16 am
by mohson
Thanks guys problem solved