How to print out two queries in one table? - php

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>

Related

Codeigniter Print Domp PDF Page Break Limit 5 records per page In table foreach

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>

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

Display JSON info in a PHP table

I am trying to display JSON content in a PHP table but its not working. I can't figure out what I should change.
Here is my code:
<html>
<head>
<title>Download</title>
</head>
<body>
<?php
$myData = file_get_contents("https://youtubetoany.com/#api/json/videostreams/VEou0QBeHlk");
$myObject = json_decode($myData);
$myObjectMap = $myObject->vidInfo;
?>
<table>
<thead>
<tr>
<td>Url</td>
<td>Size</td>
<td>Quality</td>
<td>Type</td>
</tr>
</thead>
<tbody>
<?php foreach($myObjectMap as $key => $item): ?>
<tr>
<td><?PHP echo $item->dloadUrl; ?></td>
<td><?PHP echo $item->rSize; ?></td>
<td><?PHP echo $item->round; ?></td>
<td><?PHP echo $item->quality; ?></td>
<td><?PHP echo $item->ftype; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</body>
</html>
This is what I get in my browser:
Url Size Quality Type
Your source has some javascript attached. You need to get rid of that:
$myData = file_get_contents("https://youtubetoany.com/#api/json/videostreams/VEou0QBeHlk");
// get the substring from start til the first occurence of "<script"
$myRealData = substr($myData,0,strpos($myData,"<script"));
$myObject = json_decode($myRealData);
BUT this source doesn't seem to be made to be grabbed. So I won't rely on that source or that it'll stay the way you find it now.
I just found probably why its not working. The link returns a JavaScript attached to the bottom of the JSON. So here is my solution.
<html>
<head>
<title>Download</title>
</head>
<body>
<?php
$myData = file_get_contents("https://youtubetoany.com/#api/json/videostreams/VEou0QBeHlk");
// This up to the last occurrence of the "}"
$json_block = substr($myData, 0, strripos($myData, "}"));
$myObject = json_decode($json_block);
$myObjectMap = $myObject->vidInfo;
?>
<table>
<thead>
<tr>
<td>Url</td>
<td>Size</td>
<td>Quality</td>
<td>Type</td>
</tr>
</thead>
<tbody>
<?php foreach($myObjectMap as $key => $item): ?>
<tr>
<td><?PHP echo $item->dloadUrl; ?></td>
<td><?PHP echo $item->rSize; ?></td>
<td><?PHP echo $item->round; ?></td>
<td><?PHP echo $item->quality; ?></td>
<td><?PHP echo $item->ftype; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</body>
</html>

using variable from array outside of foreach loop

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>

Working with multiple xml with same format

I am working on a xml to table project which would use PHP.
<? $xml = new SimpleXMLElement('http://example.com/genXML.php?product=388', 0, TRUE);
?>
<table>
<thead>
<tr>
<th>Symbol</th>
<th>Name</th>
<th>Price</th>
<th>Change</th>
<th>Percentage</th>
<th>High</th>
<th>Low</th>
</tr>
</thead>
<tbody>
<?php foreach ($xml->product as $check) :?>
<tr>
<td><?php echo $check->symbol; ?></td>
<td><?php echo $check->name->chinese; ?></td>
<td><?php echo $check->price; ?></td>
<td><?php echo $check->change; ?></td>
<td><?php echo $check->pct_change; ?></td>
<td><?php echo $check->high; ?></td>
<td><?php echo $check->low; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
As product will have their own unique code, I would like the programe to show all the details in one table.
Is it possible to use MYSQL Database to store the product code that i need to ref. and show them out on a single web page? Thanks!

Categories