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

时间:2011-08-19 12:33:37

标签: php switch-statement

基本上,我想打开以下switch语句(在Wordpress模板文件中):

    <?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>

到一个函数(存储在 functions.php 中),我可以在模板中使用这样的函数:

<?php rating_class(); ?>

有什么建议吗?

(这个问题有点Worpdress,但我认为这更像是一个php问题)

2 个答案:

答案 0 :(得分:3)

获得评级:

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';
}

打印评级等级:

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

打印投票短语:

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

模板代码:

<?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 :(得分:2)

<?php // Apply style based on number of votes
    function rating_class($thumbs_number)
    {
        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";
        }
    }
?>

<?php rating_class($thumbs_number); ?>