我有两个号码我想尝试创建一个喜欢/不喜欢的酒吧。下面是我的PHP和HTML,HTML使用宽度值来显示不喜欢栏中“喜欢”的数量。见下文:
<div class="dislike_base">
<div class="like" style="width: 52%"></div>
</div>
我只是不确定如何对两个数字进行数学比较以获得百分比。
PHP:
$like_post_num = 13;
$hate_post_num = 10;
$total = $like_post_num + $hate_post_num
//how do I compare the above information to get the percent of 100 of likes vs dislikes.
如果没有意义,请告诉我?
答案 0 :(得分:2)
$total = $like_post_num + $hate_post_num;
$percent = round(($like_post_num / $total) * 100);
答案 1 :(得分:2)
<?php
$like_post_num = 13;
$hate_post_num = 10;
$sum = $like_post_num + $hate_post_num;
$like_percent = round($like_post_num / $sum * 100);
$hate_percent = round($hate_post_num / $sum * 100);
?>
<div class="dislike_base">
<div class="like" style="width: <?php echo $like_percent; ?>"px></div>
<div class="dislike" style="width: <?php echo $hate_percent; ?>"px></div>
</div>