[英]How to print total sum before fetching value in mysql
我想在while循环之前打印总和,从mysql数据库中获取行
我知道我应该获取行而不是打印变量把我的问题是有任何方法在while循环之前打印变量
我试着这样做
$row=mysqli_fetch_array($projectQuery);
而不是打印$row['sum'];
比
while($row){ }
但这个想法不起作用我的代码不起作用我没有任何结果
$projectQuery=mysqli_query($conn,"
select
project.*,
location.db_lid,
location.db_location as loc,
concat(tr.db_fname,' ',tr.db_lname) as trans,
tr.db_uid,
concat(user.db_fname,' ',user.db_lname) as name,
user.db_uid,
client.db_clientid,
client.db_name as clientname,
department.db_did,
department.db_department as dep,
supplier.db_id,
supplier.db_CompanyName,
sum(activities.db_newtotal) as total
from tbl_project as project
left join tbl_location as location
on
project.db_location=location.db_lid
left join tbl_user as tr
on
project.db_transferredto=tr.db_uid
left join tbl_user as user
on
project.db_user=user.db_uid
left join tbl_client as client
on
project.db_client=client.db_clientid
left join tbl_department as department
on
project.db_department=department.db_did
left join tbl_supplier as supplier
on
project.db_transsub=supplier.db_id
left join tbl_activities as activities
on
project.db_projectid=activities.db_projectname
{$q}
group by project.db_projectid order by project.db_projectid desc
")or die(mysqli_error($conn));
$count=mysqli_num_rows($projectQuery);
if($count!=0){
echo"<h2>You Have $count project </h2>";?>
<div class="table-responsive">
<table class='ol-md-12 table-bordered table-striped table-condensed cf table-bordered' id="alternatecolor">
<thead class='cf'>
<tr>
<th rowspan='2'>#</th>
<th rowspan='2'>Date</th>
<th rowspan='2'>Project</th>
<th rowspan='2'>Location</th>
<th rowspan='2'>Cleint</th>
<th rowspan='2'>Project Manager</th>
<th rowspan='2'>Staff</th>
<th rowspan='2'>Subcontractor</th>
<th rowspan='2'>Company Name</th>
<th rowspan='2'>Department</th>
<th colspan='3'>Planing</th>
<th rowspan='2'>%</th>
<th rowspan='2'>Civil %</th>
<th rowspan='2'>Electrical %</th>
<th rowspan='2'>Mechanical %</th>
<th rowspan='2'>Total Cost</th>
<th rowspan='2'>Note</th>
<th rowspan='2'>Checklist</th>
<th rowspan='2'>Add</th>
<th rowspan='2'>Print</th>
<th rowspan='2'>Status</th>
<th rowspan='2'></th>
<th rowspan='2'>Edit</th>
</tr>
<tr>
<th> Start Date</th>
<th> Due Date</th>
<th >Duration</th>
</tr>
</thead>
<tbody>
<?php
$totalSum=0;
while($row=mysqli_fetch_array($projectQuery)){
$projectid=$row['db_projectid'];
$projectName=$row['db_projectname'];
$location=$row['loc'];
$transferred=$row['trans'];
$psd=$row['db_psd'];
$pdd=$row['db_pdd'];
$duration=$row['db_duration'];
$asd=$row['db_past'];
$add=$row['db_padd'];
$aduration=$row['db_aduration'];
$percent=$row['db_percent'];
$note=$row['db_pnote'];
$staff=$row['name'];
$client=$row['clientname'];
$cPercent=$row['db_cpercentage'];
$ePercent=$row['db_epercentage'];
$mPercent=$row['db_mpercentage'];
$status=$row['db_status'];
$supp=$row['db_CompanyName'];
$companyName=$row['db_companyname'];
$department=$row['dep'];
$clientid=$row['db_clientid'];
$date=$row['db_date'];
$totaCost=$row['total'];
$totalSum+=$row['total'];
?>
在这部分关闭我的代码我想打印totalSum
echo"<h2>You Have $count project </h2>";
代码将是这样的
你有$ count项目总和$ totalSum
请尝试下面的代码,
$totalSum=0;
$projects = array();
while($row = mysqli_fetch_array($projectQuery)){
$totalSum+=$row['total'];
$projects[] = $row;
}
echo"<h2>You Have $totalSum project </h2>";
foreach($projects AS $project) {
//you can loop through your records here
}
这完全遵循@Chris解释的方法
还有一个建议。 在使用变量之前,您无需将每个值分配给变量。 相反,您可以直接使用$ row ['db_projectname']之类的值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.