...

View Full Version : Simple if () conditional using strings



scapenz
11-19-2010, 03:15 AM
Hey guys,

Kinda new to this php lark and wasn't able to find anywhere that could explain why this is not working correctly -

Basically i'm working on a menu that changes the UL background according to which section the page is currently on.

I have a template index.php page that changes according to index.php?page=pagename

What I have in the menu is -



<?php
$pagename = $_GET['page'];
function setcurrent($name) {
echo "<ul class='section ".$name." current'>";
}
if ($pagename == 'whybamboo' || 'environmentallysound' || 'ourclients') {
setcurrent(whybamboo);
}
else
{
echo "<ul class='section whybamboo'>";
}
?>


Can anyone explain to me why the || operators are not working and if there is a simpler way of achieving this.

I also have a problem where i'd like the title of the page to change according to the ?page=pagename variable also. Now while I can appreciate that this could be done with an array or switch conditional, what is the most common way that dynamic titles on web pages is achieved?

Cheers.

firepages
11-19-2010, 03:40 AM
if ($pagename=='whybamboo' || $pagename=='environmentallysound' || $pagename=='ourclients') {
setcurrent($pagename); //or setcurrent('whybamboo');//see below
}


I am not sure if the above is actually doing what you want it to do , are you saying IF whybamboo OR environmentally sound OR ourclients then regardless send to 'whybamboo' , or do you want to setname() whatever the $_GET['page'] is ?

if so an array of allowable names might be a plan..


$okpages = array('whybamboo','evironmentallysound','ourclients','blah');
if(in_array($_GET['page'],$ok_pages)){
setcurrent($_GET['page']);
$htmltitle = $_GET['page'];
}else{
echo 'invalid page selection';
$htmltitle='Error';
}

<html title="<?=$htmltitle;?>">

hope I am on the right track here ?

scapenz
11-19-2010, 04:06 AM
First solution worked great! Simple conditional error.

I didn't think there was any difference between them but yea:




if ($variable == 'a' || 'b' || 'c')
{

}



and





if ($variable == 'a' || $variable == 'b' || $variable == 'c')
{

}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum