Two alternate row colours, help needed to make a third

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

User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Two alternate row colours, help needed to make a third

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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";
}
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post 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.
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post by mhouldridge »

Yes,

Thanks for that guys.
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post 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";
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Code: Select all

$stopped = $row['stopped']; //or whatever your field is called
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post by mhouldridge »

I get;

Parse error: parse error, unexpected $end in F:\Auditwebsite\view.php on line 95
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

you haven't closed your "for" loop
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post 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.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

put a closing curly brace before the comment

Code: Select all

//now let's close the table and be done with it
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post 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.
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post 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
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

nope, you stil haven't closed it

Code: Select all

echo "</TABLE>\n";
to

Code: Select all

}
echo "</TABLE>\n";
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post 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?
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post 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>
Post Reply