I have foreach statement that basically creates a link. Each link has it's own unique class. What I would like to do is create specific link text depending on what class the link has. I know I can do this with JavaScript but I would prefer to do it via PHP.
I have tried two different solutions. The first is as follows:
<?php
foreach($plugins as $link):
$linkParams = '';
if(isset($link['params'])){
foreach($link['params'] as $k => $v){
$linkParams .= ' ' . $k . '="' . $v . '"';
}
}
?>
<?php
$linkClass = $link['class'];
?>
<a class="btn btn-block btn-<?php echo $link['class'];?>" rel="nofollow" <?php echo $linkParams;?> href="<?php echo JRoute::_($link['link']);?>"><?php
if ($linkClass = "facebookslogin"){echo 'Login With Facebook';}
if ($linkClass = "googleslogin"){echo 'Login With Google';}
if ($linkClass = "twitterslogin"){echo 'Login With Twitter';}
if ($linkClass = "linkedinslogin"){echo 'Login With LinkedIn';}
if ($linkClass = "liveslogin"){echo 'Login With Outlook';} ?></a>
<?php endforeach; ?>
The result of this approach is that each link has the same text: "Login With FacebookLogin With GoogleLogin With TwitterLogin With LinkedInLogin With Outlook"
I have also tried to use str_replace as per the following code:
<?php
foreach($plugins as $link):
$linkParams = '';
if(isset($link['params'])){
foreach($link['params'] as $k => $v){
$linkParams .= ' ' . $k . '="' . $v . '"';
}
}
?>
<?php
$linkClass = $link['class'];
$facebookslogin = str_replace("facebookslogin", "Login With Facebook", $linkClass);
$googleslogin = str_replace("googleslogin", "Login With Google", $linkClass);
$twitterslogin = str_replace("twitterslogin", "Login With Twitter", $linkClass);
$linkedinslogin = str_replace("linkedinslogin", "Login With LinkedIn", $linkClass);
$liveslogin = str_replace("liveslogin", "Login With Outlook", $linkClass);
$loginText = $fbslogin.$googleslogin.$twitterslogin.$linkedinslogin.$liveslogin;
?>
<a class="btn btn-block btn-<?php echo $link['class'];?>" rel="nofollow" <?php echo $linkParams;?> href="<?php echo JRoute::_($link['link']);?>"><?php echo $loginText; ?></a>
<?php endforeach; ?>
The result of this was that each link had it's correct text but it was surrounded by the other classes (incorrect text). For example the Facebook link was "Login With Facebookfacebooksloginfacebooksloginfacebooksloginfacebookslogin"
I am fairly sure this can be done but I simply don't have the PHP know how to do it. I have searched for this on Google and Stack Overflow but I find that part of the problem is not have the vocabulary to know what to search for. Any help is appreciated as always. Thank you.