Dynamic rowspan table in PHP - php

I have a problem getting a table to work.
I want to adjust a printable form for order list (opencart),
but I want address box on left and products ordered on right.
The problem is the number of products varies per order, so using rowspan on the address box does not work (I left the rowspan in the code).
<thead>
<tr>
<td><b><?php echo $text_to; ?></b></td>
<td><b><?php echo $column_product; ?></b></td>
<td class="text-center"><b><?php echo $column_quantity; ?></b></td>
</tr>
</thead>
<tbody><tr>
<td rowspan="10"><?php echo $order['shipping_address']; ?><br/><br/>
<?php echo $order['telephone']; ?>
<br />
<?php if ($order['shipping_method']) { ?>
<b><?php echo $text_shipping_method; ?></b> <?php echo $order['shipping_method']; ?><br />
<?php } ?></td></tr>
<?php foreach ($order['product'] as $product) { ?>
<td><?php echo $product['name']; ?>
<?php foreach ($product['option'] as $option) { ?>
<br />
<small> - <?php echo $option['name']; ?>: <?php echo $option['value']; ?></small>
<?php } ?></td>
<td class="text-center"><?php echo $product['quantity']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>

You have all the information available it seems so you just need to change:
<td rowspan="10">
to:
<td rowspan="<?php echo count($order['product']); ?>">
However, you do need to check the generated html as it looks like you are not generating valid rows so you might need to change the logic a bit.

Related

using variable from array outside of foreach loop

Im trying to display variables from an array. I am using a foreach loop, however I need to display $order['campaign_name'] before the loop so that it only shows up once. How can I do this? If I change it to $orders['campaign_name'] I get an undefined index error.
<div class="table-responsive">
<table class="table" id="component-table">
<?php if ($orders) { ?>
<?php foreach ($orders as $order) { ?>
<thead>
<tr>
<td colspan=100%><h3><?php echo $order['campaign_name']; ?></h3></td>
</tr>
</thead>
<tbody>
<tr class="campaign-list" id="campaign-list">
<td><?php echo $order['component_name']; ?></td>
<td><?php echo $order['component_owner']; ?></td>
<td><?php echo $order['component_date']; ?></td>
<td><?php echo $order['campaign_code']; ?></td>
</tr>
<?php } ?>
<?php } else { ?>
<tr>
<td class="text-center" colspan="8"><?php echo $text_no_results; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
You are trying to get those value which is not exist before loop. You are directly calling VALUE.
Easily just put index value behind tha array
echo $orders[0]['campaign_name']
It will print your value.
You would need need to know which index of the array you want to show, but if you want to show the first index of the array you can use $orders[0]['component_name'].
<div class="table-responsive">
<table class="table" id="component-table">
<?php if ($orders) { ?>
<thead>
<tr>
<td colspan=100%><h3><?php echo $orders[0]['campaign_name']; ?></h3></td>
</tr>
</thead>
<tbody>
<?php foreach ($orders as $order) { ?>
<tr class="campaign-list" id="campaign-list">
<td><?php echo $order['component_name']; ?></td>
<td><?php echo $order['component_owner']; ?></td>
<td><?php echo $order['component_date']; ?></td>
<td><?php echo $order['campaign_code']; ?></td>
</tr>
<?php } ?>
</tbody>
<?php } else { ?>
<tbody>
<tr>
<td class="text-center" colspan="8"><?php echo $text_no_results; ?></td>
</tr>
</tbody>
<?php } ?>
</table>
</div>
Your campaign name continues to echo out because you have it inside of your foreach loop; if you want it to display only once, make sure to place it above your foreach loop.
Here's a less cluttered example to learn from:
<table>
...
<?php
if ($orders):
echo $order['campaign_name'];
foreach($orders as $order):
echo '...'; // other components
endforeach;
else:
echo $text_no_results;
endif;
?>
...
</table>

merge two foreach in one layout

i got two arrays.
from results i want to make a table with foreach
but i dont know how to make it work in one table row..
this is what i got
<table>
<?php foreach ($appky as $appka) : ?>
<tr class="counter_apps" height="20px" >
<td width="40%"><?php echo $appka->name; ?></td>
<td width="20%"><?php echo $appka->all_items;?></td>
<td width="20%"><?php echo $appka->published; ?></td>
<td width="20%"><?php echo $appka->unpublished; ?></td>
</tr>
<?php endforeach; ?>
</table>
<table>
<?php foreach ($applications as $application) : ?>
<tr><td><?php echo $application->name; ?></td></tr>
<?php endforeach; ?>
</table>
so what i want is simply add to the first table another column with $application->name;
what i'm missing here??
thanks
<table>
<?php
$count = count($appky);
for($i=0; $i< $count; $i++) { ?>
<tr class="counter_apps" height="20px" >
<td width="40%"><?php echo $appky[$i]->name; ?></td>
<td width="20%"><?php echo $appky[$i]->all_items;?></td>
<td width="20%"><?php echo $appky[$i]->published; ?></td>
<td width="20%"><?php echo $appky[$i]->unpublished; ?></td>
<td width="20%"><?php echo $applications[$i]->name; ?></td>
</tr>
<?php } ?>
</table>
use for instead of foreach
<table>
<?php for($i=0 ; $i<count($appky) ; $i++ ) { ?>
<tr class="counter_apps" height="20px" >
<td width="40%"><?php echo $appka[$i]->name; ?></td>
<td width="20%"><?php echo $appka[$i]->all_items;?></td>
<td width="20%"><?php echo $appka[$i]->published; ?></td>
<td width="20%"><?php echo $appka[$i]->unpublished; ?></td>
<td width="20%"><?php echo isset($applications[$i]) ? $applications[$i]->name : '' ; ?></td>
</tr>
<?php } ?>
</table>
Assuming your two arrays aren't necessarily in order - If you have some data that matches between your $appka and $application you can try something like this below:
<table>
<?php foreach ($appky as $appka) : ?>
<tr class="counter_apps" height="20px" >
<td width="40%"><?php echo $appka->name; ?></td>
<td width="20%"><?php echo $appka->all_items;?></td>
<td width="20%"><?php echo $appka->published; ?></td>
<td width="20%"><?php echo $appka->unpublished; ?></td>
<?php foreach ($applications as $application) : ?>
<?php if ($appka->id == $application->id) { ?>
<td><?php echo $application->name; ?></td>
<?php } ?>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
Otherwise you wont be able to spit out the correct application name. The above solution uses an id field, but you probably hove something else you can match on.

dompdf page break with codeigniter and foreach loop

I am using codeigniter and dompdf to generate a pdf document.
the document is generated with a foreach loop. each loop creates a few table rows. I want to have a pagebreak between the rows of each loop.
my relevent codeigniter view syntax is:
<table width=100%>
<?php foreach($summary as $summary){ ?>
<tr class="firstrow">
<td colspan="10">
<?php echo "<b>Customer</b>: <font color='blue'>".$summary->caccountname; ?>
<?php echo "</font> <b>Order Number</b>: <font color='blue'>".$summary->OrderNum; ?>
<?php echo "</font> <b>Sales rep</b>: <font color='blue'>".$summary->RepName; ?>
<?php echo "</font> <b>Due Date</b>: <font color='blue'>".substr($summary->DueDate,0,10); ?>
</td>
</tr>
<tr>
<td colspan="10"><hr>
</td>
</tr>
<tr>
<td colspan="10">
<table class="sortable" width="90%">
<tr><td ><b>Product</td><td ><b>Ordered</td><td ><b>Processed</td><td ><b>Outstanding</td><td ><b>Dispatched</td><td ><b>Available (incl FJ)</td><td ><b>Available FJ</td></tr>
<?php foreach($$linedetails as $linedetails){ ?>
<tr>
<td><?php echo $linedetails->Product; ?></td>
<td><?php echo $linedetails->cubicvolume; ?></td>
<td><?php echo $linedetails->Processed; ?></td>
<td><?php echo $linedetails->Outstanding; ?></td>
<td><?php echo $linedetails->Dispatched; ?></td>
<td><?php echo $linedetails->TotalVolume; ?></td>
<td><?php echo $linedetails->FJVolume; ?></td>
</tr>
<?php } ?>
</table>
</td>
</tr>
<?php } ?>
</table>
I have given the first tr a class of firstrow, I would like a page break to appear before this line every time.
As such I have applied the following style sheet in my page header:
<style>
#media print
{
.firstrow {page-break-before:always}
}
</style>
This doesnt work.
How can I acheive my desired result of placeing a page break before each loop firstrow TR?
Thanks as always
To make Pagebreak work with tr, write a line of css for tr
tr { page-break-inside:avoid; page-break-after:auto }
Try above code.
If not working, then add
table { page-break-inside:auto }
Try this, I am doing reports exactly same way
<?php
$nbsp5 = " ";
foreach($summary as $summary) { ?>
<table width="100%" style="page-break-after:always;">
<tr>
<td colspan="10">
<?php echo "<b>Customer</b>: <font color='blue'>".$summary->caccountname; ?>
<?php echo "</font>".$nbsp5 ." <b>Order Number</b>: <font color='blue'>".$summary->OrderNum; ?>
<?php echo "</font>".$nbsp5 ." <b>Sales rep</b>: <font color='blue'>".$summary->RepName; ?>
<?php echo "</font>".$nbsp5 ." <b>Due Date</b>: <font color='blue'>".substr($summary->DueDate,0,10); ?>
</td>
</tr>
<tr>
<td colspan="10"><hr></td>
</tr>
<tr>
<td colspan="10">
<table class="sortable" width="90%">
<tr>
<td><b>Product</b></td>
<td><b>Ordered</b></td>
<td><b>Processed</b></td>
<td><b>Outstanding</b></td>
<td><b>Dispatched</b></td>
<td><b>Available (incl FJ)</b></td>
<td><b>Available FJ</b></td>
</tr>
<?php foreach($$linedetails as $linedetails){ ?>
<tr>
<td><?php echo $linedetails->Product; ?></td>
<td><?php echo $linedetails->cubicvolume; ?></td>
<td><?php echo $linedetails->Processed; ?></td>
<td><?php echo $linedetails->Outstanding; ?></td>
<td><?php echo $linedetails->Dispatched; ?></td>
<td><?php echo $linedetails->TotalVolume; ?></td>
<td><?php echo $linedetails->FJVolume; ?></td>
</tr>
<?php } ?>
</table>
</td>
</tr>
</table>
<?php } ?>
(first <table> tag moved inside loop)
Please try this answer for both header and footer.

Magento customer dashboard tracking number

I'm editing the template file for the customer dashboard at sales/order/recent.phtml. I want the tracking number along with a tracking link to show up under the recent order. I have tried the following but getAllTracks is not grabbing anything. Thoughts?
<?php foreach ($_orders as $_order): ?>
<tr>
<td><?php echo $_order->getRealOrderId() ?></td>
<td><span class="nobr"><?php echo $this->formatDate($_order->getCreatedAtStoreDate()) ?></span></td>
<td><?php echo $_order->getShippingAddress() ? $this->htmlEscape($_order->getShippingAddress()->getName()) : ' ' ?></td>
<td><?php echo $_order->formatPrice($_order->getGrandTotal()) ?></td>
<td><em><?php echo $_order->getStatusLabel() ?></em></td>
<td class="a-center">
<span class="nobr">
<?php echo $this->__('View Order') ?>
<?php if ($this->helper('sales/reorder')->canReorder($_order)) : ?>
<span class="separator">|</span> <?php echo $this->__('Reorder') ?>
<?php endif ?>
</span>
</td>
</tr>
<tr>
<?php $collection = Mage::getResourceModel('sales/order_shipment_collection');
$collection->addAttributeToFilter('order_id', $_order->getRealOrderId()); ?>
<?php foreach($collection as $_ship): ?>
<?php var_dump($_ship->getAllTracks())?>
<?php $i=0; foreach ($_ship->getAllTracks() as $_item): $i++ ?>
<?php $url = $this->helper('shipping')->getTrackingPopupUrlBySalesModel($_order) ?>
<?php echo "URL is". $url ?>
<?php if ($url): ?>
<td align="center" valign="top" style="padding:3px 9px"><?php echo $_item->getNumber() ?></td>
<?php else: ?>
<td align="center" valign="top" style="padding:3px 9px"><?php echo $_item->getNumber() ?></td>
<?php endif; ?>
<?php endforeach ?>
<!---Track --->
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
Ended up being a really short line of code to put the link in there. I didn't bother to keep working at getting the tracking number. I put the link in the second to last column.
<div class="box-account box-recent">
<?php $_orders = $this->getOrders(); ?>
<div class="box-head">
<h2><?php echo $this->__('Recent Orders') ?></h2>
<?php if( sizeof($_orders->getItems()) > 0 ): ?><?php echo $this->__('View All') ?><?php endif; ?>
</div>
<?php if( sizeof($_orders->getItems()) > 0 ): ?>
<table class="data-table" id="my-orders-table">
<col width="1" />
<col width="1" />
<col />
<col width="1" />
<col width="1" />
<col width="1" />
<thead>
<tr>
<th><?php echo $this->__('Order #') ?></th>
<th><?php echo $this->__('Date') ?></th>
<th><?php echo $this->__('Ship To') ?></th>
<th><span class="nobr"><?php echo $this->__('Order Total') ?></span></th>
<th><?php echo $this->__('Status') ?></th>
<th><?php echo $this->__('Track') ?></th>
<th> </th>
</tr>
</thead>
<tbody>
<?php foreach ($_orders as $_order): ?>
<tr>
<td><?php echo $_order->getRealOrderId() ?></td>
<td><span class="nobr"><?php echo $this->formatDate($_order->getCreatedAtStoreDate()) ?></span></td>
<td><?php echo $_order->getShippingAddress() ? $this->htmlEscape($_order->getShippingAddress()->getName()) : ' ' ?></td>
<td><?php echo $_order->formatPrice($_order->getGrandTotal()) ?></td>
<td><em><?php echo $_order->getStatusLabel() ?></em></td>
<td>
<?php if ($_order->getTracksCollection()->count()) : ?>
<?php echo $this->__('Track Order Shipment') ?>
<br/>
<?php endif; ?>
</td>
<td class="a-center">
<span class="nobr">
<?php echo $this->__('View Order') ?>
<?php if ($this->helper('sales/reorder')->canReorder($_order)) : ?>
<span class="separator">|</span> <?php echo $this->__('Reorder') ?>
<?php endif ?>
</span>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<script type="text/javascript">decorateTable('my-orders-table')</script>
<?php else: ?>
<p><?php echo $this->__('You have placed no orders.'); ?></p>
<?php endif; ?>
</div>

Checking the value of all a loops value

I have this loop in PHP that echoes out a table of results,
<table cellspacing="0" cellpadding="3">
<tr>
<td><b>Content Image Title</b></td>
<td><b>Content Image Type</b></td>
<td><b>Headline Image</b></td>
<td><b>Content Image Belongs To</b></td>
<td><b>Date Created</b></td>
<!--<td><b>Uploaded By</b></td>-->
</tr>
<?php $colours = array("#f9f9f9", "#f3f3f3"); $count = 0;?>
<?php foreach ($allContentImages as $contentImages) : ?>
<tr bgcolor="<?php echo $colours[$count++ % count($colours)];?>">
<td><?php echo "<a href='#' class='screenshot' rel='/media/uploads/$contentImages[categoryId]/$contentImages[contentImageName]'>".$contentImages['contentImageName']; ?></td>
<td><?php echo $contentImages['contentImageType']; ?></td>
<td><?php if($contentImages['isHeadlineImage'] == 1){ echo "Y";}else{echo "N";} ?></td>
<td><?php echo $contentImages['contentTitle'] ?></td>
<td><?php echo date("d-m-Y", $contentImages['contentImageDateUploaded']); ?></td>
<td align="left"><a class="delete" href="<?php echo base_url();?>dashboard/deleteContentImage/<?php echo $contentImages['contentImageId'];?>"><img src="/media/images/icons/cancel.png" alt="Delete A Category"/></a></td>
</tr>
<?php
if($contentImages['isHeadlineImage'] == '0') {
echo "<tr bgcolor='red'>";
echo "<td><p>You need to assign a headline image</p></td>";
echo "</tr>";
?>
<?php endforeach; ?>
</table>
I need to check for each content piece with the same title that there is headline image, and if not then echo a new row that is red...but all I get is a new row every time there is an image that is not a headline image. Can anyone help me? I don't mind using javascript if that helps to match the values of the td's? But obviously my attempt is not correct.
It sounds like what you want to do has to be precalculated, since the foreach loop can not know about future content.
Maybe something like this before your foreach statement:
$contentHasHeadlineImage = array();
foreach ($allContentImages as $contentImages) {
if ( $contentImages['isHeadlineImage'] == 1)
$contentHasHeadlineImage[ $contentImages['contentTitle'] ] = true;
}
And then you can use
if (array_key_exists($contentImages['contentTitle'], $contentHasHeadlineImage)) {
// Has headline image...
}
to to verify if a certain title has a headline.
Try my code below:
<table cellspacing="0" cellpadding="3">
<tr>
<td><b>Content Image Title</b></td>
<td><b>Content Image Type</b></td>
<td><b>Headline Image</b></td>
<td><b>Content Image Belongs To</b></td>
<td><b>Date Created</b></td>
<td>Action</td>
</tr>
<?php
$colours = array("#f9f9f9", "#f3f3f3");
$num_colours = count($colours);
$i = 0;
?>
<?php foreach ($allContentImages as $row) : ?>
<tr bgcolor="<?php echo $colours[($i++) % $num_colours]; ?>">
<td><a href="#" class="screenshot"
rel="/media/uploads/<?php echo $row['categoryId']; ?>/<?php echo $row['contentImageName']; ?>">
<?php echo $row['contentImageName']; ?>
</a></td>
<td><?php echo $row['contentImageType']; ?></td>
<td><?php echo $row['isHeadlineImage'] == 1 ? "Y" : "N"; ?></td>
<td><?php echo $row['contentTitle']; ?></td>
<td><?php echo date("d-m-Y", $row['contentImageDateUploaded']); ?></td>
<td align="left"><a class="delete"
href="<?php echo base_url(); ?>dashboard/deleteContentImage/<?php echo $row['contentImageId'];?>">
<img src="/media/images/icons/cancel.png" alt="Delete A Category" />
</a></td>
</tr>
<?php if ( $row['isHeadlineImage'] == 0 ): ?>
<tr bgcolor="red">
<td colspan="6"><p>You need to assign a headline image</p></td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
</table>
While tidying your code, I found an unclosed a href in the first column. For the special extra row, I add colspan. I assume $row['isHeadlineImage'] have value only 1 or 0 (integer or boolean).
I also found out that you access array key without using quote. It can become potential bug in the future.
Write the code in tidy indentation and consistent when using block or echoing the php variable will help you found the bug quickly. Also, inspect the result in browser using Firebug in firefox or Web Inspector in Safari and Google Chrome to see if the page structure is like you want to have, all the tag is balanced and closed in right place.

Categories