【问题标题】:Insert results of multiple tables into one json_encode()将多个表的结果插入一个 json_encode()
【发布时间】:2013-04-27 23:01:48
【问题描述】:

请原谅我的语言错误,我会说法语。

我在这个网站和其他地方做了一些研究,但我发现并没有解决我的问题。

我正在做一个人们可以发布他们的故事的网站。我正在制作一个页面,作者应该能够看到他的故事和每个章节的章节。

我对两个表进行查询以获取故事和章节的标题。我有一个“故事”表,其中有每个故事的标题和摘要(以及其他内容,如 id 和出版日期)。还有一个叫做“chapters”的表,其中有每章的标题和它们所属的故事的id。

我想稍后在我的 html 中检索这些项目,以获得类似这样的内容:


故事标题 1

第一章

第 2 章


所以我做了一个 json_encode() 但 JSONLint 说我得到的 json 是无效的。 PHP 对我来说是新手,我开始学习所以我可能没有正确编写我的代码。

这是我的代码:

<?php
session_start();
require_once('connexion_db.php');

if($db=connect()){
    $userID = $_SESSION['id'];

    $arr = array();

    $reqRecits = $db->query('SELECT titre, id FROM recits WHERE user_id = '.$userID.'');
    $resRecits = $reqRecits->fetchAll(PDO::FETCH_ASSOC);

    foreach ($resRecits as $r){
        $recitID = $r['id'];
        $recitTitre = $r['titre'];

        $reqChapitres = $db->query('SELECT id, titre_chapitre FROM chapitres WHERE recit_id = '.$r['id'].'');
        $resChapitres = $reqChapitres->fetchAll(PDO::FETCH_ASSOC);

        foreach ($resChapitres as $c){
            $chapTitre = $c['titre_chapitre'];
        }

        $arr = array('titre'=>$recitTitre,'chapitre'=>$chapTitre);
        echo json_encode($arr, JSON_UNESCAPED_UNICODE);
    }

}else{
    echo "bdd non connectée.";
}
?>

我测试了here提出的方法,但结果更糟。 我不知道该怎么办:(

感谢您的帮助!

【问题讨论】:

  • 你能告诉我们你在$arr和json_encoded字符串的输出中得到了什么吗??

标签: php mysql json


【解决方案1】:

您似乎希望内部循环更像这样:

$chapTitre = array();
foreach ($resChapitres as $c){
  $chapTitre[] = $c['titre_chapitre'];
}

因此,您生成的 JSON 将包含所有章节标题。

此外,就目前而言,您列出了一系列未连接的 JSON 对象,它们可能应该是一个数组才能成为合法的 JSON 对象。

foreach ($resRecits as $r){
    $recitID = $r['id'];
    $recitTitre = $r['titre'];

    $reqChapitres = $db->query('SELECT id, titre_chapitre FROM chapitres WHERE recit_id = '.$r['id'].'');
    $resChapitres = $reqChapitres->fetchAll(PDO::FETCH_ASSOC);

    $chapTitre = array();
    foreach ($resChapitres as $c){
      $chapTitre[] = $c['titre_chapitre'];
    }

    $arr[] = array('titre'=>$recitTitre,'chapitre'=>$chapTitre);
}

echo json_encode($arr, JSON_UNESCAPED_UNICODE);

将它们全部收集并作为列表输出。

[
  { 
    'titre': 'Title 1',
    'chaptitre': ['Chapter 1', 'Chapter 2']
  },
  { 
    'titre': 'Title 2',
    'chaptitre': ['Chapter 1', 'Chapter 2', 'Chapter 3']
  },
]

【讨论】:

  • 这似乎是正确的结果。这是有效的。非常感谢,我已经花了好几个小时了!
【解决方案2】:
<?php
session_start();
require_once('connexion_db.php');

if($db=connect()){
    $userID = $_SESSION['id'];

    $arr = array();

    $reqRecits = $db->query('SELECT titre, id FROM recits WHERE user_id = '.$userID.'');
    $resRecits = $reqRecits->fetchAll(PDO::FETCH_ASSOC);
    $chapTitres = array();
    foreach ($resRecits as $r){
        $recitID = $r['id'];
        $recitTitre = $r['titre'];

        $reqChapitres = $db->query('SELECT id, titre_chapitre FROM chapitres WHERE recit_id = '.$r['id'].'');
        $resChapitres = $reqChapitres->fetchAll(PDO::FETCH_ASSOC);


        foreach ($resChapitres as $c){
            $chapTitres[] = $c['titre_chapitre'];
        }



        $arr[] = array('titre' => $recitTitre, 'chaptitres' => $chapTitres);
    }

    echo json_encode($arr, JSON_UNESCAPED_UNICODE);

}else{
    echo "bdd non connectée.";
}
?>

【讨论】:

    【解决方案3】:
    >  For Me this is Worked !
    
    
     <?php
        header('content-type: application/json');
        date_default_timezone_set('Asia/Dhaka');
        $DatabaseName = "";
        $HostPass = "";
        $HostUser = "";
        $HostName = "LocalHost";
        $db_connect = new mysqli($HostName, $HostUser, $HostPass, $DatabaseName);
    
        $userid = $_GET['id'];
        $status = $_GET['status'];
        $rows = array();
        $datas = array();
    
        $result = mysqli_query($db_connect, "SELECT id FROM drivers WHERE phone = '$userid'");
        if ($result)
        {
            $row = mysqli_fetch_assoc($result);
            $id = $row['id'];
    
            $result = mysqli_query($db_connect, "SELECT `delivery_id` , `date`, `time` FROM `driver_orders` WHERE driver_id='$id' AND `status` ='$status'");
            mysqli_set_charset($db_connect,"utf8");
            while ($row = mysqli_fetch_assoc($result))
            {
    
                $d_id = $row['delivery_id'];
    
                $data = mysqli_query($db_connect, "SELECT r_name,r_number,r_zone,r_address,amount FROM deliveries  WHERE id = '$d_id'");
                while ($row2 = mysqli_fetch_assoc($data))
                {
                    $datas[] = array(
                        "r_name" => $row2['r_name'],
                        "delivery_id" => $row['delivery_id'],
                        "time"=> $row['time'],
                        "date"=> $row['date'],
                        "r_number" => $row2['r_number'],
                        "r_zone" => $row2['r_zone'],
                        "amount" => $row2['amount'],
                        "r_address" => $row2['r_address']
    
                    );
    
                }
    
    
    
            }
    
        }
    
        echo json_encode($datas);
    
        ?>
    

    【讨论】:

      猜你喜欢
      • 2017-01-05
      • 2014-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多