views:

102

answers:

5

I have the following code which redirect pages depends on $path.

...
$path = $this->uri->segment(3);
$pathid = $this->uri->segment(4);
if($path=='forsiden'){
            redirect('','refresh');
        }elseif($path =='contact'){
            redirect('welcome/kontakt','refresh');
        }elseif($path =='illustration'){
            $this->_gallery($path,$pathid);
        }elseif($path =='webdesign'){
            redirect('welcome/webdesign','refresh');    
        }elseif($path==('web_tjenester' || 'webdesigndetails' || 
                        'vismahjemmeside' || 'joomla' || 'vismanettbutikk' || 
                        'vpasp' || 'artportfolio')){
        ...
     CODE A   
     ...    

}else{
        ...
        CODE B
        ...
                }

I am not getting right results with

$path==('web_tjenester' || 'webdesigndetails' || 
'vismahjemmeside' || 'joomla' || 'vismanettbutikk' || 
'vpasp' || 'artportfolio')

contact, illustration, gallery and webdesign are redirected and working alright. However all other pages are added CODE A.

I am expecting CODE A only when $path is web_tjenester', 'webdesigndetails', 'vismahjemmeside', 'joomla', 'vismanettbutikk', 'vpasp' or 'artportfolio'.

Could anyone point out my mistake and correct me please?

Thanks in advance.

--UPDATE--

The following works, but is there any ways shorten the code?

I am repeating ($path==..).

elseif(($path=='web_tjenester') || ($path=='webdesigndetails') || 
    ($path=='vismahjemmeside') || ($path=='joomla') || ($path=='vismanettbutikk') || 
    ($path=='vpasp') || ($path=='artportfolio')){
+1  A: 
$path==('web_tjenester' || 'webdesigndetails' || 
'vismahjemmeside' || 'joomla' || 'vismanettbutikk' || 
'vpasp' || 'artportfolio')

This is not valid syntax, you would need to have the $path == in front of each string separated by ORs. The easier way to do it which looks nicer is to put all your string in an array and then use the in_array() function to check.

$array = array('web_tjenester', 'webdesigndetails', 
'vismahjemmeside', 'joomla', 'vismanettbutikk',
'vpasp', 'artportfolio');
if (in_array($path, $array)) {
    // do whatever
}
animuson
It *is* valid syntax.
Gumbo
Yes, it is valid syntax. However, it's obtuse and excessively complicated for what it actually does (aka useless) semantically.
wallyk
+5  A: 

The || operator is a logical operator. The semantics of the expression a || b is that it evaluates to true if either a or b evaluates to true, and false otherwise.

So it cannot be used in that way you intended it as the expression ('web_tjenester' || … || 'artportfolio') will be evaluated to a boolean value (to be specific: true as 'web_tjenester' evaluates to true using boolean conversion) that then will be compared to the value of $path.

If you want to test if a value is in a set of values, you can use an array and test the value with in_array:

in_array($path, array('web_tjenester', 'webdesigndetails', 'vismahjemmeside', 'joomla', 'vismanettbutikk', 'vpasp', 'artportfolio'))
Gumbo
A: 

You cannot do it the way you do it: You should compare everything with $path, so

$path=='web_tjenester' || $path=='webdesigndetails' || 
$path=='vismahjemmeside' || $path=='joomla' || $path=='vismanettbutikk' || 
$path=='vpasp' || $path=='artportfolio'
Excel20
+1  A: 

because $path == ('x' || 'y') means "evaluate 'x || y', and see if that equals $path. You'd need to do

if($path == 'x' || $path == 'y')

However you may want to consider a switch statement:

switch($patch) {
   case 'web_tjenester':
   case 'webdesigndetals':
          so_and_so();
          break; 
   case 'contact':
          redirect();
          break;
}

read up on switch before doing so , however :)

Erik
A: 

You can't do this

$path==('web_tjenester' || 'webdesigndetails' || 'vismahjemmeside' || 'joomla' || 'vismanettbutikk' ||'vpasp' || 'artportfolio')

It should be

$path=='web_tjenester' || $path=='webdesigndetails' || $path=='vismahjemmeside' || $path=='joomla' ...

You could set the paths you are using in an array and use [in_array()][1] for a cleaner code.

metrobalderas