I want an example in php with if then else, that is
lets say a variable have a value containing URL
so
if the URL starts with a cetrain URL www.blabla.com/... then do something
else
if the URL start with www.whateverurl.com/... then do something else ,,
I hope its clear enough Guys, please some help,
My PHP code with the embedded HTML is like that. How can I avoid all the cases and have just one if and one else and not have elseif
?
<?php if ($item->getPrimaryLink()) : ?></br>
<?php if ($item->getPrimaryLink()->getUrl() == "http://www.blabla.com/index.php/article?id=3200") : ?></br>
<a href="<?php echo $item->getPrimaryLink()->getUrl(); ?>" class="readon"><span><?php rc_e('READ_MORE'); ?></span></a>
<?php elseif ($item->getPrimaryLink()->getUrl() == "http://www.blabla.com/index.php/article?id=1508") : ?></br>
<a href="<?php echo $item->getPrimaryLink()->getUrl(); ?>" class="readon"><span><?php rc_e('READ_MORE'); ?></span></a>
<?php elseif ($item->getPrimaryLink()->getUrl() == "http://www.blabla.com/index.php/article?id=1840") : ?></br>
<a href="<?php echo $item->getPrimaryLink()->getUrl(); ?>" class="readon"><span><?php rc_e('READ_MORE'); ?></span></a>
<?php elseif ($item->getPrimaryLink()->getUrl() == "http://www.blabla.com/index.php/article?id=2541") : ?></br>
<a href="<?php echo $item->getPrimaryLink()->getUrl(); ?>" class="readon"><span><?php rc_e('READ_MORE'); ?></span></a>
<?php else : ?></br>
<a href="<?php echo $item->getPrimaryLink()->getUrl(); ?>" class="modal" rel="{size: {x: 1024, y: 550}, handler:'iframe'}"><span><?php rc_e('READ_MORE'); ?></span></a>
<?php endif; ?>
Thank you all and any ideas or suggestions would be really appreciated.
You can use switch()
statement.
switch ($item->getPrimaryLink()->getUrl()) {
case "http://www.blabla.com/index.php/article?id=3200":
echo "<a href=". $item->getPrimaryLink()->getUrl()." class='readon'><span>". rc_e('READ_MORE'). "</span></a>";
break;
case "http://www.blabla.com/index.php/article?id=1508":
echo "<a href=". $item->getPrimaryLink()->getUrl()." class='readon'><span>". rc_e('READ_MORE')."</span></a>";
break;
...
}
Use the explode() function to get the first part of your url.
<?php
$url= $item->getPrimaryLink(); // www.blabla.com/index.php?id=123 <- The slash would be your divider in this case.
$url_ex= explode("/", $url);
// So now you have
//$url_ex[0] = 'www.blabla.com';
//$url_ex[1] = 'index.php?id=123';
?>
<?php if ($item->getPrimaryLink()) : ?></br>
<?php if ($url_ex[0] == 'www.blabla.com'){
echo "<a href=". $item->getPrimaryLink()->getUrl()." class='readon'><span>". rc_e('READ_MORE'). "</span></a>";
}
<?php else : ?></br>
<a href="<?php echo $item->getPrimaryLink()->getUrl(); ?>" class="modal" rel="{size: {x: 1024, y: 550}, handler:'iframe'}"><span><?php rc_e('READ_MORE'); ?></span></a>
<?php endif; ?>
I think you can use something similar to this.