doucheng9058 2015-01-29 17:03
浏览 54
已采纳

PHP加入MySQL表,输出格式化JSON

I'm having a difficult time trying to join two MySQL tables in my PHP Script, and then format it to JSON to be interpreted by javascript in my private app. The app is only for our art team to make edits to various products our company makes.

The purpose of this process is to create a listing of products that need attention, and a checklist of "tasks" within each product that can be checked off as they are completed (hence the "check" field).

The tables are formatted as such:

products table:

+------+-----------+-----------+---------+
|  id  |  task_id  |  product  |  notes  |
+------+-----------+-----------+---------+
|  10  |     1     |   Item 1  |         |
+------+-----------+-----------+---------+
|  11  |     2     |   Item 2  |         |
+------+-----------+-----------+---------+

tasks table:

+------+-----------+---------------+---------+
|  id  |  task_id  |      task     |  check  |
+------+-----------+---------------+---------+
|   1  |     1     |  Fix Box Art  |     0   |
+------+-----------+---------------+---------+
|   2  |     1     | Add Copyright |     1   |
+------+-----------+---------------+---------+
|   3  |     2     |  Update Text  |     0   |
+------+-----------+---------------+---------+

I've been able to join the tables in PHP with the following code:

SELECT 
   products.*, 
   GROUP_CONCAT(tasks.task, tasks.check) AS steps 
FROM products
LEFT JOIN task_steps ON products.task_id = task.task_id

Within a while loop, I am building an array like such:

$output[] = 
      array (
      "id" => $row['id'],
      "task_id" => $row['task_id'],
      "product" => $row['product'],
      "notes" => $row['notes'],
      "tasks" => $row['steps']
    );

I then output the array as JSON:

echo json_encode($output);

Here is where I am stuck. I've been able to output some JSON but nothing close to what I am looking for. The format I am looking for would look something like this:

[  
   {  
      "id":"10",
      "task_id":"1",
      "product":"Item 1",
      "notes":"",
      "tasks":[  
         {  
            "task":"Fix Box Art",
            "check":"0"
         },
         {  
            "task":"Add Copyright",
            "check":"1"
         }
      ]
   }
]

Can anyone nudge me in the right direction in how I should be formatting the table data into the mentioned JSON format, or perhaps suggest an alternative method if I am heading in the wrong direction all together?

  • 写回答

1条回答 默认 最新

  • douyu0725 2015-01-29 17:07
    关注

    Push_back each task inside your loop:

    while(...fetching) {
    
      if (!isset($output[$row['id']])) {
        $output[$row['id']] = array (
          "id" => $row['id'],
          "task_id" => $row['task_id'],
          "product" => $row['product'],
          "notes" => $row['notes'],
          "tasks" => array()
       );
      }
    
      $output[$row['id']]['tasks'][] = array(
        'task' => $row['task'],
        'check' => $row['check']
      );
    }
    

    In this way tasks is effectively an array

    You will need to retrive the other table cols too

    SELECT 
       products.*,
       tasks.*      //<<--
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误