date range wise fetch data - php

I have two date filters (from and to) which will represent days in the current month and I declare them as two variables.
How do I run a for loop in each field from start_date to end date?
I want to fetch the data in the selected date range. The table header is done, it show current selected date.
Here is my code:
$start_date = date('d', strtotime($from));
$end_date = date('d', strtotime($to));
?>
<div>
<div>
<table>
<thead>
<tr>
<th>Type</th>
<?php
$dates = range($end_date, $start_date);
foreach ($dates as $fpi) {
echo '<th>' . $fpi . '</th>';
}
?>
</tr>
</thead>
<tbody class="text-center">
<tr>
<td class="font-weight-bold text-no-wrapping">man</td>
<?php
foreach ($data['man'] as $value) {
echo '<td>' . number_format($value) . '</td>';
}
?>
</tr>
<tr>
<td class="font-weight-bold text-no-wrapping">women</td>
<?php
foreach ($data['women'] as $value) {
echo '<td>' . number_format($value) . '</td>';
}
?>
</tr>
<tr>
<td class="font-weight-bold text-no-wrapping">children</td>
<?php
foreach ($data['children'] as $value) {
echo '<td>' . number_format($value) . '</td>';
}
?>
</tr>
<tr>
<td class="font-weight-bold text-no-wrapping">as</td>
<?php
foreach ($data['as'] as $value) {
echo '<td>' . number_format($value) . '</td>';
}
?>
</tr>
<tr>
<td class="font-weight-bold text-no-wrapping">ot define</td>
<?php
foreach ($data['not_define'] as $value) {
echo '<td>' . number_format($value) . '</td>';
}
?>
</tr>
<tr>
<td class="font-weight-bold text-no-wrapping">old</td>
<?php
foreach ($data['old'] as $value) {
echo '<td>' . number_format($value) . '</td>';
}
?>
</tr>
</tbody>
</table>
</div>
</div>

this will loop through the dates range, feel free to change $date->format('Y-m-d) to what you need
$date = $start_date
while($date <= $end_date){
echo '<th>' . $date->format("Y-m-d"). '</th>';
$date->modify('+1 day');
}

Related

Show table with results grouped 3 by 3

I am trying to show results that I get from a SQL table, it is this:
what I want to do is show results 3 by 3, like this:
I mean a table for every 3 results that the "assigned_bank" field matches, and if there are 4 results with the same number in "assigned_bank", I also show it in that same table, that is; one table for each different "assigned_bank" id.
I've been trying most of the day and the closest thing I've come to is this:
This is my last code:
<?php
$tables = sizeof($search) / 3;
for ($i = 0; $i < $tables; $i++) {
?>
<table class="table customers">
<thead class="thead-blue">
<tr>
<th scope="col-xs-2">Name</th>
<th scope="col-xs-2">Lastname</th>
<th scope="col-xs-2">Bank ID</th>
</tr>
</thead>
<tbody>
<?php
foreach ($search as $item){
echo '<tr align="left">';
echo '<td class="col-xs-2">' . $item["p_name"] . '</td>' . "\r\n";
echo '<td class="col-xs-2">' . $item["p_lastname"] . '</td>' . "\r\n";
echo '<td class="col-xs-2">' . $item["assigned_bank"] . '</td>' . "\r\n";
echo '</tr>';
}
?>
</tbody>
</table>
<?php
echo "\r\n";
}
?>
Thank you very much for any possible help or comments and thank you for taking the time to respond.
<?php
$result = array();
foreach ($search as $key => $item) {
$result[$item['assigned_bank']][$key] = $item;
}
foreach($result as $key=>$search_items){
echo '<table class="table customers" border="2" >
<thead class="thead-blue">
<tr>
<th scope="col-xs-2">Name</th>
<th scope="col-xs-2">Lastname</th>
<th scope="col-xs-2">Bank ID</th>
</tr>
</thead>
<tbody>';
foreach($search_items as $skey=>$item){
echo '<tr align="left">';
echo '<td class="col-xs-2">' . $item["p_name"] . '</td>' . "\r\n";
echo '<td class="col-xs-2">' . $item["p_lastname"] . '</td>' . "\r\n";
echo '<td class="col-xs-2">' . $item["assigned_bank"] . '</td>' . "\r\n";
echo '</tr>';
}
echo '</tbody>
</table>';
}
<?>
You can use order by on assigned_bank column with ascending order:
SELECT p_name, p_lastname, assigned_bank FROM your_table order by
assigned_bank asc

Pass value from one page to another in PHP without using URL

I used to pass value from one page to another using session. E.g
page1.php
<?php
session_start();
$_SESSION["id"] = $id;
?>
page2.php
<?php
session_start();
echo $_SESSION["id"];
?>
But for the following case i don't know how can i pass the id to the next page. This code is used to display group of people and the action column have an option to view the people details.
<table>
<thead>
<tr>
<th>Name</th>
<th>Status</th>
<th>Created</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$DBSystemAccess = dbSelectByWhere("SystemAccess", "", "ORDER By Timestamp");
while ($SystemAccessDB = dbFetchArray($DBSystemAccess)) {
?>
<tr>
<?php
echo '<td>' . dbGroupNamebyId($_SESSION['PI_ID']) . '</td>';
echo '<td>' . $Status . '</td>';
echo '<td>' . $SystemAccessDB['timeStamp'] . '</td>';
echo '<td>' . '<a href="adetails.php?ID=' . $SystemAccessDB['id'] . '" >View</a> </td>'
?>
</tr>
<?php } ?>
</tbody>
How can i replace <a href="adetails.php?ID=' . $SystemAccessDB['id'] . '" >View</a> to not showing ID in the URL?
Please try this with the hyper link it is not possible to hide the GET data.We need to use POST method.
<table>
<thead>
<tr>
<th>Name</th>
<th>Status</th>
<th>Created</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$DBSystemAccess = dbSelectByWhere("SystemAccess", "", "ORDER By Timestamp");
while ($SystemAccessDB = dbFetchArray($DBSystemAccess)) {
?>
<tr>
<?php
echo '<td>' . dbGroupNamebyId($_SESSION['PI_ID']) . '</td>';
echo '<td>' . $Status . '</td>';
echo '<td>' . $SystemAccessDB['timeStamp'] . '</td>';
echo '<form name="test" method="post" action="testing.php">';
echo '<input type="hidden" name="hidden_name" value="'.$SystemAccessDB['id'].'"/>';
echo '<button type="submit">View</button>';
echo '</form>';
?>
</tr>
<?php } ?>
</tbody>
When you will click it will redirect to testing.php with hidden value 'hidden_name' on which you can further query according to that value.

how to fetch data from database and display in table format?

I am using following code to fetch data from database . its show my output correct when i insert all columns value . for example i insert NULL values for 'Hip' so its not showing in tabular format .
Please help me for the same Thanks in advance!
$sizeArray = explode(',', $rows['sizes']);
$bustArray = explode(',', $rows['bust']);
$hipArray = explode(',', $rows['hip']);
$lengthArray = explode(',', $rows['length']);
$lengthArraybottom = explode(',', $rows['lengthbottom']);
$shoulderArray = explode(',', $rows['shoulder']);
foreach ($sizeArray as $key => $value) {
foreach ($bustArray as $bkey => $bvalue) {
foreach ($waistArray as $Wkey => $Wvalue) {
foreach ($hipArray as $hkey => $hvalue) {
foreach ($lengthArray as $lkey => $lvalue) {
foreach ($lengthArraybottom as $lkeybottom => $lbottomvalue) {
foreach ($shoulderArray as $skey => $svalue) {
if ($key == $bkey && $bkey == $Wkey && $Wkey == $hkey && $hkey == $lkey && $lkey == $lkeybottom && $lkeybottom == $skey) {
echo '<tr><td>' . $value . '</td>';
echo '<td>' . $bvalue . '</td>';
echo '<td>' . Wvalue . '</td>';
echo '<td>' . $hvalue . '</td>';
echo '<td>' . $lvalue . '</td>';
echo '<td>' . $lbottomvalue . '</td>';
echo '<td>' . $svalue . '</td></tr>';
}
}
}
}
}
}
}
}
<table class="data-table">
<caption class="title">Sales Data of Electronic Division</caption>
<thead>
<tr>
<th>NO</th>
<th>CUSTOMER</th>
<th>ITEM</th>
<th>DATE</th>
<th>AMOUNT</th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
$total = 0;
while ($row = mysqli_fetch_array($query))
{
$amount = $row['amount'] == 0 ? '' : number_format($row['amount']);
echo '<tr>
<td>'.$no.'</td>
<td>'.$row['name'].'</td>
<td>'.$row['item'].'</td>
<td>'. date('F d, Y', strtotime($row['date'])) . '</td>
<td>'.$amount.'</td>
</tr>';
$total += $row['amount'];
$no++;
}?>
</tbody>
<tfoot>
<tr>
<th colspan="4">TOTAL</th>
<th><?=number_format($total)?></th>
</tr>
</tfoot>
</table>
Refrence : http://webdevzoom.com/display-mysql-data-html-5-table-using-php/

display 5 arrays values per row

I am trying to make a calendar:
<table border="1">
<tr><th colspan="7"><?php echo $current_month ?></th></tr>
<tr>
<?php foreach ($days as $day) {
echo "<th>" . $day . "</th>";
} ?>
</tr>
<tr>
<?php
foreach($keys as $row => $value) {
echo "<td>" . $value . "</td>";
}
?>
</tr>
</table>
How can I do to echo 7 values per row? AS you can see in the image, it displays all days in the same row. (of course, because I have put it in the same , but, how can I make that starts a new every 7 echo values?
Thank you!!!
You can use modulus to check if it has reached the seventh element and end the row.
<?php
foreach($keys as $row => $value) {
if ($value % 7 == 0) {
echo "<td>" . $value . "</td></tr><tr>";
} else {
echo "<td>" . $value . "</td>";
}
}
?>
An iteration counter is a pretty simple method.
<table border="1">
<tr><th colspan="7"><?php echo $current_month ?></th></tr>
<tr>
<?php foreach ($days as $day) {
echo "<th>" . $day . "</th>";
} ?>
</tr>
<tr>
<?php
$i = 0; // Begin at 0 days written
foreach($keys as $row => $value) {
if ($i == 7){
// Create new table row after every 7th iteration
print "</tr><tr>"; // Add \n or \t for output formatting
$i = 0;
}
echo "<td>" . $value . "</td>";
$i++; // Increment $i each iteration
}
?>
</tr>
</table>
Try the below code,
You can split the array into chunks of particular sizes, See more information about this from here array chunk
<?php
$current_month = 'Oct';
$days = array('1',2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31);
$chunk = array_chunk($days,7);
?>
<table border="1">
<tr>
<th colspan="7"> <?php
echo $current_month; ?>
</th>
</tr>
<?php
foreach($chunk as $day) {
echo '<tr>';
foreach($day as $key) {
echo "<th>" . $key . "</th>";
}
echo '</tr>';
} ?>
</table>

Improve combination of HTML and PHP?

Is there a better solution, apart from a template engine like smarty, for huge code like this?
<table class="table table-striped dataTable table-bordered">
<thead>
<tr>
<?php
$output = '<th class="sorting_numeric_html sorting_default_desc">' . TH_ORDERS_ID . '</th>';
$output .= '<th class="sorting_date_eu">' . TH_ORDERS_DATE . '</th>';
$output .= '<th>' . TH_ORDERS_NAME . '</th>';
$output .= '<th>' . TH_ORDERS_STATUS . '</th>';
$output .= '<th class="sorting_disabled">' . TH_ACTION . '</th>';
echo $output;
?>
</tr>
</thead>
<tbody>
<?php
$output = '';
foreach ($orders['RESULT'] as $order) {
$output .= '<tr>';
$output .= '<td class="text-right">' . inc_buildLink(inc_url(FILENAME_ORDERS, 'oID=' . $order['orders_id'] . '&action=edit'), $order['orders_id']) . '</td>';
$output .= '<td>' . inc_datetime_short($order['date_purchased']) . '</td>';
$output .= '<td>' . inc_buildLink(inc_url(FILENAME_CUSTOMERS, 'cID=' . $order['customers_id']. '&action=edit'), $order['delivery_name']) . '</td>';
$output .= '<td class="status' . $order['orders_status'] . '">' . $order['orders_status_name'] . '</td>';
$output .= '<td class="btn-group actions">';
$output .= inc_buildLink(inc_url(FILENAME_ORDERS, 'oID=' . $order['orders_id'] . '&action=edit'),
inc_image(DIR_WS_ICONS . 'edit.png', ''), TOOLTIP_EDIT, 'class="tip btn btn-mini"');
$output .= modal_delete_orders($order['customers_name'], $order['orders_id'], 'class="tip btn btn-mini"');
$output .= '</td>';
$output .= '</tr>';
}
echo $output;
?>
</tbody>
</table>
For sure Smarty would be a solution, but is there another way?
I'd do it like this, makes it easier to read as the HTML is a little bit more separated from the PHP:
<table class="table table-striped dataTable table-bordered">
<thead>
<tr>
<th class="sorting_numeric_html sorting_default_desc"><?php echo TH_ORDERS_ID; ?></th>
<th class="sorting_date_eu"><?php echo TH_ORDERS_DATE; ?></th>
<th><?php echo TH_ORDERS_NAME; ?></th>
<th><?php echo TH_ORDERS_STATUS; ?></th>
<th class="sorting_disabled"><?php echo TH_ACTION; ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($orders['RESULT'] as $order): ?>
<tr>
<td class="text-right"><?php echo inc_buildLink(inc_url(FILENAME_ORDERS, 'oID=' . $order['orders_id'] . '&action=edit'), $order['orders_id']); ?></td>
<td><?php echo inc_datetime_short($order['date_purchased']); ?></td>
<td><?php echo inc_buildLink(inc_url(FILENAME_CUSTOMERS, 'cID=' . $order['customers_id'] . '&action=edit'), $order['delivery_name']); ?></td>
<td class="status<?php echo $order['orders_status']; ?>"><?php echo $order['orders_status_name']; ?></td>
<td class="btn-group actions">
<?php echo inc_buildLink(inc_url(FILENAME_ORDERS, 'oID=' . $order['orders_id'] . '&action=edit'),
inc_image(DIR_WS_ICONS . 'edit.png', ''), TOOLTIP_EDIT, 'class="tip btn btn-mini"'),
modal_delete_orders($order['customers_name'], $order['orders_id'], 'class="tip btn btn-mini"'); ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
Instead of <php echo you can also use <?=, but lots of people dislike the use of it. It's only 4 more characters to put echo so i'd stick with that!
Yes, you normally want to separate the most you can PHP and HTML, leaving the html alone, like this:
<table class="table table-striped dataTable table-bordered">
<thead>
<tr>
<th class="sorting_numeric_html sorting_default_desc">
<?php echo TH_ORDERS_ID; ?>
</th>
<th class="sorting_date_eu">
<?php echo TH_ORDERS_DATE; ?>
</th>
...
If your system supports it, you can try instead changing the <?php echo for <?=, which is known as short tags as read in the manual, in which case the code would look much neater:
<table class="table table-striped dataTable table-bordered">
<thead>
<tr>
<th class="sorting_numeric_html sorting_default_desc">
<?= TH_ORDERS_ID; ?>
</th>
<th class="sorting_date_eu">
<?= TH_ORDERS_DATE; ?>
</th>
...
However, a couple of notes that would require you to change your code further down. They might not be for now, but they more for your future code:
Why are you echoing constants? Normally you'd like to store your data in variables and then echo them.
It's better normally to avoid putting too many classes in the html since it's the CSS what gets cached better normally. I'd go with an id="tableorders" and then simply class="id", class="date" and so on.

Categories