using variable from array outside of foreach loop - php

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>

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 }?>

How to print out two queries in one table?

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>

PHP table. Show total before the new group

I am having a problem about showing total in groups.
Here is my scenario, I have a report grouped by area and by product.
What I already have is the row for area group.
What I want to do is to show the total qty of product per area before the row for the next group. Currently, it shows the total after every row.
Here is my code.
<?php if (isset($summaryPerArea)): ?>
<div class="col-lg-12">
<table id="" class="table table-bordered table-condensed table-striped">
<thead>
<tr>
<th>Area</th>
<th>Material</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
<?php
$prevArea = '';
$total = 0;
$currentQty = 0;
?>
<?php foreach ($summaryPerArea as $key => $value): ?>
<?php $currentQty = $value['totalQty']; ?>
<?php $total += $value['totalQty']; ?>
<?php if ($value['area'] != $prevArea): ?>
<tr class="bg-info">
<?php if ($key != 0) {$total = $currentQty;} ?>
<td colspan="3"><?php echo $value['area']; ?></td>
<?php $prevArea = $value['area']; ?>
</tr>
<?php endif; ?>
<tr>
<td><?php echo $value['area']; ?></td>
<td><?php echo $value['material']; ?></td>
<td><?php echo $value['totalQty']; ?></td>
</tr>
<?php if ($value['area'] == $prevArea): ?>
<tr class="bg-success">
<td colspan="3"><?php echo $total; ?></td>
</tr>
<?php endif; ?>
<?php endforeach ?>
</tbody>
</table>
</div>
<?php endif ?>
query:
SELECT d.SOffcNm as area,
c.ProdNm as material,
SUM(Qty) as totalQty
FROM BigEMerchandiser.dbo.tbl_Delivery_H as a
INNER JOIN BigEMerchandiser.dbo.tbl_Delivery_D as b
ON a.TransCtr = b.TransCtr
INNER JOIN BigESales.dbo.tbl_Materials as c
ON b.Material = c.ExtMatGrp
INNER JOIN BigESales.dbo.tbl_Customers as d
ON a.CustCode = d.CustCode
WHERE d.SOffc LIKE ISNULL('%' + #area + '%', d.SOffc)
AND a.DtRcv BETWEEN #DtRcvFrom AND #DtRcvTo
GROUP BY d.SOffcNm,
c.ProdNm
ORDER BY d.SOffcNm asc
current result:
Thankyou. I appreciate your help.
try to change your code according to the comments in the code below (only the foreach part)
<?php foreach ($summaryPerArea as $key => $value): ?>
<?php if ($value['area'] != $prevArea and $prevArea): // show total when changing area from second area onward ?>
<tr class="bg-success">
<td colspan="3"><?php echo $total; ?></td>
</tr>
<?php $total = 0; // reset the total after displaying it ?>
<?php endif; ?>
<?php if ($value['area'] != $prevArea):// show area header at every area change ?>
<tr class="bg-info">
<td colspan="3"><?php echo $value['area']; ?></td>
</tr>
<?php endif; ?>
<?php //$currentQty = $value['totalQty']; // does not needed ?>
<?php $total += $value['totalQty']; ?>
<tr>
<td><?php echo $value['area']; ?></td>
<td><?php echo $value['material']; ?></td>
<td><?php echo $value['totalQty']; ?></td>
</tr>
<?php $prevArea = $value['area']; // set prevArea to the processed row ?>
<?php endforeach ?>
<?php // show the last total ?>
<tr class="bg-success">
<td colspan="3"><?php echo $total; ?></td>
</tr>
notice that the order of the rows are repeated as follows:
--area total--
--area header--
--area item--
and followed by:
--area total--
on the first foreach, prevArea is still '' so the first condition in the --area total-- (and $prevArea) would result in false, so that the --area total-- is suppressed, but the --area header-- does not have that condition, so the --area header-- is not suppressed.
I use a different syntax of php, using if(condition){code};
If I understood your problem, I make a different solution, I controll if the next is different from the value that i use now, because if it was different you need to print.
I rewrite your code like this:
<tbody>
<?php
$prevArea = 'AnElementLikeFlag';
$total = 0;
$currentQty = 0;
?>
<?php foreach ($summaryPerArea as $key => $value): ?>
<?php $currentQty = $value['totalQty']; ?>
<?php if ($value['area'] != $prevArea && $value['area']!="AnElementLikeFlag"): ?>
<tr class="bg-success">
<td colspan="3"><?php echo $total; ?></td>
</tr>
<tr class="bg-info">
<td colspan="3"><?php echo $value['area']; ?></td>
<?php $prevArea = $value['area']; ?>
</tr>
<?php $total=0; ?>
<?php endif; ?>
<?php $total += $value['totalQty']; ?>
<tr>
<td><?php echo $value['area']; ?></td>
<td><?php echo $value['material']; ?></td>
<td><?php echo $value['totalQty']; ?></td>
</tr>
<?php endforeach ?>
</tbody>
I hope I help you and I maybe make an error.
P.S. You print the total every time, because there is a if condition that it is not necessery

Diffrent query in one function codeigniter

I use Codeigneter and I make a search function and in my function I want run two diffrent query and I want show them in diffrent table, How I can do that Sorry I can't show the data
This is My code:
My controller
function showDetail(){
// Retrieve the posted search term.
$detailTiket = $this->input->get('ticket_id');
// Use a model to retrieve the results.
$data["result"]= $this->tracking_model->showDetail($detailTiket);
$data1["result"]= $this->tracking_model->showDetail2($detailTiket);
// Pass the results to the view.
$this->load->view('tracking/tiket_detail',$data,$data1);
}
My Model
function showDetail($detailTiket)
{
if($detailTiket==""){
$detailTiket = "";
}
$showDetailTiket=$this->db->query("My Query1");
return $detailTiketDown->result();
}
function showDetail2($detailTiket)
{
if($detailTiket==""){
$detailTiket = "";
}
$detailTiketDown=$this->db->query("My query2");
return $detailTiketDown->result();
}
My view
<table Width='800'>
<?php
foreach($data as $row){?>
<tbody>
<tr>
<td><?php echo $row->ticket_id; ?></td>
</tr>
<tr>
<td><?php echo $row->created_time; ?></td>
</tr>
<tr>
<td><?php echo $row->start_IT; ?></td>
</tr>
<tr>
<td><?php echo $row->estimasi_selesai; ?></td>
</tr>
<tr>
<td><?php echo $row->name; ?></td>
</tr>
<tr>
<td><?php echo $row->description; ?></td>
</tr>
<tr style="background-color: cyan">
<td><b><?php echo $row->Status; ?></b></td>
</tr>
</tbody>
<?php } ?>
</table>
</center>
</div>
<div>
<table Width='1000'>
<?php foreach($data1 as $rows){ ?>
<tbody>
<tr>
<td><?php echo $rows->Tgl_Waktu; ?></td>
<td><?php echo $rows->PIC; ?></td>
<td><?php echo $rows->Tracking_Ticket; ?></td>
<td><?php echo $rows->Keterangan_Ticket; ?></td>
<td><?php echo $rows->File_Pendukung; ?></td>
</tr>
</tbody>
<?php }?>
</table>
You can pass data like Associative array to view
change your controller like this
Controller:
$data["result"]= $this->tracking_model->showDetail($detailTiket);
$data["result1"]= $this->tracking_model->showDetail2($detailTiket);
// Pass the results to the view.
$this->load->view('tracking/tiket_detail',$data);
view:
Table1 :
foreach($result as $row)
{
//for first result
}
Table2:
foreach($result1 as $row1)
{
//for second result
}

Hide Table Row Using CSS

Is it possible to hide table rows using CSS, I have a project that required this concept.
Here is my code:
style.css:
#hide-row { display:none; }
file.php
<table>
<tr>
<th>Name</th>
<th>Address</th>
</tr>
<div id="hide-row">
<?php foreach( $cops as $row ) { ?>
<tr>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->address; ?></td>
</tr>
<?php } ?>
</div>
</table>
But, It didn't work, the records still appear. Anybody help how to solve this case?
Any help will be appreciated. Thank You in Advanced !
Use a class instead of an id:
.hide-row { display:none; }
And in your html/php file:
<table>
<tr>
<th>Name</th>
<th>Address</th>
</tr>
<?php foreach( $cops as $row ) { ?>
<tr class="hide-row">
<td><?php echo $row->name; ?></td>
<td><?php echo $row->address; ?></td>
</tr>
<?php } ?>
</table>
If you have to group your rows you could use a tbody tag instead of a div tag.
Can we have multiple <tbody> in same <table>?
.hide-row tr { display:none; }
And in your html/php file:
<table>
<tr>
<th>Name</th>
<th>Address</th>
</tr>
<tbody class="hide-row">
<?php foreach( $cops as $row ) { ?>
<tr>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->address; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
You can't put divs as direct children of a < table> element. To hide single rows see jantimon's answer. If you want to group multiple rows use < tbody>:
css
.hide-row { display:none; }
php
<table>
<tr>
<th>Name</th>
<th>Address</th>
</tr>
<tbody class="hide-row">
<?php foreach( $cops as $row ) { ?>
<tr>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->address; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
You can't nest a div inside a table tag directly. You'd have to give your rows a class, then hide that. Something like:
.hidden {
display: none;
}
<?php foreach( $cops as $row ) { ?>
<tr class="hidden">
<td><?php echo $row->name; ?></td>
<td><?php echo $row->address; ?></td>
</tr>
<?php } ?>
you cannot have <div> outside <tr>.. give the class to <tr> and hide that..no need to create a <div> around it
html
<tr class="hide-row">
<td><?php echo $row->name; ?></td>
<td><?php echo $row->address; ?></td>
</tr>
style.css
.hide-row { display:none; }
I would give each row you want hidden a hide-row class:
<tr class="hide-row">
Your CSS would then look like:
tr.hide-row { display: none; }
This then means you don't need the nested div.

Categories