<?php foreach((array)$query as $row):?>
<td><?php echo $row->date ?></td>
<td><?php echo $row->cost ?></td>
<?php endforeach ;?>
I want to sum all values in $row->cost when $row->date=02-01-2016, like this in a table
Thanks.
echo array_sum(array_filter(array_map(function($row){
if($row->date == '02-01-2016')
return $row->cost;
}, $array)));
You can use
array_map: to compare and skip the values for which cost is not needed to add in sum.
array_filter: and then to remove the blank indexes
array_sum: and finally to sum cost
Note: if you are getting results from database, you can simply do sum there (see below):
$db->query("SELECT SUM(costs) FROM table_name WHERE `date`='2016-01-02'");
echo $db->fetchColumn();
Try below code:
<?php
$total = 0;
foreach((array)$query as $row):
$date = $row->date;
$cost = $row->cost;
if(date("M-d-Y", strtotime($date))==date("M-d-Y")) {
$total = $total+$cost;
}
?>
<td><?php echo $date ?></td>
<td><?php echo $cost ?></td>
<?php endforeach; ?>
<td colspan="2"><?php echo $total; ?></td>
<?php
$tot = 0;
foreach( (array)$query as $row ) {
if( $row->date=='02-01-2016' ) $tot = $tot+$row->cost;
?>
<tr>
<td><?php echo $row->date ?></td>
<td><?php echo $row->cost ?></td>
</tr>
<?php } ?>
<tr><td colspan="2"><?php echo $tot; ?></td></tr>
obviously, the comparison works if $row->date is formatted as dd-mm-yyyy
<table border=1>
<?php
$tot = 0;
foreach($query as $row ) {
if( $row->date=='2016-01-02' ) $tot += $row->cost;
?>
<tr>
<td><?php echo $row->date ?></td>
<td><?php echo $row->cost ?></td>
</tr>
<?php } ?>
<tr><td></td><td><?php echo $total; ?></td></tr>
</table>
It will work for you exctly what u want.
Related
Hi I have shopping cart and i want print item name in different php file. I try this.
<?php
if(!empty($_SESSION["cart"]))
{
$total = 0;
foreach($_SESSION["cart"] as $keys => $values)
{
$product_cart = $values["item_name"];
?>
<tr>
<td><?php echo $values["item_name"]; ?></td> <?php session_start(); $_SESSION['produktik'] = $values["item_name"] ; ?>
<td><?php echo $values["item_quantity"] ?></td>
<td><?php echo $values["product_price"]; ?>€</td>
<td><?php echo number_format($values["item_quantity"] * $values["product_price"], 2); ?>€</td>
<td><span class="text-danger">X</span></td>
</tr>
and second php file is
<pre>
<?php
session_start();
echo $_SESSION['produktik'];
?>
</pre>
I want to display based on the year with repeated first_ ids and actual ids. but actual_id is unique.
<?php while($Row=oci_fetch_assoc($Result)){
$ResultArray[$j]['First_ID']=$Row['First_ID'];
$ResultArray[$j]['Actual_ID']=$Row['Actual_ID'];
$ResultArray[$j]['Title']=$Row['Title'];
$ResultArray[$j]['START_DATE']=$Row['START_DATE'];
$startDate=explode(' ',$Row['START_DATE']);
$ResultArray[$j]['YEAR']=$startDate[0];
if(($startDate[0])==($startyear)){
$ResultArray[$j][$startyear]['Actual_ID']=$Row['Actual_ID'];
$ResultArray[$j][$startyear]['Other_ID']=$Row['Other_ID'];
$ResultArray[$j][$startyear]['Content']=$Row['Content'];
}
if(($startDate[0])==($startyear+1)){
$ResultArray[$j][$startyear+$i]['Actual_ID']=$Row['Actual_ID'];
$ResultArray[$j][$startyear+$i]['Other_ID']=$Row['Other_ID'];
$ResultArray[$j][$startyear+$i]['Content']=$Row['Content'];
}
if(($startDate[0])==($startyear+1)){
$ResultArray[$j][$startyear+$i]['Actual_ID']=$Row['Actual_ID'];
$ResultArray[$j][$startyear+$i]['Other_ID']=$Row['Other_ID'];
$ResultArray[$j][$startyear+$i]['Content']=$Row['Content'];
}
$j++;
}?>
//to display in table
it display year with asc order first and then next year etc.
<table class="tab">
<tr><th>S.No</th>
<th>first_ id</th><th>Title</th>
<?phpfor($i=0;$i<4;$i++){?><th colspan="3"><?php echo $startyear+$i; ?> </th>
<?php }?>
<tr><th></th><th></th><th></th>
<th>Actual_ID</th><th>Other_ID</th><th>Content</th>
<th>Actual_ID</th><th>Other_ID</th> <th>Content</th>
<th>Actual_ID</th><th>Other_ID</th><th>Content</th>
<th>Actual_ID</th><th>Other_ID</th><th>Content</th></tr>
<?php
foreach($ResultArray as $ResultArray1){
//print_r($abstarctResultArray1);
?><tr><td><?php echo $count; $count++;?></td>
<td><?php echo $ResultArray1['First_ID'];?></td>
<td><?php echo $ResultArray1['Title'];?></td>
<?php for($i=0;$i<4;$i++){
$startDate=explode(' ',$ResultArray1['START_DATE']);
if(($startDate[0]==$startyear)){?>
<td><?php echo $ResultArray1[$startDate[0]['Actual_ID'];?></td>
<td><?php echo $ResultArray1[$startDate[0]['Other_ID'];?></td>
<td><?php echo $ResultArray1[$startDate[0]['Content'];?></td>
<?php }
else if(($startDate[0]==$startyear+1)){?>
<td><?php echo $ResultArray1[$startDate[0]['Actual_ID'];?></td>
<td><?php echo $ResultArray1[$startDate[0]['Other_ID'];?></td>
<td><?php echo $ResultArray1[$startDate[0]['Content'];?></td>
</tr>
<?php
}
else if(($startDate[0]==$startyear+2)){?>
<td><?php echo $ResultArray1[$startDate[0]['Actual_ID'];?></td>
<td><?php echo $ResultArray1[$startDate[0]['Other_ID'];?></td>
<td><?php echo $ResultArray1[$startDate[0]['Content'];?></td>
</tr>
<?php
}
}
}
?>
I have tried with different ideas, but I can't. Please help.
I have created a simple Business Management setup for office work. First I fetch all information from database. All is going well except the quantity input.
In this all payment doing calculate sum and do Echo. But I want that this echo value detect automatic store in database in another table.
so how to complete this?
This is the code of program.
<tbody>
<?php
$respectivestud = mysql_query("select * from client where client_id");
$i = 1;
while($r_client = mysql_fetch_array($respectivestud))
{
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $r_client['name']; ?></td>
<td><?php echo $r_client['project_name']; ?></td>
<td><?php echo $r_client['cost']; ?></td>
<td><?php echo $r_client['payment_1']; ?></td>
<td><?php echo $r_client['payment_2']; ?></td>
<td><?php echo $r_client['payment_3']; ?></td>
<td><?php echo $r_client['payment_4']; ?></td>
<td><?php echo $r_client['payment_5']; ?></td>
<?php $total = $r_client['payment_1'] + $r_client['payment_2'] + $r_client['payment_3'] + $r_client['payment_4'] + $r_client['payment_5'];?>
<td> <?php echo $total ; ?></td>
<?php $remain = $r_client['cost'] - $total;?>
<td><?php echo $remain; ?></td>
**<td>**
Please make sure you write valid PHP code. You are not closing your while and you are not closing tags correctly. This should be correct.
<tbody>
<?php
$respectivestud = mysql_query("select * from client where client_id");
$i = 1;
while($r_client = mysql_fetch_array($respectivestud))
{
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $r_client['name']; ?></td>
<td><?php echo $r_client['project_name']; ?></td>
<td><?php echo $r_client['cost']; ?></td>
<td><?php echo $r_client['payment_1']; ?></td>
<td><?php echo $r_client['payment_2']; ?></td>
<td><?php echo $r_client['payment_3']; ?></td>
<td><?php echo $r_client['payment_4']; ?></td>
<td><?php echo $r_client['payment_5']; ?></td>
<?php $total = $r_client['payment_1'] + $r_client['payment_2'] + $r_client['payment_3'] + $r_client['payment_4'] + $r_client['payment_5'];?>
<td> <?php echo $total ; ?></td>
<?php $remain = $r_client['cost'] - $total;?>
<td><?php echo $remain; ?></td>
</tr>
<?php
}
?>
</tbody>
To save data you could use this:
$sql=mysqli_query($connection,"insert into `tablename`(`columnname`)values('".$total."')");
foirst off all replace mysql_* with mysqli_* or Pdo statement. as mysql_* is depricated now . here is the reference mysqli and pdo
store the remaining ammount in a variable. then use this variable in update query . like
<td><?php echo $remaining_amount=$remain; ?></td>
then update in db
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE MyGuests SET tablename='".$remaining_amount."' WHERE id=yprimarykey";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
<td><?php echo $r_client['payment_5']; ?></td>
<?php $total = $r_client['payment_1'] + $r_client['payment_2'] +$r_client['payment_3'] + $r_client['payment_4'] + $r_client['payment_5'];?>
<td> $sql=mysqli_query($connection,"insert into `tablename`(`columnname`)values('".$total."')");</td>
you can do like this whatever the value you want to insert...
I've been troubleshooting for a while now, and I really can't find any answer. Basically this is what it looks like:
When I type the name of a real user: Nothing posted on the page with an exception of the header
When I type a fake user: User has not been found (direct translation from Norwegian in the code)
<?php
include_once("moduler/head.php");
$user = $_GET['user'];
$query1 = $con->query("SELECT * FROM players WHERE name='$user'");
echo $con->error;
if ($query1->num_rows == 0) {
echo "<h1>Ingen spiller funnet med: $user.</h1>";
return;
}
while ($row == mysqli_fetch_array($query1)) {
$styrkeLvl = $row['styrke'];
$beskyttelseLvl = $row['beskyttelse'];
$bueskytingLvl = $row['bueskyting'];
$trehuggingLvl = $row['trehugging'];
$gruvedriftLvl = $row['gruvedrift'];
$fiskingLvl = $row['fisking'];
$kills = $row['kills'];
$deaths = $row['deaths'];
$rank = $row['rank'];
$money = $row['money'];
$donstatus = $row['donationstatus'];
$lastlogin = $row['lastlogin'];
$regtime = $row['registrationtime'];
$rankName = getRankString($rank);
?>
<h1><?php echo $user; ?></h1>
<table class=\"userView\">
<tbody>
<tr><td>Brukerstatus</td>
<td><?php echo $rankName; ?></td>
</tr>
<tr><td>Donasjon status</td>
<td>D<?php echo $donstatus; ?></td>
</tr>
<tr><td>Styrke level</td>
<td><?php echo $styrkeLvl; ?></td>
</tr>
<tr><td>Beskyttelse level</td>
<td><?php echo $beskyttelseLvl; ?></td>
</tr>
<tr><td>Bueskyting level</td>
<td><?php echo $bueskytingLvl; ?></td>
</tr>
<tr><td>Trehugging level</td>
<td><?php echo $trehuggingLvl; ?></td>
</tr>
<tr><td>Fisking level</td>
<td><?php echo $fiskingLvl; ?></td>
</tr>
<tr><td>Drap</td>
<td><?php echo $kills; ?></td>
</tr>
<tr><td>Dødsfall</td>
<td><?php echo $deaths; ?></td>
</tr>
<tr><td>Sist pålogget</td>
<td><?php echo $lastlogin; ?></td>
</tr>
<tr><td>Registreringsdato</td>
<td><?php echo $regtime; ?></td>
</tr>
</tbody>
</table>
<?php
}
?>
Any ideas? I've also tried running the entire thing in an echo with no result
Thanks
All your variables inside the while loop are only available inside the while loop. so you cannot echo them in the tables. so <td><?php echo $rankName; ?></td> will give you an undefined variable error. The same applies for the other variables you are trying to display.
To avoid this you should try to declare your variables globally, or put your table inside the loop.
Also the other issue is that as it is the code inside the loop will never execute because of the double equal (==). You should use double equal (==) for comparison and single equal (=) for assignment.
So you have to change while ($row == mysqli_fetch_array($query1)) to while ($row = mysqli_fetch_array($query1))
I want to recapitulate report that grouped by month. But the results of sum is incorrect. It's just sum data in one month and show in all rows.
<?php
$query = mysql_query("SELECT * FROM barang GROUP BY arsip");
$i = 1;
while ($data = mysql_fetch_assoc($query)) {
?>
<tr class="<?php if ($i % 2 == 0) { echo "odd"; } else { echo "even"; } ?>">
<td><?php echo $i; ?></td>
<td><?php echo $data['arsip']; ?></td>
<td><?php
$Masuk= mysql_query("SELECT SUM(barang_in) AS masuk FROM barang GROUP BY arsip");
if($Masuk){
$data = mysql_fetch_assoc($Masuk);
echo $data['masuk'];
}
?></td>
<td><?php
$keluar= mysql_query("SELECT SUM(bijih_out+htm_out+pth_out) AS keluar FROM barang GROUP BY arsip");
if($keluar){
$data = mysql_fetch_assoc($keluar);
echo $data['keluar'];
}?></td>
<td><?php
$efisiensi= mysql_query("SELECT SUM((bijih_out+htm_out+pth_out)-barang_in) AS efisiensi FROM barang GROUP BY arsip");
if($efisiensi){
$data = mysql_fetch_assoc($efisiensi);
echo $data['efisiensi'];
}
?></td>
<td><?php
$persen= mysql_query("SELECT SUM(barang_in/(bijih_out+htm_out+pth_out))*1 AS persen FROM barang GROUP BY arsip");
$simbol = "%";
if($persen){
$data = mysql_fetch_assoc($persen);
echo number_format($data['persen'],2); echo $simbol;
}
?></td>
<?php
$i++;
}
?>
so that's my code. what's wrong with that? There are some columns for displaying income items, outcome items, efficiency ( difference of outcome and income items ) and percent of efficiency.
finally I found out the right code. Thankyou for #RandomSeed who helps me. This is my final code and it works!
<?php
$query = mysql_query("SELECT arsip, SUM(barang_in) AS income, SUM(bijih_out+htm_out+pth_out) AS outcome, SUM((bijih_out+htm_out+pth_out
)-barang_in) AS efficiency, SUM((barang_in / ( bijih_out + htm_out + pth_out))*100) AS percent FROM barang GROUP BY arsip");
$i = 1;
while ($data = mysql_fetch_assoc($query)) {
?>
<tr class="<?php if ($i % 2 == 0) { echo "odd"; } else { echo "even"; } ?>">
<td><?php echo $i; ?></td>
<td><?php echo $data['arsip']; ?></td>
<td><?php echo $data['income']; ?></td>
<td><?php echo $data['outcome']; ?></td>
<td><?php echo $data['efficiency']; ?></td>
<td><?php $simbol = "%"; echo number_format($data['percent'],0); echo $simbol; ?></td>
<?php
$i++;
}
?>