Convertir des elseif en switch
Posté : 30 déc. 2008, 23:58
Bonsoir,
J'ai écrit une suite d'instructions avec des elseif. C'est un peu bêta, et j'aimerai arriver à écrire un switch sur cette base; Les elseif fonctionnent à merveille, mais pas le switch, d'où ma question.
Voici le code avec mes elseif :
Voici le code correspondant que j'essaye d'utiliser avec la condition switch :
Merci de votre aide et de votre disponibilité,
Cordialement,
P.L.
J'ai écrit une suite d'instructions avec des elseif. C'est un peu bêta, et j'aimerai arriver à écrire un switch sur cette base; Les elseif fonctionnent à merveille, mais pas le switch, d'où ma question.
Voici le code avec mes elseif :
<?php if (($image_location !== '') && ($caption_text !== '') && ($slimbox_location !== '')) { ?>
<div id="cas-1">
<a
title="<?php if($slimbox_title !== '') { echo $slimbox_title; } ?>"
rel="lightbox"
href="<?php echo $slimbox_location; ?>"
>
<img src="<?php echo $image_location; ?>"
class="<?php if($caption_class !== '') { echo $caption_class; } else { echo "wp-caption aligncenter"; } ?>"
alt="<?php if($image_alt !== '') { echo $image_alt; } ?>"
title="<?php if($image_title !== '') { echo $image_title; } else { echo $image_alt; } ?>"
width="<?php if($image_width !== '') { echo $image_width; } ?>"
height="<?php if($image_height !== '') { echo $image_height; } ?>"
/>
</a>
<p><?php if($caption_text !== '') { echo $caption_text; } ?></p>
</div>
<?php }
elseif(($image_location !== '') && ($caption_text !== '') && ($slimbox_location == '')) { ?>
<div id="cas-2">
<img src="<?php echo $image_location; ?>"
class="<?php if($caption_class !== '') { echo $caption_class; } else { echo "wp-caption aligncenter"; } ?>"
alt="<?php if($image_alt !== '') { echo $image_alt; } ?>"
title="<?php if($image_title !== '') { echo $image_title; } else { echo $image_alt; } ?>"
width="<?php if($image_width !== '') { echo $image_width; } ?>"
height="<?php if($image_height !== '') { echo $image_height; } ?>"
/>
<p><?php if($caption_text !== '') { echo $caption_text; } ?></p>
</div>
<?php }
elseif(($image_location !== '') && ($caption_text == '') && ($slimbox_location !== '')) { ?>
<div id="cas-3">
<a
title="<?php if($slimbox_title !== '') { echo $slimbox_title; } ?>"
rel="lightbox"
href="<?php echo $slimbox_location; ?>"
>
<img src="<?php echo $image_location; ?>"
class="<?php if($image_class !== '') { echo $image_class; } else { echo "aligncenter"; } ?>"
alt="<?php if($image_alt !== '') { echo $image_alt; } ?>"
title="<?php if($image_title !== '') { echo $image_title; } else { echo $image_alt; } ?>"
width="<?php if($image_width !== '') { echo $image_width; } ?>"
height="<?php if($image_height !== '') { echo $image_height; } ?>"
/>
</a>
</div>
<?php }
elseif(($image_location !== '') && ($caption_text == '') && ($slimbox_location == '')) { ?>
<div id="cas 4">
<img src="<?php echo $image_location; ?>"
class="<?php if($image_class !== '') { echo $image_class; } else { echo "alignleft"; } ?>"
alt="<?php if($image_alt !== '') { echo $image_alt;} ?>"
title="<?php if($image_title !== '') { echo $image_title; } else { echo $image_alt; } ?>"
width="<?php if($image_width !== '') { echo $image_width; } ?>"
height="<?php if($image_height !== '') { echo $image_height; } ?>"
/>
</div>
<?php }
else { echo ''; }
?>
(Oui, c'est un peu long, mais on peut oublier les instructions en Html !)Voici le code correspondant que j'essaye d'utiliser avec la condition switch :
<?php
switch ($image_location)
{
case (($image_location !== '') && ($caption_text !== '') && ($slimbox_location !== '')):
{ ?>
<div id="cas-1">
<a
title="<?php if($slimbox_title !== '') { echo $slimbox_title; } ?>"
rel="lightbox"
href="<?php echo $slimbox_location; ?>"
>
<img src="<?php echo $image_location; ?>"
class="<?php if($caption_class !== '') { echo $caption_class; } else { echo "wp-caption aligncenter"; } ?>"
alt="<?php if($image_alt !== '') { echo $image_alt; } ?>"
title="<?php if($image_title !== '') { echo $image_title; } else { echo $image_alt; } ?>"
width="<?php if($image_width !== '') { echo $image_width; } ?>"
height="<?php if($image_height !== '') { echo $image_height; } ?>"
/>
</a>
<p><?php if($caption_text !== '') { echo $caption_text; } ?></p>
</div>
<?php }
;
break;
case ($image_location !== '') && ($caption_text !== '') && ($slimbox_location == '')):
{ ?>
<div id="cas-2">
<img src="<?php echo $image_location; ?>"
class="<?php if($caption_class !== '') { echo $caption_class; } else { echo "wp-caption aligncenter"; } ?>"
alt="<?php if($image_alt !== '') { echo $image_alt; } ?>"
title="<?php if($image_title !== '') { echo $image_title; } else { echo $image_alt; } ?>"
width="<?php if($image_width !== '') { echo $image_width; } ?>"
height="<?php if($image_height !== '') { echo $image_height; } ?>"
/>
<p><?php if($caption_text !== '') { echo $caption_text; } ?></p>
</div>
<?php }
;
break;
case (($image_location !== '') && ($caption_text == '') && ($slimbox_location !== '')):
{ ?>
<div id="cas-3">
<a
title="<?php if($slimbox_title !== '') { echo $slimbox_title; } ?>"
rel="lightbox"
href="<?php echo $slimbox_location; ?>"
>
<img src="<?php echo $image_location; ?>"
class="<?php if($image_class !== '') { echo $image_class; } else { echo "aligncenter"; } ?>"
alt="<?php if($image_alt !== '') { echo $image_alt; } ?>"
title="<?php if($image_title !== '') { echo $image_title; } else { echo $image_alt; } ?>"
width="<?php if($image_width !== '') { echo $image_width; } ?>"
height="<?php if($image_height !== '') { echo $image_height; } ?>"
/>
</a>
</div>
<?php }
;
break;
case (($image_location !== '') && ($caption_text == '') && ($slimbox_location == '')):
{ ?>
<div id="cas 4">
<img src="<?php echo $image_location; ?>"
class="<?php if($image_class !== '') { echo $image_class; } else { echo "alignleft"; } ?>"
alt="<?php if($image_alt !== '') { echo $image_alt;} ?>"
title="<?php if($image_title !== '') { echo $image_title; } else { echo $image_alt; } ?>"
width="<?php if($image_width !== '') { echo $image_width; } ?>"
height="<?php if($image_height !== '') { echo $image_height; } ?>"
/>
</div>
<?php }
;
break;
default: { echo ''; }
}
?>
Quand je teste ce switch, on dirait que Php s'arrête au premier cas, et je ne comprends pas pourquoi ! Merci de votre aide et de votre disponibilité,
Cordialement,
P.L.