0

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.

6
  • This doesn't look right to me: "for ($i=$first_date; $i=$last_date; $i++):" - shouldn't it be something more like: "for ($i = $first_date; $i <= $last_date; $i++):" ? Commented Nov 4, 2014 at 21:50
  • @peaceoutside Oh yeah, I changed that. I'll update my question. It still doesn't seem to be working correctly, even though the code works. I'm not sure if it is written correctly. Commented Nov 4, 2014 at 22:12
  • Are you getting an error message? What exactly is going wrong? Commented Nov 4, 2014 at 22:16
  • @peaceoutside it is doubling everything. If 4 events occurs on 2014-11-02, and I run the code, it brings back 8 events on 2014-11-02. And it actually isn't working correctly, as it is NOT bringing back the dates between what the user selects. For example. $first_date = 2014-11-02 and $last_date = 2014-11-03, my code doubles up on everything, and brings back all data, including other dates like 2014-11-04. lol, dang it. Commented Nov 4, 2014 at 22:31
  • 1
    Ok, thank you for the tips. I'll keep at it. Commented Nov 4, 2014 at 23:49

1 Answer 1

0

You seem to miss a line with endfor to match the <?php for ($i=$first_date; $i=$last_date; $i++): ?>

A second possible problem in your code is the loop for ($i=$first_date; $i=$last_date; $i++) - how are you entering dates on the web page? (I see you're using $_GET) Are you sure they will be two numbers, like 20141031 and 20141107? If so, looping will give you non-existing dates like 20141099, and the price calculation might be wrong.

A third possible problem is to put the loop for ($i=$first_date; $i=$last_date; $i++) inside the <TR> .. </TR> - you will have many columns instead of many rows for your table.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.