Get the total of variables in active records - php

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;?>

Related

For loop leaves blank td in a table

I am calling a for loop to get data from database with this code which is working fine but only with one problem
<table class="table" style="color:#000 !important;">
<tbody>
<tr>
<th>Question</th>
<th>User Answer</th>
</tr>
<tr>
</<?php
for ($x = 0; $x <= 30; $x++) {
?> <td><?= $info['ans_' . $x]; ?></td>
</tr>
<tr>
<?php
$sales_quiz = mysqli_query($conn_quiz, "SELECT * FROM `sales_quiz` where `que_no` = '$x'");
$sales_que = mysqli_fetch_array($sales_quiz); ?>
<td><?= $sales_que['que']; ?></td>
<?php }; ?>
</tr>
</tbody>
</table>
The result is first td of 2nd data which is que is blank and for loop start counting it from 2nd row
I do create a separate for loop for question and answer but then the table becomes broken.
If you need new rows in a table for every loop then you need to add the tr within the loop.
Also all tds that need to be on the same row has to be within one tr.
So remove the extra <tr> and bring them inside the loop.
<table class="table"
style="color:#000 !important;">
<tbody>
<tr>
<th>Question</th>
<th>User Answer</th>
</tr>
<?php
for ($x = 0; $x <= 30; $x++) {
?>
<tr>
<td><?= $info['ans_' . $x]; ?></td>
<!-- </tr> Remove these-->
<!-- <tr>-->
<?php
$sales_quiz = mysqli_query($conn_quiz, "SELECT * FROM `sales_quiz` where `que_no` = '$x'");
$sales_que = mysqli_fetch_array($sales_quiz);
?>
<td><?= $sales_que['que']; ?></td>
</tr>
<?php
} # the semi-colon (;) at the end is unnecessary
?>
</tbody>
</table>
You need to move your </tr><tr> to after the last td, and probably swap your $info['ans_'.$x]; and $sales_que['que']; since it seems like it should go question then answer, and the variable names make me think you are doing answer then question.
<table class="table" style="color:#000 !important;">
<tbody>
<tr>
<th>Question</th>
<th>User Answer</th>
</tr>
<tr>
<?php
for ($x = 0; $x <= 30; $x++) {
?> <td><?= $sales_que['que']; ?></td>
<?php
$sales_quiz = mysqli_query($conn_quiz, "SELECT * FROM `sales_quiz` where `que_no` = '$x'");
$sales_que = mysqli_fetch_array($sales_quiz); ?>
<td><?= $info['ans_'.$x]; ?></td>
</tr>
<tr>
<?php }; ?>
</tr>
</tbody>
</table>
I think it might be the "</" in the 10th line of your code, before the first <?php . There is any reason for that "</" to be there? From my perspective that creates a empty <tr></tr> on the resultant html file, and thats why you have that empty cell on your table. I might be wrong, just check it out and tell me if it works.

Showing data in table row wise by using codeigniter php

How can i show data in two row separately as shown in image
I have tried this but i am not getting what i want. Also i don't want to use two loops separately.
<table class="tbl1">
</thead>
<tbody>
<tr>
<td>
<h1> date</h1> </td>
<?php $i=1; foreach ($student as $value) { $i++;?>
<td>
<?php echo $value[ 'date']; ?>
<?php } ?>
</tr>
<tr>
<td>
<h1>Status</h1> </td>
<?php $i=1; foreach ($student as $value) { $i++; ?>
<td>
<?php echo $value[ 'status']; ?>
</td>
<?php } ?>
</tr>
</tr>
</tbody>
</table>
Do a single loop and in that loop include 2 <tr>s for date and status.
<table class="tbl1">
<tbody>
<?php foreach ($student as $value) { ?>
<tr>
<td><h1> date</h1> </td>
<td><?php echo $value['date']; ?>
</tr>
<tr>
<td><h1>Status</h1></td>
<td><?php echo $value['status']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
Also i don't see the purpose of using $i here and what about the rest of the column in front of date and status.
Or if you want only 2 rows and display all data column wise then you could do it like
<?php
$dateHTML = '';
$statusHTML = '';
foreach ($student as $value) {
$dateHTML .= '<td>'.$value['date'].'</td>';
$statusHTML .= '<td>'.$value['status'].'</td>';
}
?>
<table class="tbl1">
<tbody>
<tr>
<td><h1> date</h1> </td>
<?php echo $dateHTML;?>
</tr>
<tr>
<td><h1>Status</h1></td>
<?php echo $statusHTML;?>
</tr>
</tbody>
</table>

PHP html logic error If condition works backwards

I have a form where I'm using a nested pair of foreach loops. I can't figure out why the continue statement executes when the condition is false instead of true: Here's the code I'm using
<?php $array =[9,10,11,12,13,14,15,16,17,18,19,20,21] ; $i = 0;?>
<?php foreach ($reservations as $reservation): ?>
<table class="tabsize1" style ="margin-top:0%;margin-bottom:0%;">
<tbody>
<tr >
<td class="nextsail"><?=$reservation["date"] ?></td>
<td class="nextsail"><?=$reservation["boat"] ?></td>
<td class="nextsail"><?=$reservation["start_res"]?></td>
<td class="nextsail"><?=$reservation["end_res"] ?></td>
</tr>
</tbody>
</table>
<?php if($weather[$i]['cnt'] == 24) continue ; ?> <!-- this works if i use != but $weather[$i]['cnt'] = 24 -->
<table >
<tbody>
<tr >
<?php foreach ($array as $value): ?>
<td> <?= $weather[$i][$value]['HOUR']?></td>
<?php endforeach ?>
</tr>
</tbody>
</table>
<?php $i++; ?>
<?php endforeach ?>
You are missing ; after endforeach

How do you increment a number with every table row?

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>

Last loop in CodeIgniter foreach

I'm trying to catch the last loop in my CodeIgniter for-loop. I'm trying to generate a < hr /> under every news item but the last one. This is my code:
<table class="news">
<?php foreach($news as $news_item): ?>
<tr>
<td class="headline"><?php echo $news_item['title']; ?></td>
</tr>
<tr>
<td class="text"><?php echo $news_item['text']; ?></td>
</tr>
<tr>
<td><hr /></td>
</tr>
<?php endforeach; ?>
</table>
Уou can try something like this:
<?php for($i = 0, $lastIDX = count($news)-1; $i<=$lastIDX; $i++): ?>
<!-- html code ... $news_item is $news[$i] now -->
<? if ($i !== $lastIDX) : ?>
<tr>
<td><hr /></td>
</tr>
<?php endif; ?>
<?php endfor; ?>
But it would be better to use CSS :last-child selector.
You can use this one as a solution:
$last = end($news);
<table class="news">
<?php foreach($news as $news_item): ?>
<tr>
<td class="headline"><?php echo $news_item['title']; ?></td>
</tr>
<tr>
<td class="text"><?php echo $news_item['text']; ?></td>
</tr>
<tr>
<td><?=(($last == $news_item)?"":"<hr />")?></td>
</tr>
<?php endforeach; ?>
</table>
Where the $last is the last array of your array XD.
Get the last key of the array beforehand and compare it the current one while looping:
$keys = array_keys($news);
$last_key = end($keys);
foreach ($news as $news_key => $news_item) {
if ($news_key == $last_key) {
// at the last item
} else {
// not at the last item
}
}
Also you could count the elements in your array beforehand, and have a counter inside the loop that increments on every round, so you can tell if you are at the end or not.

Categories