foreach loop creates a new table for each entry - php

What am I missing here? It always produces new tables instead of one table where all datasets are in.
foreach($results as $print) { ?>
<table class='wp-list-table widefat striped'>
<thead>
<tr> <!-- display table header -->
<th style='text-align:center;' width=<?php echo $width[0]; ?>><?php echo $character[0]; ?></th>
<th style='text-align:center;' width=<?php echo $width[1]; ?>><?php echo $character[1]; ?></th>
</tr>
</thead>
<tbody>
<form action='' method='post'>
<tr> <!-- display data -->
<td width=<?php echo $width[0]; ?>><?php echo $print->postleitzahl; ?></td>
<td width=<?php echo $width[1]; ?>><?php echo $print->ort; ?></td>
</tr>
</form>
<?php } ?>
</tbody>
</table>
Can you find my mistake? Sorry but I can't see it.

I would do it this way:
<table class='wp-list-table widefat striped'>
<thead>
<tr> <!-- display table header -->
<th style='text-align:center;' width=<?php echo $width[0]; ?>><?php echo $character[0]; ?></th>
<th style='text-align:center;' width=<?php echo $width[1]; ?>><?php echo $character[1]; ?></th>
</tr>
</thead>
<tbody>
foreach($results as $print) { ?>
<form action='' method='post'>
<tr> <!-- display data -->
<td width=<?php echo $width[0]; ?>><?php echo $print->postleitzahl; ?></td>
<td width=<?php echo $width[1]; ?>><?php echo $print->ort; ?></td>
</tr>
</form>
<?php } ?>
</tbody>
</table>

Related

If row status is 1 show the approved status in the table

I want to output statement you are approve if the row status = 1, the data can be output now i just want the statement saying approve.
here's my code
<div class="row">
<div class="col-md-12">
<table class="table table-striped">
<tr>
<th>Nominee Name</th>
<th>Year</th>
<th>Department</th>
<th>StudentID</th>
<th>Position</th>
<th>View File</th>
<th>Nominee Photo</th>
<th>Nominated by</th>
<th>status</th>
</tr>
<?php
foreach ($nominees as $noms) {
?>
<tr>
<td><?php echo $noms['name']; ?></td>
<td><?php echo $noms['year']; ?></td>
<td><?php echo $noms['department']; ?></td>
<td><?php echo $noms['studentid']; ?></td>
<td><?php echo $noms['position']; ?></td>
<td></td>
<td><img src="<?php echo base_url(); ?>uploads/<?php echo $noms['photo']; ?>" style='width:50px;height:50px;'></td>
<td><?php echo $noms['nomname']; ?></td>
<?php if ($noms['approve'] == 1) { ?>
<td>Approve</td>
<?php } else { ?>
<td>pending</td>
<?php } ?>
</tr>
<?php } ?>
</table>
</div>
</div>
If the $nominees array returns the stays, then you can do like this
<?php if ( $nom['status'] == 1) { ?>
<td>Approve</td>
<?php }else{ ?>
<td>pending</td>
<?php }?>

One foreach loop and two arrays

I have a little issue with my foreach loop and can't find the mistake.
first array
$gemeinden = [$barsbek_G, $barsbek_U, $barsbek_F, $bendfeld_G, $bendfeld_U, $bendfeld_F, $brodersdorf_G, $brodersdorf_U, $brodersdorf_F, $fahren_G, $fahren_U, $fahren_F, $fiefbergen_G, $fiefbergen_U, $fiefbergen_F, $hoehndorf_G, $hoehndorf_U, $hoehndorf_F, $koehn_G, $koehn_U, $koehn_F, $krokau_G, $krokau_U, $krokau_F, $krummbek_G, $krummbek_U, $krummbek_F, $laboe_G, $laboe_U, $laboe_F, $lutterbek_G, $lutterbek_U, $lutterbek_F, $passade_G, $passade_U, $passade_F, $prasdorf_G, $prasdorf_U, $prasdorf_F, $probst_G, $probst_U, $probst_F, $schoenberg_G, $schoenberg_U, $schoenberg_F, $stakendorf_G, $stakendorf_U, $stakendorf_F, $stein_G, $stein_U, $stein_F, $stoltenberg_G, $stoltenberg_U, $stoltenberg_F, $wendtorf_G, $wendtorf_U, $wendtorf_F, $wisch_G, $wisch_U, $wisch_F];
second array
$results = $wpdb->get_results( $queryGemeinde );
That's my foreach loop
<?php foreach($results as $index => $print) { ?>
<tr> <!-- display data -->
<td width=<?php echo $width[0]; ?>><?php echo $print->postleitzahl; ?></td>
<td width=<?php echo $width[1]; ?>><?php echo $print->ort; ?></td>
<td width=<?php echo $width[1]; ?>><?php echo $gemeinden[$index]; ?></td>
<td width=<?php echo $width[1]; ?>><?php echo $gemeinden[$index]; ?></td>
<td width=<?php echo $width[1]; ?>><?php echo $gemeinden[$index]; ?></td>
</tr>
<?php }?>
</tbody>
</table>
That's the result
It seems as if it is stucked. And it makes sense but how Can I tell the foreach loop to go further after the third <td></td>
As per my understanding you need to fetch next item after 3rd td so you can use below approach:
<?php foreach($results as $index => $print) { ?>
<tr> <!-- display data -->
<td width=<?php echo $width[0]; ?>><?php echo $print->postleitzahl; ?></td>
<td width=<?php echo $width[1]; ?>><?php echo $print->ort; ?></td>
<td width=<?php echo $width[1]; ?>><?php echo $gemeinden[$index]; ?></td>
<?php $index++; ?>
<td width=<?php echo $width[1]; ?>><?php echo $gemeinden[$index]; ?></td>
<?php $index++; ?>
<td width=<?php echo $width[1]; ?>><?php echo $gemeinden[$index]; ?></td>
</tr>
<?php }?>
</tbody>
</table>
Or you can even iterate loop as below as well:
<?php foreach($results as $index => $print) { ?>
<tr> <!-- display data -->
<td width=<?php echo $width[0]; ?>><?php echo $print->postleitzahl; ?></td>
<td width=<?php echo $width[1]; ?>><?php echo $print->ort; ?></td>
<?php foreach($gemeinden as $gemValue) { ?>
<td width=<?php echo $width[1]; ?>><?php echo $gemValue; ?></td>
<?php } ?>
</tr>
<?php }?>
</tbody>
</table>
Hope it helps you!!

How to find the sum of column of a table in frontend

I want to calculate the sum of column on frontend and appear the sum in tfoot. Please help me out I'm new to PHP!
<?php
if(is_array($finalArr) && count($finalArr) > 0){
foreach($finalArr as $key => $finalAr){
?>
<tbody>
<tr>
<td> <?php echo $finalAr['date']; ?></td>
<td>$<?php echo $finalAr['deposit'] ?></td>
<td>$ <?php echo $finalAr['fees'] ?></td>
</tr>
<tfoot>
<tr>
<th>Total</th>
<th>$<?php echo 0; ?></th>
<th>$<?php echo 0; ?></th>
</tr>
</tfoot>
You can do like this:
<tbody>
<?php $totalFee = $totalDeposit = 0;
if(is_array($finalArr) && count($finalArr) > 0){
foreach($finalArr as $key => $finalAr){
$totalFee+= $finalAr['fees'];
$totalDeposit+= $finalAr['deposit'];
?>
<tr>
<td> <?php echo $finalAr['date']; ?></td>
<td>$<?php echo $finalAr['deposit'] ?></td>
<td>$ <?php echo $finalAr['fees'] ?></td>
</tr>
<?php }?>
<tfoot>
<tr>
<th>Total</th>
<th>$<?php echo $totalFee; ?></th>
<th>$<?php echo $totalDeposit; ?></th>
</tr>
</tfoot>
<?php } ?>
Hope this will help.

why I cannot Displaying data from view in MySQL database?

I've a view to displaying some data from database to my php program, but the data cannot be displayed when I running it?
this is my view from phpmyadmin look like
(select `m`.`id_mutasi` AS `id_mutasi`,`m`.`tgl_mutasi` AS `tgl_mutasi`,`m`.`kode_barang` AS `kode_barang`,`a`.`nm_barang` AS `nm_barang`,`m`.`ruang_lama` AS `ruang_lama`,`m`.`ruang_baru` AS `ruang_baru`,`u`.`nm_unit` AS `unit_lama`,`m`.`unit_baru` AS `unit_baru`,`i`.`user` AS `user_lama`,`m`.`userbaru` AS `user_baru`,`m`.`jumlah` AS `jumlah`,`m`.`user_posting` AS `user_posting` from ((((((`simaset`.`mutasi` `m` join `simaset`.`inventarisasi` `i` on((`m`.`kode_inventarisasi_baru` = `i`.`kode_inventarisasi`))) join `simaset`.`aset` `a` on((`a`.`kode_barang` = `m`.`kode_barang`))) join `simaset`.`ruangan` `r` on((`r`.`kode_ruangan` = `m`.`ruang_lama`))) join `simaset`.`ruangan` `rb` on((`rb`.`kode_ruangan` = `i`.`kode_ruangan`))) join `simaset`.`unit_kerja` `u` on((`u`.`kode_unit` = `m`.`unit_lama`))) join `simaset`.`unit_kerja` `ub` on((`ub`.`kode_unit` = `i`.`kode_unit`))))
and this is my php code
<table id="table-example" class="table" cellpadding="0" cellspacing="0" border="0">
<thead>
<tr>
<!-- <th>NO</th> -->
<th>ID Mutasi</th>
<th>Tgl Mutasi</th>
<th>Kode Barang</th>
<th>Barang</th>
<th>Dept Lama</th>
<th>Jabatan Lama</th>
<th>Dept Baru</th>
<th>Jabatan Baru</th>
<!-- <th>User Baru</th> -->
<th>Jumlah</th>
<th>User Posting</th>
<!-- <th>User Posting</th> -->
<th>Aksi</th>
</tr>
</thead>
<tbody>
<!-- <?php $no = 1;?> -->
<?php do { ?>
<tr class=gradeX>
<!-- <td><center><?php echo $no++ ?></center></td> -->
<td><center>
<?php echo $row_rs_data['id_mutasi']; ?>
</center></td>
<td><?php echo $row_rs_data['tgl_mutasi']; ?></td>
<td ><?php echo $row_rs_data['kode_barang']; ?></td>
<td ><?php echo $row_rs_data['nm_barang']; ?></td>
<td ><?php echo $row_rs_data['ruanglama']; ?></td>
<td ><?php echo $row_rs_data['unitlama']; ?></td>
<td ><?php echo $row_rs_data['ruangbaru']; ?></td>
<td ><?php echo $row_rs_data['unitbaru']; ?></td>
<!-- <td ><?php echo $row_rs_data['userbaru']; ?></td> -->
<td ><center><?php echo $row_rs_data['jumlah']; ?></center></td>
<td ><?php echo $row_rs_data['user_posting']; ?></td>
<td ><img src="img/icons/packs/silk/16x16/cross.png" width="16" height="16" alt="Hapus" title="Hapus" /></td>
</tr>
<?php
echo "test";
} while ($row_rs_data = mysql_fetch_assoc($rs_data)); ?>
</tbody>
</table>
Replace your <tbody> block. Use while instead of do..while and mysqli instead of mysql mysql has been deprecated.
<tbody>
<?php while ($row_rs_data = mysql_fetch_assoc($rs_data)){ ?>
<tr class=gradeX>
<!-- <td><center><?php echo $no++ ?></center></td> -->
<td><center>
<?php echo $row_rs_data['id_mutasi']; ?>
</center></td>
<td><?php echo $row_rs_data['tgl_mutasi']; ?></td>
<td ><?php echo $row_rs_data['kode_barang']; ?></td>
<td ><?php echo $row_rs_data['nm_barang']; ?></td>
<td ><?php echo $row_rs_data['ruanglama']; ?></td>
<td ><?php echo $row_rs_data['unitlama']; ?></td>
<td ><?php echo $row_rs_data['ruangbaru']; ?></td>
<td ><?php echo $row_rs_data['unitbaru']; ?></td>
<!-- <td ><?php echo $row_rs_data['userbaru']; ?></td> -->
<td ><center><?php echo $row_rs_data['jumlah']; ?></center></td>
<td ><?php echo $row_rs_data['user_posting']; ?></td>
<td ><img src="img/icons/packs/silk/16x16/cross.png" width="16" height="16" alt="Hapus" title="Hapus" /></td>
</tr>
<?php
echo "test";
} ?>
</tbody>

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>

Categories