0

我有两个数字我想尝试创建一个喜欢/不喜欢的栏。下面是我的 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.

请让我知道这是否没有意义?

4

2 回答 2

2
$total = $like_post_num + $hate_post_num;
$percent = round(($like_post_num / $total) * 100);
于 2013-08-15T23:28:18.527 回答
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>
于 2013-08-15T23:28:57.520 回答