douyao7390 2013-12-19 09:09
浏览 35
已采纳

PHP Foreach根据类更改链接文本

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.

  • 写回答

1条回答 默认 最新

  • dsnw2651 2013-12-19 09:19
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录