How to simplify if statement into loop - php

I'm currently doing this if statement here, I wanted to simplify it into a loop. I try putting it into an array for the client_fullname and etc. but turns out only string form can be done. I wanted to simplicity this coding into a loop but i have no idea how to do it.
<tr class="sale" data-id="<?= $sale['id']; ?>">
<td><?= $pagination->offset + $key + 1; ?></td>
<?php if ($checked_columns['client_fullname']): ?>
<td><?= $sale['client_fullname']; ?></td>
<?php endif; ?>
<?php if ($checked_columns['client_email']): ?>
<td> <?php echo $sale['client_email']; ?></td>
<?php endif; ?>
<?php if ($checked_columns['client_phone_number']): ?>
<td> <?php echo $sale['client_phone_number']; ?></td>
<?php endif; ?>
<?php if ($checked_columns['total_amount']): ?>
<td><?= $sale['total_amount']; ?></td>
<?php endif; ?>
<?php if ($checked_columns['total_sales_amount']): ?>
<td><?= $sale['total_sales_amount']; ?></td>
<?php endif; ?>
<?php if ($checked_columns['first_date_buy']): ?>
<td><?= $sale['first_date_buy']; ?></td>
<?php endif; ?>
<?php if ($checked_columns['created_at']): ?>
<td><?= $sale['created_at']; ?></td>
<?php endif ?>
</tr>

You can reference the below example to achieve your desired output.
<?php
foreach($checked_columns as $key=>$column){
if ($column){
if ($key == 'client_email' || $key == 'client_phone_number'){?>
<td> <?= $sale[$key]; ?></td>
<?php }else{?>
<td><?= $sale[$key]; ?></td><?php
}
}

Related

How to use if elseif inside table row without making a new row?

I want to make a code that can use if else if inside table row.
But I can't make it ,once I use else if statement inside table row it make a new row by it self.
I expect that it make a new column in the same row
Here's a code
<tbody>
<?php $i = 1;
$varpetugas = null;
$varrealisasi = null;
?>
<?php foreach ($petugas as $k) : ?>
<tr>
<?php if ($k['nama_petugas'] != $varpetugas) : ?>
<th scope="row"><?= $i++; ?></th>
<td><?= $k['jenis_survey']; ?></td>
<td>
<?= $k['nama_petugas']; ?>
<?php $varpetugas = $k['nama_petugas']; ?></td>
<td><?= $k['target']; ?></td>
<td><?= $k['realisasi']; ?></td>
<?php $varrealisasi = $k['realisasi']; ?>
<?php elseif ($k['nama_petugas'] == $varpetugas) : ?>
<td>
<?= $k['target']; ?>
</td>
<td>
<?= $k['realisasi']; ?>
</td>
<?php $varrealisasi = $k['realisasi']; ?>
<?php $varpetugas = $k['nama_petugas']; ?>
<?php endif; ?>
</tr>
<?php endforeach; ?>
</tbody>
as you can see what I expected is else if statement show the output in a same row but , here's the result
You do new row (<tr>) every time when loop (next line after foreach).
But what you want to do is to have it only when condition (<?php if ($k['nama_petugas'] != $varpetugas) : ?>) is true.
Try this:
<?php foreach ($petugas as $k) : ?>
<?php if ($k['nama_petugas'] != $varpetugas) : ?>
<?php if ($i > 1) : ?>
</tr>
<?php endif; ?>
<tr>
<th scope="row"><?= $i++; ?></th>
<td><?= $k['jenis_survey']; ?></td>
<td>
<?= $k['nama_petugas']; ?>
<?php $varpetugas = $k['nama_petugas']; ?></td>
<td><?= $k['target']; ?></td>
<td><?= $k['realisasi']; ?></td>
<?php $varrealisasi = $k['realisasi']; ?>
<?php elseif ($k['nama_petugas'] == $varpetugas) : ?>
<td>
<?= $k['target']; ?>
</td>
<td>
<?= $k['realisasi']; ?>
</td>
<?php $varrealisasi = $k['realisasi']; ?>
<?php $varpetugas = $k['nama_petugas']; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php if ($i > 1) : ?>
</tr>
<?php endif; ?>
PS: Why do you have elseif? You can simply do just else. Isn't it?

Cakephp make child array editable

My model is a user which can have multiple Email addresses. I can view them by:
<?php foreach ($user->email_addresses as $addresses): ?>
<tr>
<td><?= h($addresses->id) ?></td>
<td><?= h($addresses->email) ?></td>
</tr>
?>
I try to make them editable as input but is does not work:
<?php foreach ($user->email_addresses as $addresses): ?>
<tr>
<td><?= $this->Form->control('addresses.id'); ?></td>
<td><?= $this->Form->control('addresses.email'); ?></td>
</tr>
?>
<?php foreach ($user->email_addresses as $index=>$addresses): ?>
<tr>
<td>
<?= $this->Form->control('email_addresses.'.$index.'.id') ?>
<?= $this->Form->control('email_addresses.'.$index.'.email') ?>

How to pause or continue Foreach php

This is my code,
<?php $i=1; foreach($xls as $xls): ?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $xls->jenis; ?></td>
<td><?php echo $xls->user; ?></td>
<td><?php echo $xls->nama; ?></td>
<td><?php echo $xls->unit; ?></td>
<?php if (is_array($jawab) || is_object($jawab)){ ?>
<?php if(isset($jawab)) {?>
<?php $limit=0;?>
<?php foreach($jawab as $row): ?>
<td><?php echo $row['ket_jawaban']; ?></td>
<?php $limit++; if($limit==2) break; endforeach; }} ?>
</tr>
<?php $i++; endforeach; ?>
</tbody>
How do I make each print 2 lines from foreach 2nd move to the bottom line (tr)
OK, with explanation:
The above code prints like this:
But I want to print like this:
It's simple. Change the name of one of your $jawab variables in the foreach loop.
<?php foreach($jawab as $jawab): ?>
And NEVER repeat a variable name in a loop.
UPDATE:
Try to change your loop:
<?php $limit=0;?>
<?php foreach($jawab as $row): ?>
<td><?php echo $row['ket_jawaban']; ?></td>
<?php $limit++; if($limit==2) break; endforeach; }} ?>
to:
<td><?php echo $jawab[i*2]['ket_jawaban']; ?></td>
<td><?php echo $jawab[i*2-1]['ket_jawaban']; ?></td>

checkbox error in cakephp

<?php
$i= 0;
foreach($purchaseorder as $tdata):
$i++;
?>
<tr >
<td><?php echo $tdata['pay_date']; ?></td>
<td><?php echo $tdata['cylinder']; ?></td>
<td><?php echo $tdata['amount']; ?></td>
<td><?php echo $tdata['rtgs_no']; ?></td>
<td><?php echo $tdata['cheque_no']; ?></td>
<td><?php echo $tdata['dd_no']; ?></td>
<td><?php echo if ($tdata['approve']=='true')
{
?>
<img src="/../img/green.png" alt="alt-tag"/>;
<?php
}
else
{
?>
<img src="/../img/red.png" alt="alt-tag" />;
<?php } ?></td>
</tr>
<?php
endforeach;
?>
my code is when i check a checkbox and submit a green tick should be displayed in list page..if its not checked and submitted a red cross should be displayed..but i get a error in the above code..what sholud i do?
<td><?php if ($tdata['approve']=='true')
{ ?>
<img src="/../img/green.png" alt="alt-tag"/>
<?php }
else
{ ?>
<img src="/../img/red.png" alt="alt-tag" />
<?php } ?></td>
<?php
foreach($purchaseorder as $tdata):
?>
<tr >
<td><?php echo $tdata['pay_date']; ?></td>
<td><?php echo $tdata['cylinder']; ?></td>
<td><?php echo $tdata['amount']; ?></td>
<td><?php echo $tdata['rtgs_no']; ?></td>
<td><?php echo $tdata['cheque_no']; ?></td>
<td><?php echo $tdata['dd_no']; ?></td>
<td><?php if($tdata['approve']==true)
{
?>
<img src="/../img/green.png" alt="alt-tag"/>;
<?php
}
else
{ ?>
<img src="/../img/red.png" alt="alt-tag" />;
<?php
} ?>
</td>
</tr>
<?php
endforeach;
?>
try this code and if possible show me the data thats comes on "$purchaseorder" variable
First make var_dump($tdata) and look 'approve' index value. If is set $tdata['approve'] then change code:
<td><?php if ($tdata['approve']=='true')
{ ?>
<img src="/../img/green.png" alt="alt-tag"/>
<?php }
else
{ ?>
<img src="/../img/red.png" alt="alt-tag" />
<?php } ?></td>
for this
<?php
$approveImg = ($tdata['approve']=='true') ? 'green.png' : 'red.png';
?>
<td>
<img src="/../img/<?=$approveImg?>" alt="alt-tag"/>
</td>
Also check paths to images

How to display a empty record error message in symfony 1.4

I am using a foreach loop to display table records. Below is my code for indexSuccess.php template
<?php $counter = 0; ?>
<?php foreach ($prescription->getPrescriptionDrugs() as $prescriptionDrug): ?>
<?php $counter++; ?>
<tr>
<th class="start"><?php echo $prescriptionDrug->getDrug()->getDrugName() ?></th>
<td><?php echo $prescriptionDrug->getAmountPerTime() ?></td>
<td><?php echo $prescriptionDrug->getTimesPerDay() ?></td>
<td><?php echo $prescriptionDrug->getCreater() ?> (<?php echo $prescriptionDrug->getCreatedAt() ?>)</td>
</tr>
<?php endforeach; ?>
<?php if($counter==0): ?>
<tr>
<td colspan="5">No items found</td>
</tr>
<?php endif; ?>
I can't seem to find a way to echo
<td colspan="5">No items found</td>
when the table is not showing any records.
If i use
<?php echo var_dump ($counter); ?>
It displays the result below
int 1
int 2
int 1
int 2
int 1
int 2
int 1
int 2
int 1
int 2
<?php if($prescription->getPrescriptionDrugs()->count() == 0): ?>
<tr>
<td colspan="5">No items found</td>
</tr>
<?php else: ?>
<?php foreach ($prescription->getPrescriptionDrugs() as $prescriptionDrug): ?>
<tr>
<th class="start"><?php echo $prescriptionDrug->getDrug()->getDrugName() ?></th>
<td><?php echo $prescriptionDrug->getAmountPerTime() ?></td>
<td><?php echo $prescriptionDrug->getTimesPerDay() ?></td>
<td><?php echo $prescriptionDrug->getCreater() ?> (<?php echo $prescriptionDrug->getCreatedAt() ?>)</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
You can use the count() function witch returns you the number of rows.
<?php $rows = count($prescription->getPrescriptionDrugs()); ?>
<?php if($rows==0){ ?>
<tr>
<td colspan="5">No items found</td>
</tr>
<?php } else { ?>
<?php foreach ($prescription->getPrescriptionDrugs() as $prescriptionDrug): ?>
<tr>
<th class="start"><?php echo $prescriptionDrug->getDrug()->getDrugName() ?></th>
<td><?php echo $prescriptionDrug->getAmountPerTime() ?></td>
<td><?php echo $prescriptionDrug->getTimesPerDay() ?></td>
<td><?php echo $prescriptionDrug->getCreater() ?> (<?php echo $prescriptionDrug->getCreatedAt() ?>)</td>
</tr>
<?php endforeach; ?>
<?php } ?>

Categories