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>
Related
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
}
}
i just want data will show if ['stt']==1
Here is the code :
<?php do { ?>
<tr>
<td><?php echo $row_daftaruser['id']; ?></td>
<td><?php echo $row_daftaruser['nama']; ?></td>
<td><?php echo $row_daftaruser['email']; ?></td>
<td><?php echo $row_daftaruser['username']; ?></td>
<td><?php echo $row_daftaruser['password']; ?></td>
<td><?php echo $row_daftaruser['alamat']; ?></td>
<td><?php echo $row_daftaruser['tgl_lahir']; ?></td>
<td><?php echo $row_daftaruser['stt']; ?></td>
</tr>
<?php } while ($row_daftaruser = mysql_fetch_assoc($daftaruser)); ?>
Can somebody help me?
you could try this:
<?php do { ?>
<?php if($row_daftaruser['stt'] == 1): ?>
<tr>
<td><?php echo $row_daftaruser['id']; ?></td>
<td><?php echo $row_daftaruser['nama']; ?></td>
<td><?php echo $row_daftaruser['email']; ?></td>
<td><?php echo $row_daftaruser['username']; ?></td>
<td><?php echo $row_daftaruser['password']; ?></td>
<td><?php echo $row_daftaruser['alamat']; ?></td>
<td><?php echo $row_daftaruser['tgl_lahir']; ?></td>
<td><?php echo $row_daftaruser['stt']; ?></td>
</tr>
<?php endif ?>
<?php } while ($row_daftaruser = mysqli_fetch_assoc($daftaruser)); ?>
This code will only display values if stt is equal to 1.
Also, use mysqli not mysql because it mysql is obsolete.
Why do not you write the query this way?
SELECT * FROM TABLE WHERE stt = 1;
I have a little issue when I try to to loop my php values in HTML. So far this is what I tried but I have not excpected result.
If I remove the loop I only get the first entry. I would like to echo all the possibles entries from my research.
This is my code ( from an SQL request).
<html>
<table>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Number</th>
<th>Adress</th>
<th>link</th>
<!--<th class="glyphicon glyphicon-pencil"></th>-->
</tr>
<tr>
<?php $rowG = oci_fetch_array($stid, OCI_RETURN_NULLS);?>
<?php foreach($array as $rowG=>$value): ?> <tr>
<td><?php echo $rowG[2]; ?></td>
<td><?php echo $rowG[1]; ?></td>
<td><?php echo $rowG[0]; ?></td>
<td><?php echo $rowG[3]?></td>
<td><?php echo "<a href='./consultation.php?Login=$rowG[2]'> Link </a>" ; ?></td>
<?php endforeach;}} ?>
</tr>
</table>
</html>
Do you know where I made my mistake ?
Thank you for your help
Edit : Finally I managed to do it by using a do{}while loop.
Thank you all for your help
RFlow
It's hard to guess what you are trying to do or even what actually happens, since I don't know what is assigned to $rowG, so I tried to hack meaning out of this from the code's errors and came up with that :
<?php
while ($rowG = oci_fetch_array($stid, OCI_RETURN_NULLS)) {
?>
<tr>
<td><?php echo $rowG[2]; ?></td>
<td><?php echo $rowG[1]; ?></td>
<td><?php echo $rowG[0]; ?></td>
<td><?php echo $rowG[3]; ?></td>
<td><?php echo ' Link '; ?></td>
</tr>
<?php
}
?>
If it doesn't work with you, you'll have to provide the informations that should have been included in your question since the begining.
This is basic iteration over query results:
while ($rowG = oci_fetch_array($stid, OCI_RETURN_NULLS)) {?>
<tr>
<td><?php echo $rowG[2]; ?></td>
<td><?php echo $rowG[1]; ?></td>
<td><?php echo $rowG[0]; ?></td>
<td><?php echo $rowG[3]?></td>
<td><?php echo "<a href='./consultation.php?Login=$rowG[2]'> Link </a>" ; ?></td>
</tr>
<?php
}
<?php foreach($array as $rowG=>$value): ?> <tr>
<td><?php echo $rowG[2]; ?></td>
<td><?php echo $rowG[1]; ?></td>
<td><?php echo $rowG[0]; ?></td>
<td><?php echo $rowG[3]?></td>
<td><?php echo "<a href='./consultation.php?Login=$rowG[2]'> Link </a>" ; ?></td>
<?php endforeach;}} ?>
Would be :
<?php foreach($rowG as $arr): ?> <tr>
<td><?php echo $arr[2]; ?></td>
<td><?php echo $arr[1]; ?></td>
<td><?php echo $arr[0]; ?></td>
<td><?php echo $arr[3]?></td>
<td><?php echo "<a href='./consultation.php?Login=$arr[2]'> Link </a>" ; ?></td>
<?php endforeach;}} ?>
Note the use of $arr instead of $rowG.
In the original code $array is not used.
<?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
Here's my controller call "a_porto"
public function index() {
$this->load->model('a_porto_models');
$data = array();
$data['tampil'] = $this->a_porto_models->get_crud_all();
$this->load->view('a_porto_view', $data);
}
Here's my model call "a_porto_models"
function get_crud_all() {
$query = $this->db->get('prototype');
return $query->result();
}
And than my view call "a_porto_views"
<?php $tampil = array(); ?>
<?php foreach ($tampil as $a){ ?>
<tr>
<td><?php echo $a->id_proto; ?></td>
<td><?php echo $a->foto; ?></td>
<td><?php echo $a->nama_pro; ?></td>
<td><?php echo $a->tahun; ?></td>
<td><?php echo $a->lokasi; ?></td>
<td><?php echo $a->deskripsi; ?></td>
</tr>
<?php } ?>
What I've to do? My code doesn't show an error :(
Change to this code:
<?php foreach ($tampil as $a){ ?>
<tr>
<td><?php echo $a->id_proto; ?></td>
<td><?php echo $a->foto; ?></td>
<td><?php echo $a->nama_pro; ?></td>
<td><?php echo $a->tahun; ?></td>
<td><?php echo $a->lokasi; ?></td>
<td><?php echo $a->deskripsi; ?></td>
</tr>
<?php } ?>
You see null result because you reset your array to be empty. when its already have content from the controller sent to the view
Remove $tampil = array(); and try again. You are basically setting your result to an empty array there.