[英]PHP script logic with comparison operators and decimal place
I'm developing the web app and made the (maybe foolish) decision to try and make a rating system rather than implement someone else's. 我正在开发Web应用程序,并做出了(也许很愚蠢的)决定,试图建立一个评级系统而不是实施别人的评级系统。
Below is a screenshot of the web app. 以下是该网络应用的屏幕截图。 The decimal values are the values retrieved from the database.
十进制值是从数据库中检索到的值。 I am using the following logic to turn the value into a star-representation.
我正在使用以下逻辑将值转换为星形表示。
for($x=1;$x<=$this->retreat->total_review_activities_rating;$x++) {
echo '<i class="fas fa-star"></i>';
}
if (strpos($this->retreat->total_review_activities_rating,'.')) {
echo '<i class="fas fa-star-half-alt"></i>';
$x++;
}
while ($x<=5) {
echo '<i class="far fa-star"></i>';
$x++;
}
As you can see from the screenshot, The value 5.000 is hitting the half star option in the loop. 从屏幕截图中可以看到,值5.000在循环中达到了半星选项。 How can I adjust the second argument to avoid this from happening?
如何调整第二个参数以避免发生这种情况?
Any help appreciated. 任何帮助表示赞赏。
The strpos()
function will always output true since it outputs a position, which under Boolean logic is not 0(false). strpos()
函数将始终输出true,因为它输出的位置在布尔逻辑下不为0(false)。
Aside from this your code seems overly complex for a pretty simple problem which I would have approached in a different way. 除此之外,对于一个非常简单的问题,您的代码似乎过于复杂,我可能会以其他方式处理。 Definitely use the decimals as numerical values, and then use that to generate a number of stars.
绝对使用小数作为数值,然后使用它来生成许多星星。 Parsing for the period seems silly.
对该期间进行解析似乎很愚蠢。
while($x > 0) { //While there is still enough rating for a star left
if($x >= 0.75) {
//Add a full star
echo '<i class="fas fa-star"></i>';
$x--; //Remove one start rating
else if($x < 0.25) {
//Don't display a start
$x--; //There isn't enough left for a star so we must be done.
else {
//Display a half-star
echo '<i class="fas fa-star-half-alt"></i>';
$x--; //There is only enough for a half start left so we must be done
}
}
This is all in one loop, doesn't depend on a "limit" rating, and uses less processing time than your solution. 这全都在一个循环中,不依赖于“极限”等级,并且比您的解决方案使用更少的处理时间。 It could even be put in a function given a rating and returning star html.
甚至可以将其放入给定评级并返回星号html的函数中。
You need to change the condition with strpos
, which is always true. 您需要使用
strpos
更改条件,这始终是正确的。
For example compare AVG and integer. 例如比较AVG和整数。
if ($this->retreat->total_review_activities_rating != (int)$this->retreat->total_review_activities_rating) {
// if (4.800 != 4) {true, show half star}
// if (5.000 != 5) {false, no half star}
echo '<i class="fas fa-star-half-alt"></i>';
$x++;
}
$rating = $this->retreat->total_review_activities_rating;
The number of full stars is the whole number part of the rating. 全星的数量是评分的整数部分。
$full_stars = (int) $rating;
echo str_repeat('<i class="fas fa-star"></i>', $full_stars);
You display a half star if the decimal portion of the rating exceeds some value of your choice. 如果等级的小数部分超过您选择的某个值,则显示半星。
$half_star = $rating - $full_stars > 0.2; // 0.2 for example
echo $half_star ? '<i class="fas fa-star-half-alt"></i>' : '';
The number of remaining empty stars is the total number of possible symbols (5) minus the number of symbols displayed so far. 剩余的空星数是可能的符号总数(5)减去到目前为止显示的符号数量。 (The boolean
$half_star
will be converted to int 0 or 1.) (布尔
$half_star
将被转换为int 0或1。)
$empty_stars = 5 - ($full_stars + $half_star);
echo str_repeat('<i class="far fa-star"></i>', $empty_stars);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.