doulu1968 2011-08-19 12:33
浏览 42
已采纳

将此switch语句转换为函数(并将内容与逻辑分开)?

Basically, I would like to turn the following switch-statement (which is in a Wordpress template file):

    <?php preg_match( '!<div class="thumblock ">(.*)</div>!si' , wp_gdsr_render_article_thumbs(0, false, "", 0, "", false) , $n );
    $thumbs_number = strip_tags( $n[1] ); ?>

    <div class="topic-like-count
        <?php // Apply style based on number of votes
            switch ($thumbs_number) {
                case ($thumbs_number == 0):
                case ($thumbs_number == 1): echo ' average'; break;
                case ($thumbs_number == 2):
                case ($thumbs_number == 3): echo ' good'; break;
                case ($thumbs_number == 4):
                case ($thumbs_number == 5): echo ' great'; break;
                case ($thumbs_number == 6):
                case ($thumbs_number == 7): echo ' excellent'; break;
                default:
                    if ($thumbs_number <= -1) echo "bad";
                    else if ($thumbs_number > 7) echo "brillant";
            }
        ?>
      ">
        <h4><?php wp_gdsr_render_article_thumbs(); ?></h4>

        <?php if ( $thumbs_number == 1 || $thumbs_number == -1 ) : ?>
            <span><?php _e( 'vote' ); ?></span>
        <?php else : ?>
            <span><?php _e( 'votes' ); ?></span>
        <?php endif; ?>
    </div>

into a function (stored in functions.php) that I can use like this in a template:

<?php rating_class(); ?>

Any suggestions?

(The question has a bit of Worpdress but I think it is more a php question)

  • 写回答

2条回答 默认 最新

  • drema2014 2011-08-19 12:45
    关注

    Get the rating class:

    function get_rating_class($thumbs_number) {
        if ($thumbs_number < 0) return 'bad';
        if ($thumbs_number < 2) return 'average';
        if ($thumbs_number < 4) return 'good';
        if ($thumbs_number < 6) return 'great';
        if ($thumbs_number < 8) return 'excellent';
        return 'brillant';
    }
    

    Print the rating class:

    function rating_class($thumbs_number) {
        echo get_rating_class($thumbs_number);
    }
    

    Print the vote phrase:

    function votes($thumbs_number) {
        echo ($thumbs_number == 1 || $thumbs_number == -1) ? _e('vote') : _e('votes');
    }
    

    Template code:

    <?php
        preg_match( '!<div class="thumblock ">(.*)</div>!si' , wp_gdsr_render_article_thumbs(0, false, "", 0, "", false) , $n );
        $thumbs_number = strip_tags( $n[1] );
    ?>
    
    <div class="topic-like-count <?php rating_class($thumbs_number); ?>">
        <h4><?php wp_gdsr_render_article_thumbs(); ?></h4>
        <span><?php votes($thumbs_number); ?></span>
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集