Use the in_array() function to write you condition:
<?php if (in_array($mycat, array("animation", "direction", "grading")))
greg0ire
2010-07-16 16:52:21
Use the in_array() function to write you condition:
<?php if (in_array($mycat, array("animation", "direction", "grading")))
Path 1:
<?php if ($mycat == "animation" || $mycat == "direction" || $mycat == "grading"){
Path 2:
<?php if (in_array($mycat,array("animation","direction","grading"))) {
Create an array holding the values, then test if the value of $mycat
exists in the array. Also, you're echoing the link whatever $mycat
is, so the else statement can be completely dropped and the if structure further simplified to just the following:
$categories = array(
'animation',
'direction',
'grading',
'online',
'showcase',
'showreel',
'vfx'
);
if (in_array($mycat, $categories)) {
$_SESSION["theCategory"] = $mycat;
}
?>
<a href="<?php bloginfo('home')?>/category/<?php echo $_SESSION["theCategory"];?>"><?php echo $_SESSION["theCategory"];?></a>
And like others have said, remember to escape your output.
Something like this?
<?php
$cats = array(
'animation',
'direction',
'grading',
'online',
'showcase',
'showreel'
'vfx',
);
if ( in_array($mycat, $cats) ) :
$_SESSION['theCategory'] = $mycat; ?>
<a href="<?php bloginfo('home')?>/category/<?php echo $mycat;?>"><?php echo $mycat;?></a>
<?php else: ?>
<a href="<?php bloginfo('home')?>/category/<?php echo $_SESSION["theCategory"];?>"><?php echo $_SESSION["theCategory"];?></a>
<?php endif; ?>
I don't understand why you even need a conditional here. You're not changing anything inside each conditional possibility.
$_SESSION["theCategory"] = $mycat;
<a href="<?php bloginfo('home')?>/category/<?php echo $_SESSION["theCategory"];?>"><?php echo $_SESSION["theCategory"];?></a>
And using the switch
statement:
switch($mycat) {
case 'animation':
case 'direction':
case 'grading':
case 'online':
case 'showcase':
case 'showreel':
case 'vfx':
$_SESSION["theCategory"] = $mycat;
default:
?>
<a href="<?php bloginfo('home')?>/category/<?php echo $_SESSION["theCategory"];?>"><?php echo $_SESSION["theCategory"];?></a>
<?php
}
Note the fall-through after the case statements. You could either do that, or:
switch($mycat) {
case 'animation':
case 'direction':
case 'grading':
case 'online':
case 'showcase':
case 'showreel':
case 'vfx':
$_SESSION["theCategory"] = $mycat;
}
?>
<a href="<?php bloginfo('home')?>/category/<?php echo $_SESSION["theCategory"];?>"><?php echo $_SESSION["theCategory"];?></a>
<?php