/    Sign up×
Community /Pin to ProfileBookmark

How to change row background color?

Hi Folks,

Am wondering if its possible to set a row’s background color to gray. I only want to gray out a specific row if it has the value 1 in $visible column but am having a tough time figuring it out! Please help my brain hurts! ๐Ÿ˜ฎ

to post a comment
PHP

17 Comments(s) โ†ด

Copy linkTweet thisAlerts:
@rbudjJan 03.2007 โ€”ย It's been a while but lets see...

First, create your external stylesheet and link to it in your header.

Stylesheet.css

.gray_background

{

background-color:#CCCCCC;

}

Now use an if() statement to make it work:

<table>

<tr>

<?php

if ($visible == '1') {

print "<td class="gray_backgroung">;

} else {

print "<td>;

}

?>

</tr>

</table>

If this does not work completely, maybe it will give you some ideas. This should be pretty good though. If you don't want to use a stylesheet you could do this:

<?php

if ($visible == '1') {

print "<td bgcolor="#CCCCCC">;

} else {

print "<td>;

}

?>

</tr>

</table>
Copy linkTweet thisAlerts:
@MistaTauthorJan 03.2007 โ€”ย Cheers for the suggestion but i tried the bit of code without a stylesheet and its just giving me a parse error ?
Copy linkTweet thisAlerts:
@Stephen_PhilbinJan 03.2007 โ€”ย [code=php]<?php
if ($visible == '1') {
print "<td class="gray_background">";
} else {
print "<td>";
}
?>[/code]


[code=php]<?php
if ($visible == '1') {
print "<td bgcolor="#CCCCCC">";
} else {
print "<td>";
}
?>[/code]


Try that. It's just what rbudj gave you, but (s)he kept missing the last " to end each string, so I just added it and corrected a small typo in a class name. Should work fine then.
Copy linkTweet thisAlerts:
@chickenlandJan 03.2007 โ€”ย there was a couple of typos - (missing ") try below :

[code=php]<table>
<tr>

<?php
if ($visible == '1') {
print "<td class="gray_backgroung">";
} else {
print "<td>";
}
?>
</tr>
</table>

If this does not work completely, maybe it will give you some ideas. This should be pretty good though. If you don't want to use a stylesheet you could do this:

<?php
if ($visible == '1') {
print "<td bgcolor="#CCCCCC">";
} else {
print "<td>";
}
?>
</tr>
</table>[/code]
Copy linkTweet thisAlerts:
@MistaTauthorJan 03.2007 โ€”ย its worked (in a way) its added another column onto the end of my table, the gray is at the correct row but its not filling in the entire row with gray??
Copy linkTweet thisAlerts:
@NightShift58Jan 03.2007 โ€”ย If you want the row to be grey, you either have to apply this logic to every <TD> in every row or - better - apply it to every <TR> in your table.[code=php]print "<tr" . $visible==1?" bgcolor='#CCCCCC'":"" . ">";[/code]
Copy linkTweet thisAlerts:
@Stephen_PhilbinJan 03.2007 โ€”ย Oh you want the row. Right. Well in that case just change the table cell to a row. Just change each instance of print "&lt;td to print "&lt;tr.
Copy linkTweet thisAlerts:
@MistaTauthorJan 03.2007 โ€”ย Ok probably best i post my code so you can see what i'm trying to do, its not the complete code but it will give you an idea of what i need to do!

[CODE]
$querypd = "select count(*)
from blwdq.SERVICEREQ srv,blwdq.sms_out_audit sms
where (srv.pkey = sms.contact_id(+))
and (srv.QUICKCALL_KEY='39748')
and (TO_CHAR(srv.DATEMODIFIED, 'dd/mm/yyyy')= '$datesearch')
ORDER BY to_char(srv.DATEMODIFIED,'dd/mm/yyyy hh24:mi') DESC, sms.sms_id";

ora_parse($curspd, $querypd);
ora_exec($curspd);
while (ora_fetch($curspd))
{
$count = ora_getcolumn($curspd, 0);

#echo $count . " comments for today";
}

ora_close($curspd);
ora_logoff($conncount);

#################################################################################




#and (srv.type = 3 or (srv.type = 8 and sms.visible = 0))


$querypd = "select srv.PKEY, srv.CONTACT_KEY, srv.DESCRIPTION, srv.NOTES, srv.C0500,srv.TYPE,

TO_CHAR(srv.DATEMODIFIED, 'HH24:MI'), srv.NOTES, sms.visible,sms.sms_id
from blwdq.SERVICEREQ srv,blwdq.sms_out_audit sms
where (srv.pkey = sms.contact_id(+))
and (srv.QUICKCALL_KEY='39748')
and (TO_CHAR(srv.DATEMODIFIED, 'dd/mm/yyyy')= '$datesearch')
ORDER BY to_char(srv.DATEMODIFIED,'dd/mm/yyyy hh24:mi') DESC, sms.sms_id";
#$query = sprintf("select * from actor");

#echo $querypd;

ora_parse($curspd, $querypd);
ora_exec($curspd);

echo "<body bgcolor="#FFFFFF"><font face="arial">";
echo "<center><img src="banner.png"><br><BR>";
echo "<center><b>($count) Questions/Comments - for " . $datesearch . "</b><P>";
echo "<table border=1 bordercolor=#000000 " width="100%" cellpadding="4" cellspacing="0">";

#table top row

echo "
<tr><td bgcolor="#C9B360"><B><font color="#000000"><center>Time</td>
<td bgcolor="#C9B360"><B><font color="#000000"><center>Questions/Comments</td>
<td bgcolor="#C9B360"><B><font color="#000000"><center>Contact</td>
<td bgcolor="#C9B360"><B><font color="#000000"><center>Channel</td>
<td bgcolor="#C9B360"><B><font color="#000000"><center>Hide</td></tr>";



while (ora_fetch($curspd))
{
$contactid = ora_getcolumn($curspd, 0);
$contact_key = ora_getcolumn($curspd, 1);
$description = ora_getcolumn($curspd, 2);
$c0500 = ora_getcolumn($curspd, 4);
$datestamp = ora_getcolumn($curspd, 6);
$notes = ora_getcolumn($curspd, 7);
$type = ora_getcolumn($curspd,5);
$visible = ora_getcolumn($curspd,8);
$smsid = ora_getcolumn($curspd,9);



if ($type=='3'){
$channel="Phone";
}

elseif ($type=='0'){
$channel="E-Mail";
}

elseif ($type=='8'){
$channel="SMS";
}

if ($visible == '1') {
print "<tr bgcolor="#CCCCCC">";
} else {
print "<tr>";
}








#######################################################################
#Subsearch for phone no.

#$title = "";
#$fullname = "";
$phone = "";

$curs2 = ora_open($conn);
$query2 = "select PHONE from CONTACT where PKEY = '$contact_key'";
ora_parse($curs2, $query2);
ora_exec($curs2);

while (ora_fetch($curs2))
{
#$title = ora_getcolumn($curs2, 0);
#$fullname = ora_getcolumn($curs2, 1);
$phone = ora_getcolumn($curs2, 0);

if ($phone==""){
$phone = "No number";
}

}

ora_close($curs2);
######################################################################







#layout...

echo "<TR><TD>";
echo $datestamp . "</TD><TD>";
echo $description . "</TD><TD>";
#echo "<a href="fulldetails.php?contact_key=$contact_key&notes=$notes&contactid=$contactid">" . $phone

."</a></TD>";
echo $phone . "</a></TD><TD>";
echo $channel . "</TD><TD>";
echo "<a href="hideid.php?id=$contactid.vis=$visible">Hide</a></TD>";




}

echo "</table>";



echo "</CENTER><BR><map name="FPMap0"><area href="index.php" shape="rect" coords="0, 4, 33, 33"></map>
<img border="0" src="CosmicGelLb1.gif" usemap="#FPMap0" width="34" height="34" alt="Home">";


ora_close($curspd);
ora_logoff($conn);
#### End of Excellent Comments[/CODE]
Copy linkTweet thisAlerts:
@NightShift58Jan 03.2007 โ€”ย This:[code=php]if ($visible == '1') {
print "<tr bgcolor="#CCCCCC">";
} else {
print "<tr>";
}[/code]
and this[code=php]print "<tr" . $visible==1?" bgcolor='#CCCCCC'":"" . ">";[/code]will do the same thing for you.

Looking at your code, though, I think that you haven't found the right spot to insert this code. You should use it as a replacement for the current <TR>, which - I think - is this one:[code=php]#layout...

echo "<TR><TD>";[/code]
After the change, it would look like this:[code=php]#layout...

echo "<TR" . $visible==1?" bgcolor='#CCCCCC'":"" . "><TD>";[/code]
Copy linkTweet thisAlerts:
@MistaTauthorJan 03.2007 โ€”ย thats superb its grayed out the correct row but now i have the following sitting above my table

>
>
>
>With gray line
>
>

any ideas?
Copy linkTweet thisAlerts:
@NightShift58Jan 03.2007 โ€”ย Did you remove this code?[code=php]if ($visible == '1') {
print "<tr bgcolor="#CCCCCC">";
} else {
print "<tr>";
}[/code]
Copy linkTweet thisAlerts:
@MistaTauthorJan 03.2007 โ€”ย no cos when i took it out the whole layout went all over the place

This is what i've got

[CODE]while (ora_fetch($curspd))
{
$contactid = ora_getcolumn($curspd, 0);
$contact_key = ora_getcolumn($curspd, 1);
$description = ora_getcolumn($curspd, 2);
$c0500 = ora_getcolumn($curspd, 4);
$datestamp = ora_getcolumn($curspd, 6);
$notes = ora_getcolumn($curspd, 7);
$type = ora_getcolumn($curspd,5);
$visible = ora_getcolumn($curspd,8);
$smsid = ora_getcolumn($curspd,9);



if ($type=='3'){
$channel="Phone";
}

elseif ($type=='0'){
$channel="E-Mail";
}

elseif ($type=='8'){
$channel="SMS";
}

if ($visible == '1') {
print "<tr bgcolor="#CCCCCC">";
} else {
print "<tr>";
}








#######################################################################
#Subsearch for phone no.

#$title = "";
#$fullname = "";
$phone = "";

$curs2 = ora_open($conn);
$query2 = "select PHONE from CONTACT where PKEY = '$contact_key'";
ora_parse($curs2, $query2);
ora_exec($curs2);

while (ora_fetch($curs2))
{
#$title = ora_getcolumn($curs2, 0);
#$fullname = ora_getcolumn($curs2, 1);
$phone = ora_getcolumn($curs2, 0);

if ($phone==""){
$phone = "No number";
}

}

ora_close($curs2);
######################################################################







#layout...

echo "<TR" . $visible==1?" bgcolor='#CCCCCC'":"" . "><TD>";
echo $datestamp . "</TD><TD>";
echo $description . "</TD><TD>";
#echo "<a href="fulldetails.php?contact_key=$contact_key&notes=$notes&contactid=$contactid">" . $phone

."</a></TD>";
echo $phone . "</a></TD><TD>";
echo $channel . "</TD><TD>";
echo "<a href="hideid.php?id=$contactid.vis=$visible">Hide</a></TD>";




}

echo "</table>";[/CODE]
Copy linkTweet thisAlerts:
@NightShift58Jan 03.2007 โ€”ย See previous post... lines crossed over... ?
Copy linkTweet thisAlerts:
@MistaTauthorJan 03.2007 โ€”ย thanks for your help its much appreciated, i still cant figure out why its producing the lines and shading above though but will keep trying!!
Copy linkTweet thisAlerts:
@NightShift58Jan 03.2007 โ€”ย (1) You need to remove this code?[code=php]if ($visible == '1') {
print "<tr bgcolor="#CCCCCC">";
} else {
print "<tr>";
}[/code]
and you need to add a closing </TR>, which is now missing.
Copy linkTweet thisAlerts:
@MistaTauthorJan 03.2007 โ€”ย sorry mate i'm having a brain drain

when i remove

[code=php]if ($visible == '1') {
print "<tr bgcolor="#CCCCCC">";
} else {
print "<tr>";
} [/code]


it then doesnt highlight any rows in gray and i'm left with lots of > at the top of the page, could you amend my code with the changes?
Copy linkTweet thisAlerts:
@NightShift58Jan 03.2007 โ€”ย Try this:[code=php]<?php
$querypd = "select count(*)
from blwdq.SERVICEREQ srv,blwdq.sms_out_audit sms
where (srv.pkey = sms.contact_id(+))
and (srv.QUICKCALL_KEY='39748')
and (TO_CHAR(srv.DATEMODIFIED, 'dd/mm/yyyy')= '$datesearch')
ORDER BY to_char(srv.DATEMODIFIED,'dd/mm/yyyy hh24:mi') DESC, sms.sms_id";

ora_parse($curspd, $querypd);
ora_exec($curspd);
while (ora_fetch($curspd)) {
$count = ora_getcolumn($curspd, 0);
#echo $count . " comments for today";
}

ora_close($curspd);
ora_logoff($conncount);

#################################################################################
#and (srv.type = 3 or (srv.type = 8 and sms.visible = 0))

$querypd = "select srv.PKEY, srv.CONTACT_KEY, srv.DESCRIPTION, srv.NOTES, srv.C0500,srv.TYPE,
TO_CHAR(srv.DATEMODIFIED, 'HH24:MI'), srv.NOTES, sms.visible,sms.sms_id
from blwdq.SERVICEREQ srv,blwdq.sms_out_audit sms
where (srv.pkey = sms.contact_id(+))
and (srv.QUICKCALL_KEY='39748')
and (TO_CHAR(srv.DATEMODIFIED, 'dd/mm/yyyy')= '$datesearch')
ORDER BY to_char(srv.DATEMODIFIED,'dd/mm/yyyy hh24:mi') DESC, sms.sms_id";

#$query = sprintf("select * from actor");
#echo $querypd;

ora_parse($curspd, $querypd);
ora_exec($curspd);

echo "<body bgcolor='#FFFFFF'><font face='arial'>";
echo "<center><img src='banner.png'><br><BR>";
echo "<center><b>($count) Questions/Comments - for $datesearch </b><P>";
echo "<table border='1' bordercolor='#000000' width='100%' cellpadding='4' cellspacing='0'>";

#table top row

echo "
<tr><td bgcolor='#C9B360'><B><font color='#000000'><center>Time</td>
<td bgcolor='#C9B360'><B><font color='#000000'><center>Questions/Comments</td>
<td bgcolor='#C9B360'><B><font color='#000000'><center>Contact</td>
<td bgcolor='#C9B360'><B><font color='#000000'><center>Channel</td>
<td bgcolor='#C9B360'><B><font color='#000000'><center>Hide</td></tr>";



while (ora_fetch($curspd)) {
$contactid = ora_getcolumn($curspd, 0);
$contact_key = ora_getcolumn($curspd, 1);
$description = ora_getcolumn($curspd, 2);
$c0500 = ora_getcolumn($curspd, 4);
$datestamp = ora_getcolumn($curspd, 6);
$notes = ora_getcolumn($curspd, 7);
$type = ora_getcolumn($curspd,5);
$visible = ora_getcolumn($curspd,8);
$smsid = ora_getcolumn($curspd,9);

if ($type=='3'){
$channel="Phone";
} elseif ($type=='0'){
$channel="E-Mail";
} elseif ($type=='8'){
$channel="SMS";
}

#######################################################################
#Subsearch for phone no.

#$title = "";
#$fullname = "";
$phone = "";

$curs2 = ora_open($conn);
$query2 = "select PHONE from CONTACT where PKEY = '$contact_key'";
ora_parse($curs2, $query2);
ora_exec($curs2);

while (ora_fetch($curs2)) {
#$title = ora_getcolumn($curs2, 0);
#$fullname = ora_getcolumn($curs2, 1);
$phone = ora_getcolumn($curs2, 0);

if ($phone=="") {
$phone = "No number";
}

}

ora_close($curs2);
######################################################################

#layout...

echo "<TR" . ($visible==1 ? " bgcolor='#CCCCCC'" : "") . ">";
echo "<TD>$datestamp</TD>";
echo "<TD>$description</TD>

#echo "<TD><a href='fulldetails.php?contact_key=$contact_key&notes=$notes&contactid=$contactid'>$phone</a></TD>";
echo "<TD>$channel</TD>";
echo "<TD><a href='hideid.php?id=$contactid.vis=$visible'>Hide</a></TD>";
echo "</TR>";
}

echo "</table>";

echo "</CENTER><BR><map name='FPMap0'><area href='index.php' shape='rect' coords='0, 4, 33, 33'></map>";
echo "<img border='0' src='CosmicGelLb1.gif' usemap='#FPMap0' width='34' height='34' alt='Home'>";


ora_close($curspd);
ora_logoff($conn);
#### End of Excellent Comments
?>[/code]
You had at least one mixup in there with [b]"[/b] and the missing [b]</TR>[/b] I mentioned earlier.
ร—

Success!

Help @MistaT spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 โ€” BETA 5.4,
whats_new: community page,
up_next: more Davinciโ€ข003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinciโ€ข003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...