Change styling depending on var value
Change styling depending on var value
am 20.11.2009 20:45:28 von Phil Matt
I am trying to style HTML table cells depending on the values stored in
a MySQL db. Thanks to other on this list, I can now refer to the
variable that holds a CSS styling value.
Now, I need to apply different values of that variable, depending on the
content of the table cell itself.
I tried something like this, where $row[3] contains string values:
$entree ="meat";
$dessert ="ice cream";
$beverage="coffee";
(then the query that returns the data)
if ($row[3] = $entree)
{
$rowcolor = "color:red";
elseif ($row[3] = $dessert)
{
$rowcolor = "color:blue";
elseif ($row[3] = $beverage)
{
$rowcolor = "color:green";
}
else
{
$rowcolor ="color:black";
}
I think I'm not properly expressing that I want the values in the
$row[3] compared to those in the specified vars
Thanks again.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Change styling depending on var value
am 20.11.2009 20:55:04 von Ashley Sheridan
--=-RFRrXcNBziAgkVAPHD5Q
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
On Fri, 2009-11-20 at 14:45 -0500, Phil Matt wrote:
> I am trying to style HTML table cells depending on the values stored in
> a MySQL db. Thanks to other on this list, I can now refer to the
> variable that holds a CSS styling value.
>
> Now, I need to apply different values of that variable, depending on the
> content of the table cell itself.
>
> I tried something like this, where $row[3] contains string values:
>
> $entree ="meat";
> $dessert ="ice cream";
> $beverage="coffee";
>
> (then the query that returns the data)
>
> if ($row[3] = $entree)
> {
> $rowcolor = "color:red";
> elseif ($row[3] = $dessert)
> {
> $rowcolor = "color:blue";
> elseif ($row[3] = $beverage)
> {
> $rowcolor = "color:green";
> }
> else
> {
> $rowcolor ="color:black";
> }
>
> I think I'm not properly expressing that I want the values in the
> $row[3] compared to those in the specified vars
>
> Thanks again.
>
Well, you're main problem here is that you are only using a single =
character. What that is saying to PHP is: "if you let me assign the
value of $beverage to $row[3] then do this next bit", but what I think
you wanted it to say was "if $row[3] is the same as $beverage then do
this next bit", which would need == instead of =
Thanks,
Ash
http://www.ashleysheridan.co.uk
--=-RFRrXcNBziAgkVAPHD5Q--
Re: Change styling depending on var value
am 20.11.2009 21:16:55 von Ashley Sheridan
--=-a+lGZAvIaxSE2JvcAFCq
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
On Fri, 2009-11-20 at 15:11 -0500, Phil Matt wrote:
> Ashley Sheridan wrote:
>
> > Well, you're main problem here is that you are only using a single =
> > character. What that is saying to PHP is: "if you let me assign the
> > value of $beverage to $row[3] then do this next bit", but what I think
> > you wanted it to say was "if $row[3] is the same as $beverage then do
> > this next bit", which would need == instead of =
> >
>
> Thanks, Ashley. I don't do enough PHP to get used to the syntax.
>
> I changed the operators to ==, but my conditional apparently isn't
> working correctly. I checked to make sure the values in the cells were
> the strings as I've specified them, but the resultant formatting always
> defaults to the ELSE color.
>
> Must be something very simple, but I'm just not getting it.
>
> Cheers --- Phil
Don't forget to reply to all!
Just a quick question, what do you get if you do:
print $row[3];
Does it contain a string like you expect?
Thanks,
Ash
http://www.ashleysheridan.co.uk
--=-a+lGZAvIaxSE2JvcAFCq--
Re: Change styling depending on var value
am 20.11.2009 21:29:45 von Ashley Sheridan
--=-m4O3ccyHeMX4I+NpcQPL
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
On Fri, 2009-11-20 at 15:30 -0500, Phil Matt wrote:
> Ashley Sheridan wrote:
>
> > print $row[3];
> >
> > Does it contain a string like you expect?
> >
>
> Yes. I'm going to try the same kind of formatting on a different $row
> cell and see what happens.
>
> Cheers and thanks again --- Phil
Copying back the phplist *again*!
what does your code currently look like now, with the right == in?
Thanks,
Ash
http://www.ashleysheridan.co.uk
--=-m4O3ccyHeMX4I+NpcQPL--
Re: Change styling depending on var value
am 20.11.2009 21:32:51 von Dan McCullough
--0016e64cda8c657a100478d36215
Content-Type: text/plain; charset=ISO-8859-1
To add to what Ashley said about $row[3], remember that when you are
returning from the db the counter for fields will start at 0 not 1, so if
its the 3rd field that will be $row[2]. You might also want to do switch
rather then elseif but thats always a good debate.
On Fri, Nov 20, 2009 at 3:16 PM, Ashley Sheridan
wrote:
> On Fri, 2009-11-20 at 15:11 -0500, Phil Matt wrote:
>
> > Ashley Sheridan wrote:
> >
> > > Well, you're main problem here is that you are only using a single =
> > > character. What that is saying to PHP is: "if you let me assign the
> > > value of $beverage to $row[3] then do this next bit", but what I think
> > > you wanted it to say was "if $row[3] is the same as $beverage then do
> > > this next bit", which would need == instead of =
> > >
> >
> > Thanks, Ashley. I don't do enough PHP to get used to the syntax.
> >
> > I changed the operators to ==, but my conditional apparently isn't
> > working correctly. I checked to make sure the values in the cells were
> > the strings as I've specified them, but the resultant formatting always
> > defaults to the ELSE color.
> >
> > Must be something very simple, but I'm just not getting it.
> >
> > Cheers --- Phil
>
>
> Don't forget to reply to all!
>
> Just a quick question, what do you get if you do:
>
> print $row[3];
>
> Does it contain a string like you expect?
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>
>
--0016e64cda8c657a100478d36215--
Re: Change styling depending on var value
am 20.11.2009 21:52:39 von Phil Matt
Ashley Sheridan wrote:
> Copying back the phplist *again*!
Sorry. I'll remember to copy in...
>
> what does your code currently look like now, with the right == in?
$entree="meat";
$beverage="coffee";
if ($row[3] == $entree)
{
$newcolor="color:red";
}
elseif ($row[3] == $beverage)
{
$newcolor="color:red";
}
else
{
$newcolor="color:green";
The result is that all of the text in the styled cell is green.
'.$row[3].' |
I know the script is running through the conditional statement, because
if I change the ELSE value of $newcolor, all of the styled cells change
to whatever color I specify there.
I went back and browsed the db table to see to make sure I had the right
row and that the entries matched my references - no probs there.
Cheers --- Phil
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Change styling depending on var value
am 20.11.2009 21:53:46 von Ashley Sheridan
--=-8jQ/W+0t/mNVQ5z5wVf6
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
On Fri, 2009-11-20 at 15:52 -0500, Phil Matt wrote:
> Ashley Sheridan wrote:
>
> > Copying back the phplist *again*!
> Sorry. I'll remember to copy in...
> >
> > what does your code currently look like now, with the right == in?
>
> $entree="meat";
> $beverage="coffee";
> if ($row[3] == $entree)
> {
> $newcolor="color:red";
> }
> elseif ($row[3] == $beverage)
> {
> $newcolor="color:red";
> }
> else
> {
> $newcolor="color:green";
>
>
> The result is that all of the text in the styled cell is green.
>
>
'.$row[3].' |
>
> I know the script is running through the conditional statement, because
> if I change the ELSE value of $newcolor, all of the styled cells change
> to whatever color I specify there.
>
> I went back and browsed the db table to see to make sure I had the right
> row and that the entries matched my references - no probs there.
>
> Cheers --- Phil
>
copy the results of this:
vardump($row);
Thanks,
Ash
http://www.ashleysheridan.co.uk
--=-8jQ/W+0t/mNVQ5z5wVf6--
Re: Change styling depending on var value
am 20.11.2009 21:54:16 von Phil Matt
Dan McCullough wrote:
> To add to what Ashley said about $row[3], remember that when you are
> returning from the db the counter for fields will start at 0 not 1, so
> if its the 3rd field that will be $row[2]. You might also want to do
> switch rather then elseif but thats always a good debate.
>
Thanks, Dan. I had checked that a while ago.
I also tried the same script on a different row, changing the strings in
the variables to match the data in the fields - same result, always
defaults to the ELSE color.
Cheers --- Phil
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Change styling depending on var value
am 20.11.2009 22:15:38 von Ashley Sheridan
--=-luLlEfc7ir9OPHTdcoI2
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
On Fri, 2009-11-20 at 16:17 -0500, Phil Matt wrote:
> Ashley Sheridan wrote:
>
> > copy the results of this:
> >
> > vardump($row);
> >
> Sirloin Steak fresh Acme meat
> Chicken Breast frozen Acme meat
> Decaf Columbian pantry Giant coffee
> Ice Cream frozen Giant dessert
>
> All looks as expected.
> NB: This is just test data until I get the formatting right.
>
> Cheers --- Phil
That's not a vardump, a vardump would contain the type of variable. I
wanted to see the whole thing.
Thanks,
Ash
http://www.ashleysheridan.co.uk
--=-luLlEfc7ir9OPHTdcoI2--
Re: Change styling depending on var value
am 20.11.2009 22:17:41 von Phil Matt
Ashley Sheridan wrote:
> copy the results of this:
>
> vardump($row);
>
Sirloin Steak fresh Acme meat
Chicken Breast frozen Acme meat
Decaf Columbian pantry Giant coffee
Ice Cream frozen Giant dessert
All looks as expected.
NB: This is just test data until I get the formatting right.
Cheers --- Phil
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Change styling depending on var value
am 20.11.2009 22:37:11 von M.Ford
> -----Original Message-----
> From: Ashley Sheridan [mailto:ash@ashleysheridan.co.uk]
> Sent: 20 November 2009 21:16
> To: Phil Matt
> Cc: php-general@lists.php.net
> Subject: Re: [PHP] Change styling depending on var value
>=20
> On Fri, 2009-11-20 at 16:17 -0500, Phil Matt wrote:
>=20
> > Ashley Sheridan wrote:
> >
> > > copy the results of this:
> > >
> > > vardump($row);
> > >
> > Sirloin Steak fresh Acme meat
> > Chicken Breast frozen Acme meat
> > Decaf Columbian pantry Giant coffee
> > Ice Cream frozen Giant dessert
> >
> > All looks as expected.
> > NB: This is just test data until I get the formatting right.
> >
> > Cheers --- Phil
>=20
>=20
> That's not a vardump, a vardump would contain the type of variable.
> I
> wanted to see the whole thing.
>=20
Ash, you mean var_dump!
Phil, put var_dump($row); right before your if statement and report the res=
ult back to this list.
Cheers!
Mike
--=20
Mike Ford,
Electronic Information Developer, Libraries and Learning Innovation,
Leeds Metropolitan University, C507, Civic Quarter Campus,=20
Woodhouse Lane, LEEDS,=A0 LS1 3HE,=A0 United Kingdom=20
Email: m.ford@leedsmet.ac.uk=20
Tel: +44 113 812 4730
To view the terms under which this email is distributed, please go to http:=
//disclaimer.leedsmet.ac.uk/email.htm
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Change styling depending on var value
am 20.11.2009 22:41:20 von Phil Matt
Ashley Sheridan wrote:
> That's not a vardump, a vardump would contain the type of variable. I
> wanted to see the whole thing.
I played with this for a while and checked the PHP manual; not sure how
to use this.
Cheers --- Phil
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Change styling depending on var value
am 20.11.2009 22:42:22 von Ashley Sheridan
--=-LmHyeEtv2ooKz/dIFycV
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
On Fri, 2009-11-20 at 16:41 -0500, Phil Matt wrote:
> Ashley Sheridan wrote:
>
> > That's not a vardump, a vardump would contain the type of variable. I
> > wanted to see the whole thing.
>
> I played with this for a while and checked the PHP manual; not sure how
> to use this.
>
>
> Cheers --- Phil
As Mike pointed out, I meant var_dump(), sorry!
Thanks,
Ash
http://www.ashleysheridan.co.uk
--=-LmHyeEtv2ooKz/dIFycV--
Re: Change styling depending on var value
am 20.11.2009 23:07:16 von Ashley Sheridan
--=-biSLVVSiBVNZcRf49TXS
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
On Fri, 2009-11-20 at 17:08 -0500, Phil Matt wrote:
> Ashley Sheridan wrote:
> >
> > As Mike pointed out, I meant var_dump(), sorry!
> >
>
> Is the idea to put the variable in question within the parentheses?
>
> I tried the statement,
>
> var_dump($row[3];
>
> And I got in the output:
>
> NULL
>
> Also tried
>
> var_dump();
>
> And I got in the output:
>
> Warning: Wrong parameter count for var_dump() in...
>
> I reread the PHP manual for var_dump. I still don't see where it goes in
> the PHP script or how to implement it.
>
> Cheers --- Phil
put
var_dump($row);
Thanks,
Ash
http://www.ashleysheridan.co.uk
--=-biSLVVSiBVNZcRf49TXS--
Re: Change styling depending on var value
am 20.11.2009 23:08:07 von Phil Matt
Ashley Sheridan wrote:
>
> As Mike pointed out, I meant var_dump(), sorry!
>
Is the idea to put the variable in question within the parentheses?
I tried the statement,
var_dump($row[3];
And I got in the output:
NULL
Also tried
var_dump();
And I got in the output:
Warning: Wrong parameter count for var_dump() in...
I reread the PHP manual for var_dump. I still don't see where it goes in
the PHP script or how to implement it.
Cheers --- Phil
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Change styling depending on var value
am 20.11.2009 23:23:32 von Phil Matt
Ashley Sheridan wrote:
> put
>
> var_dump($row);
>
I inserted this line in the script at the end of the html table, still
inside the PHP echo statement.
This yields:
bool(false)
Cheers --- Phil
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Change styling depending on var value
am 20.11.2009 23:29:13 von Ashley Sheridan
--=-udKfQsl/PkkniO71ZZGX
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
On Fri, 2009-11-20 at 17:23 -0500, Phil Matt wrote:
> Ashley Sheridan wrote:
>
> > put
> >
> > var_dump($row);
> >
> I inserted this line in the script at the end of the html table, still
> inside the PHP echo statement.
>
> This yields:
>
> bool(false)
>
> Cheers --- Phil
That means that $row doesn't contain what you thought it did. It
contains the boolean value false (which is different from the string
'false') Are you sure you spelt the varialbe correctly in all of your
code?
Thanks,
Ash
http://www.ashleysheridan.co.uk
--=-udKfQsl/PkkniO71ZZGX--
Re: Change styling depending on var value
am 20.11.2009 23:49:40 von Mari Masuda
On Nov 20, 2009, at 2:29 PM, Ashley Sheridan wrote:
> On Fri, 2009-11-20 at 17:23 -0500, Phil Matt wrote:
>=20
>> Ashley Sheridan wrote:
>>=20
>>> put
>>>=20
>>> var_dump($row);
>>>=20
>> I inserted this line in the script at the end of the html table, =
still=20
>> inside the PHP echo statement.
>>=20
>> This yields:
>>=20
>> bool(false)
>>=20
>> Cheers --- Phil
>=20
>=20
> That means that $row doesn't contain what you thought it did. It
> contains the boolean value false (which is different from the string
> 'false') Are you sure you spelt the varialbe correctly in all of your
> code?
This may be a dumb question, but did you actually fetch the db query's =
results and put them in $row before trying to use $row? In MySQL you =
could do something like:
$query =3D "select * from my_table";
$result =3D mysql_query($query);
$row =3D mysql_fetch_array($result); //this statement would need to be =
inside of a loop if there is more than one result
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Change styling depending on var value
am 23.11.2009 15:41:03 von Phil Matt
Mari Masuda wrote:
> This may be a dumb question, but did you actually fetch the db query's results and put them in $row before trying to use $row? In MySQL you could do something like:
>
> $query = "select * from my_table";
> $result = mysql_query($query);
> $row = mysql_fetch_array($result); //this statement would need to be inside of a loop if there is more than one result
>
Thanks, Mari. That was it: I usually declare the variables first, then
on with the rest of the code. I had inserted the $row conditional values
right after the vars, before the rest of the query...just plain dumb.
Cheers --- Phil
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php