简体   繁体   English

PHP Foreach根据类更改链接文本

[英]PHP Foreach change link text depending on class

I have foreach statement that basically creates a link. 我有foreach语句,基本上创建一个链接。 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. 我知道我可以使用JavaScript做到这一点,但我更希望通过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" 这种方法的结果是每个链接具有相同的文本:“使用Facebook登录使用Google登录使用Twitter登录使用LinkedIn登录使用Outlook登录”

I have also tried to use str_replace as per the following code: 我还尝试根据以下代码使用str_replace:

<?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" 例如,Facebook链接为“使用Facebook登录facebooksloginfacebooksloginfacebooksloginfacebookslogin”

I am fairly sure this can be done but I simply don't have the PHP know how to do it. 我相当确定这是可以做到的,但是我只是没有PHP知道如何去做。 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. 我已经在Google和Stack Overflow上进行了搜索,但是我发现问题的一部分是没有词汇去知道要搜索什么。 Any help is appreciated as always. 一如既往地提供任何帮助。 Thank you. 谢谢。

Your first example is actually correct, you just need to replace the single "=" with a double. 您的第一个示例实际上是正确的,您只需要用双精度代替单个“ =”。

if ($linkClass = "facebookslogin"){echo 'Login With Facebook';}

should become

if ($linkClass == "facebookslogin"){echo 'Login With Facebook';}

A simpler way using arrays could work as such: 使用数组的一种更简单的方法可以这样工作:

<?php
$linkText = array(
    'facebookslogin' => 'Login with Facebook',
    'googleslogin' => 'Login with Google'
);
?>

<a class="btn btn-block"><?php echo $linkText[$linkClass]; ?></a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM