I want to print the total sum before while loop that fetch rows from mysql database
I know that i should fetch the rows than print the variable put my question is there any methode to print the variable before the while loop
I try to do that
$row=mysqli_fetch_array($projectQuery);
and than print $row['sum'];
than
while($row){ }
But this idea didn't work my code don't work i don't have any result
$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'];
?>
in this part off my code i want to print the totalSum
echo"<h2>You Have $count project </h2>";
The code will be like this
You have $count project Total sum $totalSum
Please try below code,
$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
}
this exactly follows the approach explained by @Chris
Also, one more suggestion. You don't need to assign each value into a variable before using it. Instead you can directly use the values like $row['db_projectname']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.