如何安排这个输出评级(好、很棒、优秀等)的 php if 语句?

发布于 2024-11-30 22:06:11 字数 1306 浏览 2 评论 0原文

我正在使用 php if 语句向与评级相关的 div 添加类:

CSS:

.average .rating-result {
    color: #888; /* 0~1 votes */
}
.good .rating-result {
    color: #9a6e65; /* 2~3 votes */
}
.great .rating-result {
    color: #aa5443; /* 4~5 votes */
}
.excellent .rating-result {
    color: #bb3b22; /* 6~7 votes */
}
.brilliant .rating-result {
    color: #cc2200; /* 8~9 votes */
}

PHP:

 <div class="topic-like-count<?php if ( $thumbs_number == 0 || 1 ) {
 echo " good"; } elseif ( $thumbs_number == 2 || 3 ) { echo " great"; }
 elseif ( $thumbs_number == 4 || 5 ) { echo " excellent"; } elseif (
 $thumbs_number > 6 || 7 ) { echo " brilliant"; } else { echo "
 average"; }?>"> h4><?php wp_gdsr_render_article_thumbs(); ?></h4>
 </div>

OUTPUT 示例:

<div class="topic-like-count good">
<h4>
<div style="display: none">UA:D [1.9.10_1130]</div>
<div class="thumblock ">
<span class="rating-result">4</span>
<div class="ratingtext ">
<div class="raterclear"></div>
</div>
</h4>
</div>

我不知道如何组织 PHP 部分。上面我描述了这个想法,但该代码无法正常工作。

有人对安排这些 php if 语句有什么建议吗?

I'm using php if-statements to add classes to divs that are related to rating:

CSS:

.average .rating-result {
    color: #888; /* 0~1 votes */
}
.good .rating-result {
    color: #9a6e65; /* 2~3 votes */
}
.great .rating-result {
    color: #aa5443; /* 4~5 votes */
}
.excellent .rating-result {
    color: #bb3b22; /* 6~7 votes */
}
.brilliant .rating-result {
    color: #cc2200; /* 8~9 votes */
}

PHP:

 <div class="topic-like-count<?php if ( $thumbs_number == 0 || 1 ) {
 echo " good"; } elseif ( $thumbs_number == 2 || 3 ) { echo " great"; }
 elseif ( $thumbs_number == 4 || 5 ) { echo " excellent"; } elseif (
 $thumbs_number > 6 || 7 ) { echo " brilliant"; } else { echo "
 average"; }?>"> h4><?php wp_gdsr_render_article_thumbs(); ?></h4>
 </div>

OUTPUT example:

<div class="topic-like-count good">
<h4>
<div style="display: none">UA:D [1.9.10_1130]</div>
<div class="thumblock ">
<span class="rating-result">4</span>
<div class="ratingtext ">
<div class="raterclear"></div>
</div>
</h4>
</div>

I'm not sure how to organize the PHP part. Above I discribed the idea but that code doesn't work properly.

Does anyone have any suggestion to arrange these php if-statements?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

久随 2024-12-07 22:06:11
  <div class="topic-like-count
  <?php 
    switch ($thumbs_number) {
      case 0:
      case 1: echo ' good'; break;
      case 2:
      case 3: echo ' great'; break;
      case 4:
      case 5: echo ' excellent'; break;
      case 6:
      case 7: echo ' brilliant'; break;
      default: echo ' average'; break;
    }
  ?>
  ">
  <h4><?php wp_gdsr_render_article_thumbs(); ?></h4>
  </div>
  <div class="topic-like-count
  <?php 
    switch ($thumbs_number) {
      case 0:
      case 1: echo ' good'; break;
      case 2:
      case 3: echo ' great'; break;
      case 4:
      case 5: echo ' excellent'; break;
      case 6:
      case 7: echo ' brilliant'; break;
      default: echo ' average'; break;
    }
  ?>
  ">
  <h4><?php wp_gdsr_render_article_thumbs(); ?></h4>
  </div>
酒解孤独 2024-12-07 22:06:11

您的 if 语法错误:
if ( $thumbs_number == 0 || 1 ) 计算结果为:
if ( ($thumbs_number == 0) || 1) 始终为真。

你应该写: if ( $thumbs_number == 0 || $thumbs_number == 1 )

Your if syntax is wrong:
if ( $thumbs_number == 0 || 1 ) evaluates to:
if ( ($thumbs_number == 0) || 1) which is always true.

You should write: if ( $thumbs_number == 0 || $thumbs_number == 1 )

ゝ偶尔ゞ 2024-12-07 22:06:11
<?php if ( $thumbs_number == 0 || $thumbs_number ==1 ) {
 echo " good"; } elseif ( $thumbs_number == 2 || $thumbs_number ==3 ) { echo " great"; }
 elseif ( $thumbs_number == 4 || $thumbs_number ==5 ) { echo " excellent"; } elseif (
 $thumbs_number > 6 || $thumbs_number ==7 ) { echo " brilliant"; } else { echo "
 average"; }

?>
<?php if ( $thumbs_number == 0 || $thumbs_number ==1 ) {
 echo " good"; } elseif ( $thumbs_number == 2 || $thumbs_number ==3 ) { echo " great"; }
 elseif ( $thumbs_number == 4 || $thumbs_number ==5 ) { echo " excellent"; } elseif (
 $thumbs_number > 6 || $thumbs_number ==7 ) { echo " brilliant"; } else { echo "
 average"; }

?>
清秋悲枫 2024-12-07 22:06:11

这将导致它始终显示

|| 1 

This will cause it to always display good.

|| 1 
星軌x 2024-12-07 22:06:11

将其合并到 switch 语句中会更有意义;

switch ( $thumbs_number ) {
    case 0:
    case 1:
        $rating = 'good';
        break;
    case 2:
    case 3:
        $rating = 'great'; 
        break;
    // etc ...
}
?>

<div class="topic-like-count <?=$rating?>">

It would make more sense to incorporate this in a switch statment;

switch ( $thumbs_number ) {
    case 0:
    case 1:
        $rating = 'good';
        break;
    case 2:
    case 3:
        $rating = 'great'; 
        break;
    // etc ...
}
?>

<div class="topic-like-count <?=$rating?>">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文