I'm attempting to create a form that searches JSON's datetime value, and returns the information between the first and last date the user enters. I have to admit, I do not know if I am doing this correctly, and so far I haven't been able to get it working.
My JSON data looks like this:
{
"apiVersion": "0.1",
"data": {
"offset": 0,
"limit": 50,
"count": 50,
"total": 1008,
"reports": [
{
"type": "STB_EVENT_TIMED",
"data": {
"datetime": "2014-11-04T08:25:07-08:00",
"originator": "6007",
"roomNumber": "1474",
"eventItem": "Watched movie 31008",
"duration": "P0Y0M0DT0H0M45.000S"
}
},
{
"type": "ROOM_CHARGE",
"data": {
"datetime": "2014-11-04T08:25:07-08:00",
"originator": "6006",
"roomNumber": "204",
"chargeItem": "Premium Services",
"chargeAmountCents": 495
}
},
So I have this form, where I am setting the values for each text field entered, and bringing them into my TEST.PHP page using the GET Method. The user enters the room number, and the first and last date in the format "2014-11-04".
if(isset($_GET['submit_search'])){
$room_number = $_GET['id'];
$first_date = $_GET['first_date'];
$last_date = $_GET['last_date'];
}
In my PHP script where I format the data, I was trying to create a for loop, that takes $i= $first_date; $i<=$last_date; i++ and display each line item from the JSON that are between the users set dates.
Here is the snippet of my code that I have where I'm trying to output this information:
<?php $matchFound = false; ?>
<?php foreach($output1['data']['reports'] as $reporting): ?>
<?php if($reporting['type'] == "ROOM_CHARGE" && $reporting['data']['roomNumber'] == $room_number): ?>
<?php for ($i=$first_date; $i<=$last_date; $i++): ?>
<tr>
<td><?php echo 'Room Charge'; ?></td>
<td><?php echo substr($reporting['data']['datetime'], -26, 10) ?></td>
<td><?php echo substr($reporting['data']['datetime'], -14, 8) ?></td>
<td><?php echo ($reporting['data']['roomNumber']) ?> </td>
<td><?php echo ($reporting['data']['originator']) ?> </td>
<td><?php echo ($reporting['data']['chargeItem']) ?></td>
<td><?php echo number_format(($reporting['data']['chargeAmountCents'])/100, 2, '.', ',') ?></td>
<td>-</td>
</tr>
<?php endfor; ?>
<?php elseif($reporting['type'] == "STB_EVENT_TIMED" && $reporting['data']['roomNumber'] == $room_number): ?>
<?php for ($i=$first_date; $i<=$last_date; $i++): ?>
<tr>
<td><?php echo 'Event'; ?></td>
<td><?php echo substr($reporting['data']['datetime'], -26, 10) ?></td>
<td><?php echo substr($reporting['data']['datetime'], -14, 8) ?></td>
<td><?php echo ($reporting['data']['roomNumber'])?> </td>
<td><?php echo ($reporting['data']['originator']) ?> </td>
<td><?php echo ($reporting['data']['eventItem']) ?></td>
<td>-</td>
<td><?php echo substr($reporting['data']['duration'], -10, 2) ?>:<?php echo substr($reporting['data']['duration'], -7, 2) ?>:<?php echo substr($reporting['data']['duration'], -4, 2) ?></td>
</tr>
<?php endfor; ?>
<?php endif; ?>
<?php endforeach; ?>
I'm getting hung up on the for loop. I'm not sure if this is how it's supposed to be written? Does anyone have an idea if this would even work correctly? If there is another way that I can achieve this, please point me in the right direction. I would really like to figure this out.