Page 1 of 1
Colour Problems...
Posted: Mon May 24, 2004 9:36 pm
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
Posted: Mon May 24, 2004 9:45 pm
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);
}
Re: Colour Problems...
Posted: Mon May 24, 2004 9:46 pm
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
Posted: Mon May 24, 2004 9:49 pm
by Joe
Thanks a bunch guys!
Posted: Mon May 24, 2004 10:29 pm
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!
Posted: Mon May 24, 2004 10:35 pm
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>";
}
?>
Posted: Mon May 24, 2004 10:40 pm
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

Posted: Mon May 24, 2004 10:45 pm
by feyd
probably has something to do with at least 2 fetches happening without reseting the sql pointers..
look into
mysql_data_seek()
Posted: Mon May 24, 2004 10:52 pm
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!