创建表单以使用“datetime”搜索日期范围的JSON值 [英] JSON creating a form to search a date range using "datetime" value

查看:106
本文介绍了创建表单以使用“datetime”搜索日期范围的JSON值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个搜索JSON的 datetime 值的表单,并返回用户输入的第一个和最后一个日期之间的信息。我不得不承认,我不知道我是否正确地做到这一点,到目前为止我还没有得到它的工作。

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.

我的JSON数据看起来像这个:

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
        }
      },

所以我有这个表单,我在哪里设置输入的每个文本字段的值,并将它们使用GET方法进入我的TEST.PHP页面。用户输入房间号,第一个和最后一个日期格式为2014-11-04。

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'];
}

在我格式化数据的PHP脚本中,我正在尝试创建一个for循环,需要 $ i = $ first_date; $ i< = $ last_date; i ++ 并显示来自用户设置日期之间的JSON中的每个行项目。

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; ?>

我正在挂上 for循环。我不知道这是怎么写的?有没有人有这样的想法,甚至可以正常工作?如果有另一种方式可以实现这一点,请指出我正确的方向。我真的很想弄清楚这一点。

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.

推荐答案

你似乎错过了一条与 endfor 以匹配<?php for($ i = $ first_date; $ i = $ last_date; $ i ++):?>

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

代码中的第二个可能的问题是($ i = $ first_date; $ i = $ last_date; $ i ++) - 你如何在网页上输入日期? (我看到你使用$ _GET)你确定他们会是两个数字,像20141031和20141107?如果是这样,循环会给你不现实的日期像20141099,价格计算可能是错误的。

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.

第三个可能的问题是将循环< TR>中的($ i = $ first_date; $ i = $ last_date; $ i ++) ..< / TR> - 您将有很多列,而不是表的许多行。

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.

这篇关于创建表单以使用“datetime”搜索日期范围的JSON值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆