如何安排这个输出评级(好、很棒、优秀等)的 php if 语句?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的 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 )
这将导致它始终显示好。
This will cause it to always display good.
将其合并到 switch 语句中会更有意义;
It would make more sense to incorporate this in a switch statment;