In my admin panel i have a view of orders of day, individually is ok, but i want to count and sum the values (count number of orders) and (sum value from orders).
$pedido->valor_pedido (is the individual price from order)
My code is like this.
if(count($pedidos) > 0){ foreach($pedidos as $pedido){ $totalHoje += $valorCompra; ?>
<tr onclick="window.location='<?=base_url();?>index.php/adm/pedidos/alterar/<?=$pedido->id;?>'" style="cursor:pointer" target='_blank'>
<td width="50">
<?=$pedido->id;?>
</td>
<td class="pedidonome">
<?=strtolower($pedido->nome ? $pedido->nome : $pedido->razao_social);?>
</td>
<td>
<?php echo getPagamento($pedido->pagamento);?>
</td>
<td>R$
<?=$pedido->frete;?>
</td>
<td>R$
<?=$pedido->valor_pedido;?>
</td>
<td><span class="<?=$pedido->situacao;?>"><?=$pedido->situacao;?></span></td>
</tr>
If i understand you the right way you need to get a sum of orders, that's how you can do this:
<?php if(count($pedidos) > 0): ?>
<?php $sum = 0.00; ?>
<?php foreach($pedidos as $pedido): ?>
<tr onclick="window.location='<?=base_url();?>index.php/adm/pedidos/alterar/<?=$pedido->id;?>'" style="cursor:pointer" target='_blank'>
<?php $sum += $pedido->valor_pedido; ?>
<td width="50">
<?=$pedido->id;?>
</td>
<td class="pedidonome">
<?=strtolower($pedido->nome ? $pedido->nome : $pedido->razao_social);?>
</td>
<td>
<?php echo getPagamento($pedido->pagamento);?>
</td>
<td>R$
<?=$pedido->frete;?>
</td>
<td>R$
<?=$pedido->valor_pedido;?>
</td>
<td><span class="<?=$pedido->situacao;?>"><?=$pedido->situacao;?></span></td>
</tr>
...................
<?php endforeach; ?>
<?= 'Total: ' . $sum ?>
<?php endif; ?> //end if statement
<?php
$con = mysqli_connect("localhost","username","pwd","db name);
$result = mysqli_query($con,"select count(1) FROM tablename");
$row = mysqli_fetch_array($result);
$total = $row[0];
echo "Total rows: " . $total;
?>
Related
Ok, I have a variable called $total_bal that is the answer of a simple equation made from two queries stored in variables x and y
for example
$y = $row->amount_one;
$z = $row->amount_two;
$total_bal = $z + $y;
However I have many entries in amount_one and amount_two.
As I am using codeigniters active records
I tried
echo $this->db->count_all($total_bal);
But this dose not work, any idea of the best way to do this ?
So Im after a way to add all the $total_balup, for more incite into my code see bellow.
<?php
if (isset($records)) : foreach ($records as $row) :
$x = $row->amount_two;
$y = $row->ammount_one;
$total_bal = $z + $y;
?>
<table>
<tbody>
<tr>
<td>amount one</td>
<td>amount two</td>
</tr>
<tr>
<td>
<?php echo $x;?>
</td>
<td>
<?php echo $y;?>
</td>
<td>
<?php echo $$total_bal;?>
</td>
</tr>
<!-- <tr>-->
<!-- <td>-->
<!-- --><?php //echo $this->db->count_all('$total_bal'); ?>
<!-- </td>-->
<!-- </tr>-->
</tbody>
</table>
<?php endforeach; ?>
<?php else : ?>
<h3>You Have No Accounts</h3>
<h4>Why No Add A Account?</h4>
<?php endif; ?>
One way to do this is to use an accumulator variable. Take for example, a $grandTotal variable. You set it to 0 outside the foreach, over each iteration of the loop, you add the $rowTotal to the $grandTotal. Eventually when the loop ends, you have a total value of all row totals.
The benefit to this method is that it doesn't require any additional calls to the database and since you are already looping through the values to display them, accumulating them is minimal processing.
<?php if (isset($records)) : ?>
<table>
<thead>
<tr>
<th>Amount One</th>
<th>Amount Two</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<?php $grandTotal = 0; ?>
<?php foreach ($records as $row) : ?>
<?php
// Add field values to get row total
$rowTotal = $row->amount_one + $row->amount_two;
?>
<?php
// Add row total to grand total
$grandTotal += $rowTotal;
?>
<tr>
<td>
<?php echo $row->amount_one;?>
</td>
<td>
<?php echo $row->amount_two;?>
</td>
<td>
<?php echo $rowTotal;?>
</td>
</tr>
<?php endforeach; ?>
<tr>
<td></td>
<td></td>
<td><?php $grandTotal; ?></td>
</tr>
</tbody>
</table>
<?php else : ?>
<h3>You Have No Account</h3>
<h4>Why Not Add An Account?</h4>
<?php endif;?>
I attached my output image here: this my output
<table width="100%" style="text-align:center">
<tr>
<td>Subject </td>
<td>Grade </td>
</tr>
<?php
include 'connect.php';
$class_n =$_POST['class_n'];
$class_s =$_POST['class_s'];
$r =$_POST['roll'];
$sql="select * from full_result where class_n='$class_n' AND class_s='$class_s' AND roll='$r'";
$data = mysql_query($sql);
$sum=0;
while ($row = mysql_fetch_array($data))
{
$r=$row['marks'];
if($r>=40)
{
$r1=$row['point'];
}
elseif($r<=33)
{
$r1=0;
}
$sum=$sum+$r1;
?>
<tr>
<td><?php echo $row['subject']; ?></td>
<td><?php echo $row['grade']; ?> </td>
<td><?php echo $row['point']; ?> </td>
</tr>
<?php } ?>
</table>
<br>
<p><?php echo $sum;
?> </p>
This my code, here I am trying to use one condition, but it's doesn't work. it comes almost total Marks. But what I want is that when any subject becomes 0 or F, the Total mark will become F.
Use a flag variable and set it to 1 if the student get 0 or F (I assume it's when $r <= 33) for any subject. Then before displaying sum, check if the flag is set and if set change value of $sum to 'F'
<table width="100%" style="text-align:center">
<tr>
<td>Subject </td>
<td>Grade </td>
</tr>
<?php
include 'connect.php';
$class_n =$_POST['class_n'];
$class_s =$_POST['class_s'];
$r =$_POST['roll'];
$flag = 0;
$sql="select * from full_result where class_n='$class_n' AND class_s='$class_s' AND roll='$r'";
$data = mysql_query($sql);
$sum=0;
while ($row = mysql_fetch_array($data))
{
$r=$row['marks'];
if($r>=40)
{
$r1=$row['point'];
}
elseif($r<=33)
{
$r1=0;
$flag = 1;
}
$sum=$sum+$r1;
?>
<tr>
<td><?php echo $row['subject']; ?></td>
<td><?php echo $row['grade']; ?> </td>
<td><?php echo $row['point']; ?> </td>
</tr>
<?php }
if($flag == 1)
{
$sum = F;
}
?>
</table>
<br>
<p><?php echo $sum; ?>
?> </p>
I have to display a table of 5 rows fetching from MySQL database using PHP. From the code below, if I have 3 rows in database, it will display only 3 rows. But, I need to display 5 rows with 2 empty rows along with 3 fetched rows. If there are no records found in the database, I should display 5 empty rows. I need your help on that.
Note: I am generating a report using PHP and MySQL. From the above method, I can fix the table height and so report will generate without any overlaps.
CODE:
<?php
$select= "select * from table where id=1";
$select2= mysql_query($select);
$select3= mysql_num_rows($select2);
$row_count = 1;
while($row = mysql_fetch_assoc($select2)){
?>
<tr>
<td ><?php echo $row_count;?>.</td>
<td ><?php echo $rows['id']; ?></td>
<td ><?php echo $rows['name']; ?></td>
<td ><?php echo $rows['phone_number']; ?></td>
</tr>
<?php $row_count++;
}?>
As you are already counting the $row_count variable, you can add simple while loop, like this:
<?
while($row_count < 5){
?>
<tr>
<td > </td>
<td > </td>
<td > </td>
<td > </td>
</tr>
<?php $row_count++;
}?>
What is ? Is it needed?
Also check the #paxdiablo's answer about limit 5 option for your query.
This should do the trick.
<?php
$select= "select * from table where id=1";
$select2= mysql_query($select);
$select3= mysql_num_rows($select2);
$row_count = 1;
while($row = mysql_fetch_assoc($select2)){
?>
<tr>
<td ><?php echo $row_count;?>.</td>
<td ><?php echo $rows['id']; ?></td>
<td ><?php echo $rows['name']; ?></td>
<td ><?php echo $rows['phone_number']; ?></td>
</tr>
<?php $row_count++;
}
if ($row_count < 5){
for ($i=1; $i <= (5-$row_count); $i++){
?>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<?php
}
}
?>
You appear to have two distinct problems (albeit related).
If you only want five rows even if your query returns twenty, you can either use the control variable $row_count to only output rows for the first five, or (preferably) just add limit 5 on to your query to get five rows or less.
The second problem is what to do if it returns less than five rows. In that case, use the control variable to output blank rows, something like adding the following to the end:
<?php
while ($row_count < 5) {
?>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<?php
$row_count++;
}
?>
while($row = mysql_fetch_assoc($select2)){
?>
<tr>
<td ><?php echo $row_count;?>.</td>
<td ><?php echo $rows['id']; ?></td>
<td ><?php echo $rows['name']; ?></td>
<td ><?php echo $rows['phone_number']; ?></td>
</tr> <?php }
if($select3<5){
for($i=1;$i<5-$select3;$i++){?>
<tr>
<td ></td>
<td ></td>
<td ></td>
<td ></td>
</tr>
<?php }
}
It is very simple, but I cannot do it. Now it takes every information from my database and introduces it in the Nr. td. How can I increment the value with every row?
<?php while ($row=mysql_fetch_array($query)) { ?>
<tbody>
<tr>
<td (this is what I want to auto-increment)>
<?php
$i = 1;
foreach($row as $i){
$i++;
echo $i;
}
?>
</td>
<td width="120">
<?php echo $row['Name'];?>
</td>
</tr>
</tbody>
<?php } ?>
Thanks!
Try this:
Modify your code as given below:
<tbody>
<?php
$i = 1;
while ($row=mysql_fetch_array($query)) { ?>
<tr>
<td width="5">
<?php
echo $i;
$i++;
?>
</td>
<td width="120">
<?php echo $row['Name'];?>
</td>
</tr>
<?php }?>
</tbody>
Initialize $i before the while loop then echo in the td then increment it.
<?php
$i=1;
while ($row=mysql_fetch_array($query)) { ?>
<tbody>
<tr>
<td width="5">
<?php
echo $i;
$i++;
//You can join the above 2 lines into 1 like this:
// echo $i++;
// This way you're telling php, echo $i then increment it by 1
?>
</td>
<td width="120">
<?php echo $row['Name'];?>
</td>
</tr>
</tbody>
<?php } ?>
NB: if you're just learning PHP, you shouldn't be learning mysql_* as they are deprecated and will be removed in future releases. Check out mysqli or PDO.
initialize
$i = 1;
outside while loop
while ($row=mysql_fetch_array($query))
, so that it will increase.
Have your row counter ($rowNum) outside of the while
<?php
$rowNum = 1; ?>
<tbody>
<?php while ($row=mysql_fetch_array($query)) { ?>
<tr>
<td width="5">
<?php echo $rowNum; $rowNum++ ?>
</td>
<td width="120">
<?php echo $row['Name'];?>
</td>
</tr>
<?php } ?>
</tbody>
essentially I want to have two tables one with everyone with Confirmed = 1 and the other with Confirmed = 0.
I have this sql query at the moment
$result = mysqli_query($con,"SELECT * FROM tbl_booking WHERE $row[Confirmed] = 1");
but I keep getting
Notice: Undefined variable: row in C:\xampp\htdocs\test.php on line 19
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in
rest of the code for that page
echo "<table border='1'>
<tr>
<th>ID</th>
<th>BookID</th>
<th>date</th>
<th>time</th>
<th>confirmed?</th>
</tr>";
while($row = mysqli_fetch_array($result) )
{ ?>
<tr>
<td> <?php echo $row['tbl_mem_id']; ?> </td>
<td> <?php echo $row['BookID']; ?> </td>
<td> <?php echo $row['date']; ?> </td>
<td> <?php echo $row['time']; ?> </td>
<td> <?php echo $row['Confirmed']; ?> </td>
<td> <a href=delete.php?BookID=<?php echo $row['BookID']; ?> </a> Delete </td>
<td>
<?php
if ($row['Confirmed'] == 0) { ?>
<a href=confirm.php?BookID=<?php echo $row['BookID']; ?> </a> Confirm </td> <?php } else { ?>
<a href=deny.php?BookID=<?php echo $row['BookID']; ?> </a> Deny </td> <?php
} ?>
</tr> <?php
} ?>
</table>
Are you really trying to put the value in $row['Confirmed'] in the query, or are you just trying to query a field named Confirmed? if its the latter try this:
$result = mysqli_query($con,"SELECT * FROM tbl_booking WHERE Confirmed= 1");
but since also you seem to differentiate the table in php with if ($row['Confirmed'] == 0) you probably need to get all data in one query with this:
$result = mysqli_query($con,"SELECT * FROM tbl_booking");
this won't give you 2 tables as you initially asked for though.
you can create 2 html tables with this:
<?php
$result = mysqli_query($con,"SELECT * FROM tbl_booking WHERE Confirmed = 1");
echo "<table border='1'>
<tr>
<th>ID</th>
<th>BookID</th>
<th>date</th>
<th>time</th>
<th>confirmed?</th>
</tr>";
while($row = mysqli_fetch_array($result) )
{ ?>
<tr>
<td> <?php echo $row['tbl_mem_id']; ?> </td>
<td> <?php echo $row['BookID']; ?> </td>
<td> <?php echo $row['date']; ?> </td>
<td> <?php echo $row['time']; ?> </td>
<td> <?php echo $row['Confirmed']; ?> </td>
<td> <a href=delete.php?BookID=<?php echo $row['BookID']; ?> </a> Delete </td>
<td>
<a href=deny.php?BookID=<?php echo $row['BookID']; ?> </a> Deny </td>
</tr> <?php
} ?>
</table>
<?php
echo "<table border='1'>
<tr>
<th>ID</th>
<th>BookID</th>
<th>date</th>
<th>time</th>
<th>confirmed?</th>
</tr>";
$result = mysqli_query($con,"SELECT * FROM tbl_booking WHERE Confirmed = 0 ");
while($row = mysqli_fetch_array($result) )
{ ?>
<tr>
<td> <?php echo $row['tbl_mem_id']; ?> </td>
<td> <?php echo $row['BookID']; ?> </td>
<td> <?php echo $row['date']; ?> </td>
<td> <?php echo $row['time']; ?> </td>
<td> <?php echo $row['Confirmed']; ?> </td>
<td> <a href=delete.php?BookID=<?php echo $row['BookID']; ?> </a> Delete </td>
<td>
<a href=confirm.php?BookID=<?php echo $row['BookID']; ?> </a> Confirm </td>
</tr> <?php
} ?>
</table>