I have this code
<?php foreach ($column as $k => $v): ?>
<tr>
<td><?php echo $v; ?></td>
<td><?php echo $k; ?></td>
</tr>
<?php endforeach ?>
And i get this output
<tr>
<td>id</td>
</tr>
<tr>
<td>name</td>
</tr>
<tr>
<td>address</td>
</tr>
<tr>
<td>birth</td>
</tr>
<tr>
<td>foto</td>
</tr>
but i don't want to include the foto, how can i do this ?
you can use if condition inside foreach
<?php foreach ($column as $k => $v):
if($v=='foto'){ // skip iteration if value is `foto`
continue;
}
?>
<tr>
<td><?php echo $v; ?></td>
<td><?php echo $k; ?></td>
</tr>
<?php endforeach ?>
You can do the following before starting the loop-
If foto is the key -
if(isset($column['foto']))
unset($column['foto']);
If foto is value -
if(in_array('foto', $column))
unset($column[array_search('foto', $column)]);
After that looping starts -
<?php foreach ($column as $k => $v): ?>
<tr>
<td><?php echo $v; ?></td>
<td><?php echo $k; ?></td>
</tr>
<?php endforeach ?>
Related
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>
I have two queries in a form. I print out the results like this:
<table>
<thead>
<tr>
<th>Heading1</th>
<th>Heading2</th>
<th>Heading3</th>
<th>Heading4</th>
</tr>
</thead>
<tbody>
<?php foreach ($result1 as $row1) : ?>
<tr>
<td><?php echo escape($row1["Column1"]); ?></td>
<td><?php echo escape($row1["Column2"]); ?></td>
<td><?php echo escape($row1["Column3"]); ?></td>
<?php endforeach; ?> </tr>
<tr> <?php foreach ($result2 as $row2) : ?>
<td><?php echo escape($row2["Column4"]); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
I want to present each result in a different column. But the result from the second query ("Column4") seems to be presented in a second table (??). It's not echoed next to the other columns, but below:
Current Output
How can I fix this issue?
You have to change
<?php endforeach; ?> </tr>
<tr> <?php foreach ($result2 as $row2) : ?>
into
<?php endforeach; ?>
<?php foreach ($result2 as $row2) : ?>
You have begun another row ('tr' tag closed and re-opened).
Try this fix...
I have removed the things that are causing it to echo on next line
Now this will work...
<table>
<thead>
<tr>
<th>Heading1</th>
<th>Heading2</th>
<th>Heading3</th>
<th>Heading4</th>
</tr>
</thead>
<tbody>
<?php foreach ($result1 as $row1) : ?>
<tr>
<td><?php echo escape($row1["Column1"]); ?></td>
<td><?php echo escape($row1["Column2"]); ?></td>
<td><?php echo escape($row1["Column3"]); ?></td>
<?php endforeach; ?>
<?php foreach ($result2 as $row2) : ?>
<td><?php echo escape($row2["Column4"]); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
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.
I want 3 elements of array to be displayed before setTimeout(30000) function and 3 elements after it;so that there is gap of 30 seconds between both the loops.But I am not getting the desired result.Following is the code:
<?php
$info=array(array("name"=>"abc",
"phone"=>"12345"),
array("name"=>"pqr",
"phone"=>"23456"),
array("name"=>"def",
"phone"=>"34567"),
array("name"=>"asd",
"phone"=>"45678"),
array("name"=>"ghj",
"phone"=>"56789"),
array("name"=>"jkl",
"phone"=>"67566"),
);
echo ( $info[0]["name"]);
?>
<body>
<table>
<tr>
<th>id</th>
<th>Name</th>
<th>phone<th>
</tr>
<?php
$i=1;
foreach ($info as $data) {
if($i==4)
{
break;
}
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $data["name"]; ?></td>
<td><?php echo $data["phone"]; ?></td>
</tr>
<?php $i++;}?>
<script>
window.setTimeout(30000);
</script>
<?php
for($j=3;$j<6;$j++)
{
?>
<tr>
<td><?php echo $j+1; ?></td>
<td><?php echo $info[$j]["name"]; ?></td>
<td><?php echo $info[$j]["phone"]; ?></td>
</tr>
<?php } ?>
</table>
Controller
This is Controller Page.
public function index() {
$data = array(
'year' => $this->year_model->list_all(),
'speces' => $this->speces_model->list_all(),
);
foreach ($data['year'] as $value):
$temp = array();
$temp['year_key'] = $value['year-range'];
$temp['year_id'] = $value['id'];
$temp['speces_key'] = array();
foreach ($data['speces'] as $value1):
$temp['speces_key'][$value1['name']] = $this->db->get_where('expendituredata', array('spacesId' => $value1['id'], 'yearId' => $value['id']))->row_array();
endforeach;
$final_result[] = $temp;
endforeach;
$data['result'] = $final_result;
$this->load->view('site/template', $data);
}
Model
public function list_all() {
return $this->db->get('year')->result_array();
}
View
This is View file.
<table>
<thead>
<tr>
<th>Year Range</th>
<?php foreach ($speces as $value): ?>
<th> <?php echo $value['name']; ?></th>
<?php endforeach; ?>
<th>TOTAL</th>
</tr>
</thead>
<tbody>
<?php foreach ($result as $value): ?>
<?php $sum = 0; ?>
<tr>
<td><?php echo $value['year_key']; ?></td>
<?php foreach ($value['speces_key'] as $key => $value2): ?>
<td><?php echo $value2['data']; ?></td>
<?php $sum+= $value2['data']; ?>
<?php endforeach; ?>
<td><?php echo $sum; ?></td>
</tr>
<?php endforeach; ?>
<tr>
<td>Total</td>
<?php foreach ($speces as $value6): ?>
<?php
$this->db->select_sum('data');
$query = $this->db->get_where('expendituredata', array('spacesId' => $value6['id']))->row_array();
$totals = $query['data'];
?>
<td><?php echo $totals; ?></td>
<?php endforeach; ?>
</tr>
</table>
[Screen Shot]
My Question: How to calculate sum of column (column name is total).column name total is display above screenshot.so how to calculate sum of column(column name is total) and sum display in red arrow in above screen shot
I think you want to calculate sum of total column which is not a database field. So to do that you can take a variable in which you will addup the column value of every row.
Just Initialize a variable to zero.
<?php $sum = $totalSum = 0; ?>
Now add value of sum variable for every row.
change your line,
<td><?php echo $sum; ?></td>
with
<td><?php $totalSum+=$sum; echo $sum; ?></td>
Now your $totalSum field will have the sum of your total column. You can show it where ever you want.
UPDATED
Use this in your view file and check.
<table>
<thead>
<tr>
<th>Year Range</th>
<?php foreach ($speces as $value): ?>
<th> <?php echo $value['name']; ?></th>
<?php endforeach; ?>
<th>TOTAL</th>
</tr>
</thead>
<tbody>
<?php $totalSum=0; ?>
<?php foreach ($result as $value): ?>
<?php $sum = 0; ?>
<tr>
<td><?php echo $value['year_key']; ?></td>
<?php foreach ($value['speces_key'] as $key => $value2): ?>
<td><?php echo $value2['data']; ?></td>
<?php $sum+= $value2['data']; ?>
<?php endforeach; ?>
<td><?php $totalSum+= $sum; echo $sum; ?></td>
</tr>
<?php endforeach; ?>
<tr>
<td>Total</td>
<?php foreach ($speces as $value6): ?>
<?php
$this->db->select_sum('data');
$query = $this->db->get_where('expendituredata', array('spacesId' => $value6['id']))->row_array();
$totals = $query['data'];
?>
<td><?php echo $totals; ?></td>
<?php endforeach; ?>
<td><?php echo $totalSum; ?></td>
</tr>
</table>
I think this will solve your problem.
try to use
<?php $sum = $sum + $value2['data']; ?>
instead of
<?php $sum+= $value2['data']; ?>