我有一个捐赠网站,并且有点坚持找到当前捐赠的捐赠目标的百分比?
在我的MySQLi数据库中,我有两列 - donation_goal
(数字例如500)和current_raised
(数字例如20.00)。
我需要做的是查找并显示捐赠目标中目前已提高的百分比。因此,如果捐赠10英镑并且目标是100英镑,它将显示10%。但是每次加载页面都会这样做。
我使用下面的代码显示每个页面,并且需要找到一种方法将此功能添加到以下代码中:
<?php
$sql = "SELECT * FROM pages ORDER BY RAND() LIMIT 3";
$result = mysqli_query($mysqli,$sql)or die(mysqli_error());
while($row = mysqli_fetch_array($result)) {
$page_name = $row['page_name'];
$count = strlen($page_name);
if($count < 27){
$space = '<br> <br/>';
} else {
$space = '';
}
$page_dp = $row['page_dp'];
$donation_goal = $row['donation_goal'];
$current_raised = $row['current_raised'];
?>
<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<img src="pageimg/<? echo $page_dp; ?>" style="width:348px;height:233px;" alt="Page Image">
<div class="caption">
<h3><? echo $page_name; ?> <? echo $space; ?></h3>
<div class="progress">
<div class="progress-bar progress-bar-success progress-bar-striped" role="progressbar" aria-valuenow="10" aria-valuemin="0" aria-valuemax="100" style="width: 10%">10%
<span class="sr-only">10% Complete (success)</span>
</div>
</div>
<p><b>£<? echo $donation_goal; ?>.00</b> Target | <b>£<? echo $current_raised; ?></b> Raised</p>
Here is a little info about this current topic, please help us raise more than we can! We need your help!
<p><a href="#" class="btn btn-success" role="button">Donate Now</a> <a href="#" class="btn btn-default" role="button">Details</a></p>
</div>
</div>
</div>
<?
}
?>
我尝试将此代码添加到混音中,但收到错误,所以我很确定我不知道如何正确添加它:
$sql = "SELECT * FROM pages, COUNT( current_raised ) AS test, concat(round(( current_raised/donation_goal * 100 ),2),'%') AS percentage ORDER BY id DESC LIMIT 3";
这只是给出了错误。有关如何使用短代码高效执行此操作的任何想法?
答案 0 :(得分:1)
如果您想从目标中计算出目标的百分比,可以直接在查询中执行此操作:
SELECT page_dp,
donation_goal,
current_raised,
ROUND(current_raised/donation_goal*100) percentage
FROM pages
ORDER BY RAND()
LIMIT 3
然后你只需输出循环中的百分比:
<span class="sr-only"><?php print $row['percentage']; ?>% Complete (success)</span>
答案 1 :(得分:0)
这是你想要的吗?
SELECT p.*, pc.cr / donation_goal
FROM pages p CROSS JOIN
(SELECT SUM(current_raised) as cr FROM pages) pc
ORDER BY RAND()
LIMIT 3;