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
mohson
Forum Contributor
Posts: 372 Joined: Thu Dec 02, 2004 6:58 am
Location: London
Post
by mohson » Thu Dec 16, 2004 10:41 am
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 » Thu Dec 16, 2004 10:53 am
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 » Thu Dec 16, 2004 11:13 am
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 » Thu Dec 16, 2004 11:31 am
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 { background-color: blue }
.odd { background-color: red }
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 » Thu Dec 16, 2004 4:27 pm
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?
mohson
Forum Contributor
Posts: 372 Joined: Thu Dec 02, 2004 6:58 am
Location: London
Post
by mohson » Fri Dec 17, 2004 6:50 am
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 » Fri Dec 17, 2004 12:31 pm
$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 » Fri Dec 17, 2004 4:13 pm
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 » Fri Dec 17, 2004 6:19 pm
harsha
Forum Contributor
Posts: 103 Joined: Thu Jul 11, 2002 1:35 am
Location: Bengaluru (Bangalore) > Karnataka > India
Post
by harsha » Fri Dec 17, 2004 10:24 pm
<?
$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 » Sat Dec 18, 2004 3:40 am
Hi
In the code below
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 » Mon Dec 20, 2004 5:16 am
Thanks guys problem solved