Forum Moderators: coopster

Message Too Old, No Replies

php if then statements

if one statement is true then several others are true

         

Driven

3:17 am on Feb 25, 2005 (gmt 0)

10+ Year Member



Hello all,

I'm trying to define several different styles in my web page based on one variable in the query string. So if I have www.mysite.com/index.php?style=1 then I want table cells to be a certain color, links to be a certain color, etc. Obviously style=2 would be a different color scheme.

I have the following code but it doesn't seem to work.

<?php

$style = $_GET['style'];

if ( $style == 1 ) {
$border = 336699;
$bg = FFFFFF;
$links = 0000FF;
$url = 008000;
$text = 000000;
$footer = FFFFFF;
}

if ( $style == 2 ) {
$border = 000000;
$bg = F0F0F0;
$links = 0000FF;
$url = 008000;
$text = 000000;
$footer = FFFFFF;
}

?>

It produces "Parse error: parse error, unexpected T_STRING in /home/drivenin/public_html/partners/style.php on line 12"

Can anyone help me with this? Please let me know if you need furhter info.

Thanks,
Dan

jatar_k

3:35 am on Feb 25, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld Driven,

it's because you are missing your quotes

if ( $style == 1 ) {
$border = '336699';
$bg = 'FFFFFF';
$links = '0000FF';
$url = '008000';
$text = '000000';
$footer = 'FFFFFF';
}

if ( $style == 2 ) {
$border = '000000';
$bg = 'F0F0F0';
$links = '0000FF';
$url = '008000';
$text = '000000';
$footer = 'FFFFFF';

try that

ironik

3:39 am on Feb 25, 2005 (gmt 0)

10+ Year Member



The values you've assigned the variables aren't quoted, so the PHP parser is expecting a constant value (unless that value is actually a number).

Try:

$style = (int)$_GET['style'];

switch($style)
{
case 1:
$border = '#336699';
$bg = '#FFFFFF';
$links = '#0000FF';
$url = '#008000';
$text = '#000000';
$footer = '#FFFFFF';
break;

case 2:
$border = '#000000';
$bg = '#F0F0F0';
$links = '#0000FF';
$url = '#008000';
$text = '#000000';
$footer = '#FFFFFF';
break;
}

(I've also typecast the $_GET variable so it will only return an integer, and put your options into a switch statement, which will make it easier to read as you add more styles)

jatar_k

3:47 am on Feb 25, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



ironik was more thourough

here you can find more info about switch [php.net]

Driven

4:16 am on Feb 25, 2005 (gmt 0)

10+ Year Member



Thanks a ton for the help. It worked. Webmaster World rocks!