Sort XML data by newest to oldest in PHP - php

I've got this code that works well now but thought it would be useful to have the Observed Data start from the newest date to the oldest. How can I flip only the Observed Data? Here is the code (Sorry if it doesn't look right on here, still getting use to posting on this site):
<?php
$url = "http://r7j8v4x4.map2.ssl.hwcdn.net/NOD_R.xml";
$xml = simplexml_load_file($url);
?>
<?php foreach ($xml->RESULTSET[0]->ROW as $MSG) :?>
<?php echo '<h4>', $MSG->MSG_TXT; '</h4>'; ?>
<?php endforeach; ?>
<!-- Table Style -->
<style>
table {border: 2px solid #fff;}
table td {height: 15px;}
table td {border: 1px solid #fff; }
table tr {border: 1px solid #fff; }
table td {padding: 3px; }
</style>
<h2>Observed Data</h2>
<table>
<div style="overflow-x:auto;">
<thead>
<tr>
<td><span style="margin:0px; font-weight:bold">Day</span></td>
<td><span style="margin:0px; font-weight:bold">Time(EST)</span></td>
<td><span style="margin:0px; font-weight:bold">Reservoir Elev.(behind dam)*</span</td>
<td><span style="margin:0px; font-weight:bold">Tailwater Elev.(below dam)*</span></td>
<td><span style="margin:0px; font-weight:bold">Avg Hourly Discharge* </span></td>
</tr>
</thead>
<tbody>
<?php foreach ($xml->RESULTSET[1]->ROW as $obs) :?>
<tr>
<td><?php echo $obs->OBS_DAY; ?></td>
<td><?php echo $obs->OBS_HR; ?></td>
<td><?php echo $obs->UPSTREAM_ELEV; ?></td>
<td><?php echo $obs->DOWNSTREAM_ELEV; ?></td>
<td><?php echo $obs->AVG_HOURLY_DISCHARGE; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<h2>Predicted Data</h2>
<table>
<div style="overflow-x:auto;">
<thead>
<tr>
<td><span style="margin:25px; font-weight:bold">Day</span></td>
<td><span style="margin:5px; font-weight:bold">Average Inflow* </span</td>
<td><span style="margin:5px; font-weight:bold">Midnight Elevation*</span></td>
<td><span style="margin:5px; font-weight:bold">Average Outflow*</span></td>
</tr>
</thead>
<tbody>
<?php foreach ($xml->RESULTSET[2]->ROW as $pred) :?>
<tr>
<td><?php echo $pred->PREDICTED_DAY; ?></td>
<td><?php echo $pred->DAILY_AVG_INFLOW; ?></td>
<td><?php echo $pred->MIDNIGHT_ELEV; ?></td>
<td><?php echo $pred->DAILY_AVG_OUTFLOW; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>`

You can read them into arrays before you output.
foreach ($xml->RESULTSET[1]->ROW as $obs) {
$resultSet1[] = $obs;
}
foreach ($xml->RESULTSET[2]->ROW as $obs) {
$resultSet2[] = $obs;
}
That will make it easier for you to reverse them. Once they're in arrays there are various ways to do it. You can use array_reverse, a for loop with a decrementing index, or something like this:
<?php while ($obs = array_pop($resultSet1)) :?>
<tr>
<td><?php echo $obs->OBS_DAY; ?></td>
<td><?php echo $obs->OBS_HR; ?></td>
<td><?php echo $obs->UPSTREAM_ELEV; ?></td>
<td><?php echo $obs->DOWNSTREAM_ELEV; ?></td>
<td><?php echo $obs->AVG_HOURLY_DISCHARGE; ?></td>
</tr>
<?php endwhile; ?>

Related

php code printing on 2 pages instead of 1

I have build a POS system on php and facing one very funny issue. I can add upto 20 items on the sale memo and it gets stored in the db and we can retrieve it and print it, but whenever I print it, it always comes out in 2 pages instead of one even if there's one item and the rest of the value is null. Is there a way to force print on one page?
Here's the code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Print Invoice - Chara Motor Servicing</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="font-awesome/css/font-awesome.min.css" />
<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="container">
<div class="row">
<div class="col-xs-12">
<div class="text-center">
<h3>Chara Motor Servicing</h3>
<h4>Address Line 1</h4>
<h4>Telephone: 000000000000</h4>
<h4>GSTIN: XXXXXXXXXXXXXXX</h4>
</div>
<hr>
<?php
include 'database_connection.php';
$data=$_GET['invoice_id'];
$user_id=$data;
$sql = "SELECT `name`, `phone`, `town`, `date`, `item1`, `item2`, `item3`, `item4`, `item5`, `item6`, `item7`, `item8`,`item9`, `item10`, `item11`, `item12`, `item13`, `item14`, `item15`, `item16`,`item17`, `item18`, `item19`, `item20`, `item21`, `item22`, `q1`, `q2`, `q3`, `q4`, `q5`, `q6`, `q7`, `q8`,`q9`, `q10`, `q11`, `q12`, `q13`, `q14`, `q15`, `q16`,`q17`, `q18`, `q19`, `q20`, `q21`, `q22`, `taxper`, `amt1`, `amt2`, `amt3`, `amt4`, `amt5`, `amt6`, `amt7`, `amt8`, `amt9`, `amt10`, `amt11`, `amt12`, `amt13`, `amt14`, `amt15`, `amt16`,`amt17`, `amt18`, `amt19`, `amt20`, `amt21`, `amt22`,`taxtotal`, `subtotal`, `status` FROM `sales` WHERE id=$user_id";
$result = $conn->query($sql);
while ($row = mysqli_fetch_array($result)):
?>
<div class="row">
<div class="col-xs-12 col-md-3 col-lg-3 pull-left">
<div class="panel panel-default height">
<div class="panel-heading">Invoiced To: </div>
<div class="panel-body">
<strong><?php echo $row['name']; ?></strong><br>
<?php echo $row['phone']; ?><br>
<?php echo $row['town']; ?><br>
<?php echo $row['date']; ?><br>
<strong>Invoice#: <?php echo $user_id; ?><br> </strong>
<strong><?php echo $row['status']; ?><br> </strong><br>
<button class="btn btn-primary hidden-print" onclick="myFunction()"><span class="glyphicon glyphicon-print" aria-hidden="true"></span> Print</button>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="text-center"><strong>Order Summary</strong></h3>
</div>
<div class="panel-body">
<div class="table-responsive">
<table class="table table-condensed">
<thead>
<tr>
<td><strong>Item Name</strong></td>
<td class="text-center"><strong>Item Quantity</strong></td>
<td class="text-right"><strong>Total Price</strong></td>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $row['item1']; ?></td>
<td class="text-center"><?php echo $row['q1']; ?></td>
<td class="text-right"><?php echo $row['amt1']; ?></td>
</tr>
<tr>
<td><?php echo $row['item2']; ?></td>
<td class="text-center"><?php echo $row['q2']; ?></td>
<td class="text-right"><?php echo $row['amt2']; ?></td>
</tr>
<tr>
<td><?php echo $row['item3']; ?></td>
<td class="text-center"><?php echo $row['q3']; ?></td>
<td class="text-right"><?php echo $row['amt3']; ?></td>
</tr>
<tr>
<td><?php echo $row['item4']; ?></td>
<td class="text-center"><?php echo $row['q4']; ?></td>
<td class="text-right"><?php echo $row['amt4']; ?></td>
</tr>
<tr>
<td><?php echo $row['item5']; ?></td>
<td class="text-center"><?php echo $row['q5']; ?></td>
<td class="text-right"><?php echo $row['amt5']; ?></td>
</tr>
<tr>
<td><?php echo $row['item6']; ?></td>
<td class="text-center"><?php echo $row['q6']; ?></td>
<td class="text-right"><?php echo $row['amt6']; ?></td>
</tr>
<tr>
<td><?php echo $row['item7']; ?></td>
<td class="text-center"><?php echo $row['q7']; ?></td>
<td class="text-right"><?php echo $row['amt7']; ?></td>
</tr>
<tr>
<td><?php echo $row['item8']; ?></td>
<td class="text-center"><?php echo $row['q8']; ?></td>
<td class="text-right"><?php echo $row['amt8']; ?></td>
</tr>
<tr>
<td><?php echo $row['item9']; ?></td>
<td class="text-center"><?php echo $row['q9']; ?></td>
<td class="text-right"><?php echo $row['amt9']; ?></td>
</tr>
<tr>
<td><?php echo $row['item10']; ?></td>
<td class="text-center"><?php echo $row['q10']; ?></td>
<td class="text-right"><?php echo $row['amt10']; ?></td>
</tr>
<tr>
<td><?php echo $row['item11']; ?></td>
<td class="text-center"><?php echo $row['q11']; ?></td>
<td class="text-right"><?php echo $row['amt11']; ?></td>
</tr>
<tr>
<td><?php echo $row['item12']; ?></td>
<td class="text-center"><?php echo $row['q12']; ?></td>
<td class="text-right"><?php echo $row['amt12']; ?></td>
</tr>
<tr>
<td><?php echo $row['item13']; ?></td>
<td class="text-center"><?php echo $row['q13']; ?></td>
<td class="text-right"><?php echo $row['amt13']; ?></td>
</tr>
<tr>
<td><?php echo $row['item14']; ?></td>
<td class="text-center"><?php echo $row['q14']; ?></td>
<td class="text-right"><?php echo $row['amt14']; ?></td>
</tr>
<tr>
<td><?php echo $row['item15']; ?></td>
<td class="text-center"><?php echo $row['q15']; ?></td>
<td class="text-right"><?php echo $row['amt15']; ?></td>
</tr>
<tr>
<td><?php echo $row['item16']; ?></td>
<td class="text-center"><?php echo $row['q16']; ?></td>
<td class="text-right"><?php echo $row['amt16']; ?></td>
</tr>
<tr>
<td><?php echo $row['item17']; ?></td>
<td class="text-center"><?php echo $row['q17']; ?></td>
<td class="text-right"><?php echo $row['amt17']; ?></td>
</tr>
<tr>
<td><?php echo $row['item18']; ?></td>
<td class="text-center"><?php echo $row['q18']; ?></td>
<td class="text-right"><?php echo $row['amt18']; ?></td>
</tr>
<tr>
<td><?php echo $row['item19']; ?></td>
<td class="text-center"><?php echo $row['q19']; ?></td>
<td class="text-right"><?php echo $row['amt19']; ?></td>
</tr>
<tr>
<td><?php echo $row['item20']; ?></td>
<td class="text-center"><?php echo $row['q20']; ?></td>
<td class="text-right"><?php echo $row['amt20']; ?></td>
</tr>
<tr>
<td><?php echo $row['item21']; ?></td>
<td class="text-center"><?php echo $row['q21']; ?></td>
<td class="text-right"><?php echo $row['amt21']; ?></td>
</tr>
<tr>
<td><?php echo $row['item22']; ?></td>
<td class="text-center"><?php echo $row['q22']; ?></td>
<td class="text-right"><?php echo $row['amt22']; ?></td>
</tr>
<tr>
<td class="highrow"></td>
<td class="highrow"></td>
<td class="highrow text-center"><strong>GST/Tax (%)</strong></td>
<td class="highrow text-right"><?php echo $row['taxper']; ?></td>
</tr>
<tr>
<td class="emptyrow"></td>
<td class="emptyrow"></td>
<td class="emptyrow text-center"><strong>GST/Tax Amount</strong></td>
<td class="emptyrow text-right"><?php echo $row['taxtotal']; ?></td>
</tr>
<tr>
<td class="emptyrow"></td>
<td class="emptyrow text-center"><strong>SubTotal</strong></td>
<td class="emptyrow text-right"><strong><?php echo $row['subtotal']; ?></strong></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
<script>
function myFunction() {
window.print();
}
</script>
<style>
.height {
min-height: 200px;
}
.icon {
font-size: 47px;
color: #5CB85C;
}
.iconbig {
font-size: 77px;
color: #5CB85C;
}
.table > tbody > tr > .emptyrow {
border-top: none;
}
.table > thead > tr > .emptyrow {
border-bottom: none;
}
.table > tbody > tr > .highrow {
border-top: 3px solid;
}
</style>
</div>
</body>
</html>

PHP, HTML table create

I have little problem with tables when u echo my results.
and this is my code
<?php
//return data if any
while ($client = mysqli_fetch_assoc($result)) {
//output data form each client
//var_dump($client);
?>
<table id="t01">
<tr>
<th>ID</th>
<th>name</th>
<th>lastname</th>
<th>age</th>
<th>Username</th>
<th>Password</th>
</tr>
<tr>
<td><?php echo $client['id'] ?></td>
<td><?php echo $client['emri'] ?></td>
<td><?php echo $client['mbiemri'] ?></td>
<td><?php echo $client['mosha'] ?></td>
<td><?php echo $client['username'] ?></td>
<td><?php echo $client['password'] ?></td>
</tr>
<style>
table {
width:100%;
}
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
table#t01 tr:nth-child(even) {
background-color: #eee;
}
table#t01 tr:nth-child(odd) {
background-color:#fff;
}
table#t01 th {
background-color: black;
color: white;
}
</style>
</table>
this is my code and the list its echo the first line like id, name ,lastname... for every result. so i want attributes on the top and then down results (i cant post a photo)
You just Need to reorder your code:
<table id="t01">
<tr>
<th>ID</th>
<th>name</th>
<th>lastname</th>
<th>age</th>
<th>Username</th>
<th>Password</th>
</tr>
<?php
//return data if any
while ($client = mysqli_fetch_assoc($result)) {
//output data form each client
//var_dump($client);
?>
<tr>
<td><?php echo $client['id'] ?></td>
<td><?php echo $client['emri'] ?></td>
<td><?php echo $client['mbiemri'] ?></td>
<td><?php echo $client['mosha'] ?></td>
<td><?php echo $client['username'] ?></td>
<td><?php echo $client['password'] ?></td>
</tr>
><?php } ?>
</table>
You had Everything in youre While so for every Entry everything got printed.
And put your Style Tag in the Head Section of your HTML DOC

dompdf page break with codeigniter and foreach loop

I am using codeigniter and dompdf to generate a pdf document.
the document is generated with a foreach loop. each loop creates a few table rows. I want to have a pagebreak between the rows of each loop.
my relevent codeigniter view syntax is:
<table width=100%>
<?php foreach($summary as $summary){ ?>
<tr class="firstrow">
<td colspan="10">
<?php echo "<b>Customer</b>: <font color='blue'>".$summary->caccountname; ?>
<?php echo "</font> <b>Order Number</b>: <font color='blue'>".$summary->OrderNum; ?>
<?php echo "</font> <b>Sales rep</b>: <font color='blue'>".$summary->RepName; ?>
<?php echo "</font> <b>Due Date</b>: <font color='blue'>".substr($summary->DueDate,0,10); ?>
</td>
</tr>
<tr>
<td colspan="10"><hr>
</td>
</tr>
<tr>
<td colspan="10">
<table class="sortable" width="90%">
<tr><td ><b>Product</td><td ><b>Ordered</td><td ><b>Processed</td><td ><b>Outstanding</td><td ><b>Dispatched</td><td ><b>Available (incl FJ)</td><td ><b>Available FJ</td></tr>
<?php foreach($$linedetails as $linedetails){ ?>
<tr>
<td><?php echo $linedetails->Product; ?></td>
<td><?php echo $linedetails->cubicvolume; ?></td>
<td><?php echo $linedetails->Processed; ?></td>
<td><?php echo $linedetails->Outstanding; ?></td>
<td><?php echo $linedetails->Dispatched; ?></td>
<td><?php echo $linedetails->TotalVolume; ?></td>
<td><?php echo $linedetails->FJVolume; ?></td>
</tr>
<?php } ?>
</table>
</td>
</tr>
<?php } ?>
</table>
I have given the first tr a class of firstrow, I would like a page break to appear before this line every time.
As such I have applied the following style sheet in my page header:
<style>
#media print
{
.firstrow {page-break-before:always}
}
</style>
This doesnt work.
How can I acheive my desired result of placeing a page break before each loop firstrow TR?
Thanks as always
To make Pagebreak work with tr, write a line of css for tr
tr { page-break-inside:avoid; page-break-after:auto }
Try above code.
If not working, then add
table { page-break-inside:auto }
Try this, I am doing reports exactly same way
<?php
$nbsp5 = " ";
foreach($summary as $summary) { ?>
<table width="100%" style="page-break-after:always;">
<tr>
<td colspan="10">
<?php echo "<b>Customer</b>: <font color='blue'>".$summary->caccountname; ?>
<?php echo "</font>".$nbsp5 ." <b>Order Number</b>: <font color='blue'>".$summary->OrderNum; ?>
<?php echo "</font>".$nbsp5 ." <b>Sales rep</b>: <font color='blue'>".$summary->RepName; ?>
<?php echo "</font>".$nbsp5 ." <b>Due Date</b>: <font color='blue'>".substr($summary->DueDate,0,10); ?>
</td>
</tr>
<tr>
<td colspan="10"><hr></td>
</tr>
<tr>
<td colspan="10">
<table class="sortable" width="90%">
<tr>
<td><b>Product</b></td>
<td><b>Ordered</b></td>
<td><b>Processed</b></td>
<td><b>Outstanding</b></td>
<td><b>Dispatched</b></td>
<td><b>Available (incl FJ)</b></td>
<td><b>Available FJ</b></td>
</tr>
<?php foreach($$linedetails as $linedetails){ ?>
<tr>
<td><?php echo $linedetails->Product; ?></td>
<td><?php echo $linedetails->cubicvolume; ?></td>
<td><?php echo $linedetails->Processed; ?></td>
<td><?php echo $linedetails->Outstanding; ?></td>
<td><?php echo $linedetails->Dispatched; ?></td>
<td><?php echo $linedetails->TotalVolume; ?></td>
<td><?php echo $linedetails->FJVolume; ?></td>
</tr>
<?php } ?>
</table>
</td>
</tr>
</table>
<?php } ?>
(first <table> tag moved inside loop)
Please try this answer for both header and footer.

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.

Creating table out from this array structure in php

Im having trouble creating a table out from this array:
http://pastebin.com/DXFjfhHJ
I started out with this:
<table style="width: 100%; text-align: center;">
<tr>
<td>Time</td>
<td>Aktivitet</td>
<td>Duration</td>
<td>Metabolisation</td>
</tr>
then i did:
foreach{$training as $time => $metabolisation}{
?>
<tr style="text-align: left;">
<td><?php echo $time; ?></td>
<td>Activity name</td>
<td>Duration</td>
<td><?php echo $metabolisation; ?></td>
</tr>
<?php
}
Which works almost.. It shows the right $time ( 03:00 etc. ), but nothing in $metabolisation. And I dont know how to call the Activity, it should be the arrays "name" variable. Same with duration, it should be the arrays "duration"
How can I do this?
Well the structure your using in your pastebin is not compatible with your foreach. You realy need to look deeper in your array structure like this.
<?php foreach( $training as $time => $data ): ?>
<tr style="text-align: left;">
<td><?php echo $time; ?></td>
<td>Total <?php echo $data['entries'][0]['name']; ?></td>
<td>Duration <?php echo $data['entries'][0]['duration']; ?></td>
<td><?php echo $data['entries'][0]['metabolisation']; ?></td>
</tr>
<?php endforeach; ?>
foreach($training as $time => $metabolisation){
foreach($metabolisation['entries'] as $entry) {
?>
<tr style="text-align: left;">
<td><?php echo $time; ?></td>
<td><?php echo $entry['name']; ?></td>
<td><?php echo $entry['duartion']; ?></td>
<td><?php echo $entry['metabolisation']; ?></td>
</tr>
<?php
}
}
?>
It probably doesn't output the data in an appropriate form, but you get the idea how to access it.

Categories