Colour Problems...

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
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Colour Problems...

Post by Joe »

This is really a question out of curiosity but you know on a website where each setion of the news is a different colour, like:

<tr><td bgcolor='#eeeeee'>
News Section 1...

<tr><td bgcolor='#ffffff'>
News Section 2...

Well I was trying to do that with the php code:

while (true)
{
$row = mysql_fetch_assoc($result);
if ($row == false) break;
echo "<tr bgcolor='#eeeeee'>";
echo "<b>".$row['news']."<br></b>";
}

But I cannot get it to add colour #ffffff for the next section of the news. Its hard to explain this but is it possible...


Joe
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$colors = array('eeeeee','ffffff');

$cnt = 0;
while($row = mysql_fetch_assoc($result))
{
  echo "<tr bgcolor="#{$colors[$cnt]}">";
  echo "<td><b>{$row['news']}<br /></b></td></tr>";
  $cnt++;
  $cnt %= sizeof($colors);
}
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Colour Problems...

Post by John Cartwright »

The way I do it is

Code: Select all

<?

$i=1;
while  ($row = mysql_fetch_assoc($result)) 
{ 
 
 if ($i % 2) == 0 { $bg="#ffffff";}else
                         { $bg="#eeeeee";} 

 echo "<tr bgcolor=".$bg.">";
 echo "<b>".$row['news']."<br></b>";
 $i++;
}

?>
EDIT: grr everyone is owning me today
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Thanks a bunch guys!
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Well now I am having a problem yet again! I have tried everything with this colour project but just cannot seem to beat it. It only shows one colour (#eeeeee) and all of the news articles are running from ID #1. My code is:

$sql = "SELECT * FROM news ORDER BY ID";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);

$headline = $row['headline'];
$username1 = $row['username'];
$mainnews = $row['mainnews'];
$date = $row['date'];
$i = 1;
$j = $i % 2;

while ($row = mysql_fetch_assoc($result))
{
if ($j == 0)
$bg = "#e1e1e1";
else
$bg = "#eeeeee";

echo "<tr><td bgcolor=".$bg.">";
echo "<font face='verdana' size=1>";
echo "<b>.:$headline:.</b> Posted By: $username1 - <i>$date</i><br><hr color='black'>";
echo "$mainnews<p>";
echo "<a href='changes/edit4.php'>Edit Post</a> ";
echo "</td></tr>";
}


Any help appreciated!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

<?php
$sql = "SELECT * FROM news ORDER BY ID"; 
 $result = mysql_query($sql) or die(mysql_error());
 $row = mysql_fetch_assoc($result);

 $headline = $row['headline'];
 $username1 = $row['username'];
 $mainnews = $row['mainnews'];
 $date = $row['date'];
 $i = 1;

 while  ($row = mysql_fetch_assoc($result))
 {
  if ($i % 2 == 0) {
  $bg = "#e1e1e1";
  else
  $bg = "#eeeeee";
  }
  $i ++
  echo "<tr><td bgcolor=".$bg.">"; 
  echo "<font face='verdana' size=1>"; 
  echo "<b>.:$headline:.</b> Posted By: $username1 - <i>$date</i><br><hr color='black'>"; 
  echo "$mainnews<p>";
  echo "<a href='changes/edit4.php'>Edit Post</a> ";
  echo "</td></tr>";
 }

?>
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

The colouring works great now, thanks. The only problem is that its only reading ID #1 from th db table. Any suggestion???

Joe 8)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

probably has something to do with at least 2 fetches happening without reseting the sql pointers..

look into mysql_data_seek()
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Hey great thanks a lot guys for all of your help. In the end up I managed to get it using:

$sql = "SELECT * FROM news ORDER BY ID";
$result = mysql_query($sql) or die(mysql_error());
$i = 1;

while ($row = mysql_fetch_assoc($result))
{
$headline = $row['headline'];
$username1 = $row['username'];
$mainnews = $row['mainnews'];
$comments = $row['comments'];
$date = $row['date'];

if ($i % 2 == 0)
$bg = "#e1e1e1";
else
$bg = "#eeeeee";
$i ++;
echo "<tr><td bgcolor=".$bg.">";
echo "<font face='verdana' size=1>";
echo "<b>.:$headline:.</b> Posted By: $username1 - <i>$date</i><br><hr color='black'>";
echo "$mainnews<p>";
echo "<a href='changes/edit4.php'>Edit Post</a> ";
echo "</td></tr>";
}

It must have been 2 fetches at the same time. Thanks!
Post Reply