Page 1 of 2

Two alternate row colours, help needed to make a third

Posted: Tue May 31, 2005 3:35 am
by mhouldridge
Hi,

The code below outputs a table with two alternate row colours;

Code: Select all

for($i = 0; $i < $numofrows; $i++) {
    $row = mysql_fetch_array($result); //get a row from our result set
    if($i % 2) {         echo "<TR bgcolor=\"#ffff99\">\n";
    } else { 
        echo "<TR bgcolor=\"#ffffcc\">\n";
    }
    echo "<TD><font color=\"red\"><b><a href='viewall.php?varl=".$row['asset']."'>".$row['asset']."</a></b></font></TD><TD><font size=\"2\">".$row['title']."</TD><TD><font size=\"2\">".$row['customer']."</TD><TD><font size=\"2\">".$row['type']."</TD><TD><font size=\"2\">".$row['IP']."</TD>\n"; 
    echo "</TR>\n";
}
echo "</TABLE>\n";
I need to add a third colur (red) which will display if the field for a row in a field reads "yes".

In this case the current table simply outputs asset information into a table and lists all of the assets. I would like it so that assets which have been decomissioned show a background colour of red. This is determined if a value = yes.

Posted: Tue May 31, 2005 4:08 am
by timvw
if($i % 2)
{
echo "<TR bgcolor=\"#ffff99\">\n";
}
else
{
echo "<TR bgcolor=\"#ffffcc\">\n";
}
Let's explain this piece of code.

- if the rest of $i divided by 2 equals 0, then color it #ffff99.
- else color it ffffcc

so now you can write it like this:

Code: Select all

if ($i % 3 == 0)
{
  echo "ffff99";
}
else if ($i % 3 == 1)
{
  echo "ffffc";
}
else
{
  echo "newcolor";
}

Posted: Tue May 31, 2005 4:29 am
by phpScott

Code: Select all

if($someValue =='yes')
{         
  echo "<TR bgcolor=\"#redcolour\">\n";
}
else if($i % 2) {         echo "<TR bgcolor=\"#ffff99\">\n";
    } else { 
        echo "<TR bgcolor=\"#ffffcc\">\n";
    }
check to see if $someValue is equal to yes if it is then set the bgcolor. If it isn't then do your other rotating color check.

Posted: Tue May 31, 2005 4:39 am
by mhouldridge
Yes,

Thanks for that guys.

Posted: Tue May 31, 2005 6:47 am
by mhouldridge
Hi,

I am not sure how to incorporate the $stopped part in to my query.

Please could you help

Code: Select all

mysql_select_db("audit") or die("Problem selecting database");
$query = "SELECT * FROM dedicated order by type";
$result = mysql_query($query) or die ("Query failed");
//let's get the number of rows in our result so we can use it in a for loop
$numofrows = mysql_num_rows($result);
echo "<TABLE BORDER=\"0\" table width=\"100%\">\n";
echo "<TR bgcolor=\"#cccccc\"><TD font colour=\"red\"><b>Asset</b></TD><TD><b>Title</b></TD><TD><b>Customer</b></TD><TD><b>Type</b></TD><TD><b>IP address</b></TD></TR>\n";
for($i = 0; $i < $numofrows; $i++) {
    $row = mysql_fetch_array($result); //get a row from our result set
	$stopped = mysql_fetch_array($result);
	if($stopped =='yes'){
		echo "<TR bgcolor=\"#ffcccc\">\n";
		}
	else if($i % 2) {
		echo "<TR bgcolor=\"#ffff99\">\n";    
		} 
	else {         
		echo "<TR bgcolor=\"#ffffcc\">\n";    
		}

//now let's close the table and be done with it 
echo "</TABLE>\n";

Posted: Tue May 31, 2005 6:52 am
by JayBird

Code: Select all

$stopped = $row['stopped']; //or whatever your field is called

Posted: Tue May 31, 2005 7:43 am
by mhouldridge
I get;

Parse error: parse error, unexpected $end in F:\Auditwebsite\view.php on line 95

Posted: Tue May 31, 2005 8:01 am
by JayBird
you haven't closed your "for" loop

Posted: Tue May 31, 2005 8:07 am
by mhouldridge
This is what I have;

Code: Select all

[b]$query = "SELECT * FROM dedicated order by type";
$result = mysql_query($query) or die ("Query failed");
//let's get the number of rows in our result so we can use it in a for loop
$numofrows = mysql_num_rows($result);
echo "<TABLE BORDER=\"0\" table width=\"100%\">\n";
echo "<TR bgcolor=\"#cccccc\"><TD font colour=\"red\"><b>Asset</b></TD><TD><b>Title</b></TD><TD><b>Customer</b></TD><TD><b>Type</b></TD><TD><b>IP address</b></TD></TR>\n";
for($i = 0; $i < $numofrows; $i++) {
    $row = mysql_fetch_array($result); //get a row from our result set
	$stopped = $row['stopped'];
	}
	if($stopped =='yes'){
		echo "<TR bgcolor=\"#ffcccc\">\n";
		}
	else if($i % 2) {
		echo "<TR bgcolor=\"#ffff99\">\n";    
		} 
	else {         
		echo "<TR bgcolor=\"#ffffcc\">\n";    
		}

//now let's close the table and be done with it 
echo "</TABLE>\n";
?>[/b]
This is simply not outputting anything. I believe its because I have closed the for loop in an incorrect space. Please could you advise.

Posted: Tue May 31, 2005 8:09 am
by JayBird
put a closing curly brace before the comment

Code: Select all

//now let's close the table and be done with it

Posted: Tue May 31, 2005 8:10 am
by phpScott
this closing bracket

Code: Select all

$stopped = $row['stopped'];
    }
should be here

Code: Select all

}
echo "</TABLE>\n";
of course in the code you have posted you are missing closing tr tags, as well as anything that would be in the td tags.

Posted: Tue May 31, 2005 8:23 am
by mhouldridge

Code: Select all

for($i = 0; $i < $numofrows; $i++) {
    $row = mysql_fetch_array($result); //get a row from our result set
	$stopped = $row['stopped'];
		if($stopped =='yes'){
		echo "<TR bgcolor=\"#ffcccc\">\n";
		}
	else if($i % 2) {
		echo "<TR bgcolor=\"#ffff99\">\n";    
		} 
	else {         
		echo "<TR bgcolor=\"#ffffcc\">\n";    
		}

echo "</TABLE>\n";
?>
As you can see I have closed the for loop. Still getting that error. Please help

Posted: Tue May 31, 2005 8:25 am
by JayBird
nope, you stil haven't closed it

Code: Select all

echo "</TABLE>\n";
to

Code: Select all

}
echo "</TABLE>\n";

Posted: Tue May 31, 2005 8:29 am
by mhouldridge
hmm...

It now displays the page, however the table is not shown, just whitespace (the whitespace is only small, and not the full size of before).

Any ideas?

Posted: Tue May 31, 2005 8:30 am
by mhouldridge
Ah,

Just checked the source and can see the the table rows have been created but nothing has been shown...

Code: Select all

<TR bgcolor=&quote;#ffffcc&quote;>
<TR bgcolor=&quote;#ffff99&quote;>
<TR bgcolor=&quote;#ffffcc&quote;>
<TR bgcolor=&quote;#ffff99&quote;>
<TR bgcolor=&quote;#ffffcc&quote;>
<TR bgcolor=&quote;#ffff99&quote;>
<TR bgcolor=&quote;#ffffcc&quote;>
<TR bgcolor=&quote;#ffff99&quote;>
<TR bgcolor=&quote;#ffffcc&quote;>
<TR bgcolor=&quote;#ffff99&quote;>
<TR bgcolor=&quote;#ffffcc&quote;>
<TR bgcolor=&quote;#ffff99&quote;>
<TR bgcolor=&quote;#ffffcc&quote;>
<TR bgcolor=&quote;#ffff99&quote;>
<TR bgcolor=&quote;#ffffcc&quote;>
<TR bgcolor=&quote;#ffff99&quote;>
<TR bgcolor=&quote;#ffffcc&quote;>
<TR bgcolor=&quote;#ffff99&quote;>
<TR bgcolor=&quote;#ffffcc&quote;>
<TR bgcolor=&quote;#ffff99&quote;>
<TR bgcolor=&quote;#ffffcc&quote;>
<TR bgcolor=&quote;#ffff99&quote;>
<TR bgcolor=&quote;#ffffcc&quote;>
<TR bgcolor=&quote;#ffff99&quote;>
<TR bgcolor=&quote;#ffffcc&quote;>
<TR bgcolor=&quote;#ffff99&quote;>
<TR bgcolor=&quote;#ffffcc&quote;>
<TR bgcolor=&quote;#ffff99&quote;>
<TR bgcolor=&quote;#ffffcc&quote;>
<TR bgcolor=&quote;#ffff99&quote;>
<TR bgcolor=&quote;#ffffcc&quote;>
<TR bgcolor=&quote;#ffff99&quote;>
</TABLE>