PHP html logic error If condition works backwards - php

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

Related

Codeigniter Print Domp PDF Page Break Limit 5 records per page In table foreach

I want to show only 5 records data per page in print pdf. This is my code :
<table border="1" width="100%" cellpadding="10">
<thead>
<tr>
<th>No</th>
<th>Part No</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<?php
$i=1;
foreach($items as $row):
?>
<tr>
<td><?php echo $i++ ?></td>
<td><?php echo $row->part_no?></td>
<td><?php echo $row->price ?></td>
</tr>
<?php if ($i % 5 === 1): ?>
<p style="page-break-before: always;"></p>
<?php endif; ?>
<?php endforeach; ?>
</tbody>
<tfoot>
<tr>
<td></td>
<td></td>
<td><?php echo $total ?></td>
</tr>
</tfoot>
</table>
Data is show correctly but any zero in my table in second and next page, like this image below :
how to use the code below correctly, ?
<?php if ($i % 5 === 1): ?>
<p style="page-break-before: always;"></p>
<?php endif; ?>
$array = array(1,2,3,4,5,6,7,8,9,10,11,12);
foreach ($array as $item){
if ($item == 5) {
break;
}
echo $item;
}
I have solved this, my table is look good now. Thank you :
I changed code like this :
<tbody>
<?php
$i=1;
foreach($items as $row):
?>
<tr>
<td><?php echo $i++ ?></td>
<td><?php echo $row->part_no?></td>
<?php if ($i % 5 == 1) {
echo '<tr><td><div style="page-break-before: always;"></div></td></tr>';
}?>
</tr>
<?php endforeach; ?>
</tbody>

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 table. Show total before the new group

I am having a problem about showing total in groups.
Here is my scenario, I have a report grouped by area and by product.
What I already have is the row for area group.
What I want to do is to show the total qty of product per area before the row for the next group. Currently, it shows the total after every row.
Here is my code.
<?php if (isset($summaryPerArea)): ?>
<div class="col-lg-12">
<table id="" class="table table-bordered table-condensed table-striped">
<thead>
<tr>
<th>Area</th>
<th>Material</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
<?php
$prevArea = '';
$total = 0;
$currentQty = 0;
?>
<?php foreach ($summaryPerArea as $key => $value): ?>
<?php $currentQty = $value['totalQty']; ?>
<?php $total += $value['totalQty']; ?>
<?php if ($value['area'] != $prevArea): ?>
<tr class="bg-info">
<?php if ($key != 0) {$total = $currentQty;} ?>
<td colspan="3"><?php echo $value['area']; ?></td>
<?php $prevArea = $value['area']; ?>
</tr>
<?php endif; ?>
<tr>
<td><?php echo $value['area']; ?></td>
<td><?php echo $value['material']; ?></td>
<td><?php echo $value['totalQty']; ?></td>
</tr>
<?php if ($value['area'] == $prevArea): ?>
<tr class="bg-success">
<td colspan="3"><?php echo $total; ?></td>
</tr>
<?php endif; ?>
<?php endforeach ?>
</tbody>
</table>
</div>
<?php endif ?>
query:
SELECT d.SOffcNm as area,
c.ProdNm as material,
SUM(Qty) as totalQty
FROM BigEMerchandiser.dbo.tbl_Delivery_H as a
INNER JOIN BigEMerchandiser.dbo.tbl_Delivery_D as b
ON a.TransCtr = b.TransCtr
INNER JOIN BigESales.dbo.tbl_Materials as c
ON b.Material = c.ExtMatGrp
INNER JOIN BigESales.dbo.tbl_Customers as d
ON a.CustCode = d.CustCode
WHERE d.SOffc LIKE ISNULL('%' + #area + '%', d.SOffc)
AND a.DtRcv BETWEEN #DtRcvFrom AND #DtRcvTo
GROUP BY d.SOffcNm,
c.ProdNm
ORDER BY d.SOffcNm asc
current result:
Thankyou. I appreciate your help.
try to change your code according to the comments in the code below (only the foreach part)
<?php foreach ($summaryPerArea as $key => $value): ?>
<?php if ($value['area'] != $prevArea and $prevArea): // show total when changing area from second area onward ?>
<tr class="bg-success">
<td colspan="3"><?php echo $total; ?></td>
</tr>
<?php $total = 0; // reset the total after displaying it ?>
<?php endif; ?>
<?php if ($value['area'] != $prevArea):// show area header at every area change ?>
<tr class="bg-info">
<td colspan="3"><?php echo $value['area']; ?></td>
</tr>
<?php endif; ?>
<?php //$currentQty = $value['totalQty']; // does not needed ?>
<?php $total += $value['totalQty']; ?>
<tr>
<td><?php echo $value['area']; ?></td>
<td><?php echo $value['material']; ?></td>
<td><?php echo $value['totalQty']; ?></td>
</tr>
<?php $prevArea = $value['area']; // set prevArea to the processed row ?>
<?php endforeach ?>
<?php // show the last total ?>
<tr class="bg-success">
<td colspan="3"><?php echo $total; ?></td>
</tr>
notice that the order of the rows are repeated as follows:
--area total--
--area header--
--area item--
and followed by:
--area total--
on the first foreach, prevArea is still '' so the first condition in the --area total-- (and $prevArea) would result in false, so that the --area total-- is suppressed, but the --area header-- does not have that condition, so the --area header-- is not suppressed.
I use a different syntax of php, using if(condition){code};
If I understood your problem, I make a different solution, I controll if the next is different from the value that i use now, because if it was different you need to print.
I rewrite your code like this:
<tbody>
<?php
$prevArea = 'AnElementLikeFlag';
$total = 0;
$currentQty = 0;
?>
<?php foreach ($summaryPerArea as $key => $value): ?>
<?php $currentQty = $value['totalQty']; ?>
<?php if ($value['area'] != $prevArea && $value['area']!="AnElementLikeFlag"): ?>
<tr class="bg-success">
<td colspan="3"><?php echo $total; ?></td>
</tr>
<tr class="bg-info">
<td colspan="3"><?php echo $value['area']; ?></td>
<?php $prevArea = $value['area']; ?>
</tr>
<?php $total=0; ?>
<?php endif; ?>
<?php $total += $value['totalQty']; ?>
<tr>
<td><?php echo $value['area']; ?></td>
<td><?php echo $value['material']; ?></td>
<td><?php echo $value['totalQty']; ?></td>
</tr>
<?php endforeach ?>
</tbody>
I hope I help you and I maybe make an error.
P.S. You print the total every time, because there is a if condition that it is not necessery

Get the total of variables in active records

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

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