2017-08-23 11 views
0

ここで私はほとんどの関連する回答をstackoverflowで調べましたが、私の場合に適したものを見つけることができませんでした。PHP pdoを使用してJSONネストされたオブジェクトを作成する方法

どんな種類の返信も高く評価されます。私は

  • news_id
  • comment_namesをコメントをという名前の2つのMySQLテーブルからデータを取得し、comment_id

    • コメントテーブルの下の値でcomments_replyするPHP PDOを使用しています

    • COMMENT_DATE

    comment_replyテーブル

    • をreply_id私が代わりにこのようなJOSNフォーマットされた結果、その結果として戻って空の配列を取得し、返信
    • REPLY_DATE

  • reply_names
  • をcomment_id:

    [ 
        {"id": 1, 
        "comment_date": "2017-08-09", 
        "comment_time": "06:10:00", 
        "names": "Imenwo Alex", 
        "img": "c1.jpg", 
        "comments": "<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,There are many variations of passages of Lorem Ipsum available, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,There are many variations of passages of Lorem Ipsum available, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>", 
        "replies":[ 
         { 
           "id": 1, 
           "reply_date": "2017-08-09", 
           "reply_time": "06:10:00", 
           "names": "frank Alex", 
           "img": "c1.jpg", 
           "reply": "<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,There are many variations of passages of Lorem Ipsum available, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,There are many variations of passages of Lorem Ipsum available, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>" 
          }, 
          { 
           "id": 2, 
           "reply_date": "2017-08-09", 
           "reply_time": "06:10:00", 
           "names": "frank Alex", 
           "img": "c1.jpg", 
           "reply": "<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,There are many variations of passages of Lorem Ipsum available, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,There are many variations of passages of Lorem Ipsum available, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>" 
          } 
        ] 
        }, 
        {"id": 2, 
        "comment_date": "2017-08-09", 
        "comment_time": "06:10:00", 
        "names": "Imenwo Alex", 
        "img": "c1.jpg", 
        "comments": "<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,There are many variations of passages of Lorem Ipsum available, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,There are many variations of passages of Lorem Ipsum available, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>", 
        "replies":[ 
          { 
           "id": 1, 
           "reply_date": "2017-08-09", 
           "reply_time": "06:10:00", 
           "names": "frank Alex", 
           "img": "c1.jpg", 
           "reply": "<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,There are many variations of passages of Lorem Ipsum available, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,There are many variations of passages of Lorem Ipsum available, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>" 
          } 
         ] 
        } 
    ] 
    
    以下は

    私のPHPスクリプトです:

    // 
         $q = "SELECT * FROM comments WHERE comments.news_id =:id";   
         //prepare 
         $stmt = $this->DB_con->prepare($q); 
         //bind parameters 
         $stmt->bindParam(':id', $id); 
    
         //create an array 
         $json_response = array(); 
         while($obj=$stmt->fetch(PDO::FETCH_OBJ)) { 
    
          $currentComments = array(
           'id' => $obj->comment_id, 
           'comment_names' => $obj->comment_names, 
           'comment_date' => $obj->comment_date, 
           'comment_time' => $obj->comment_time, 
           'comment_img' => $obj->comment_img, 
           'comments' => $obj->comments, 
           'replies' => array() 
         ); 
    
          // 
          $r = "SELECT * FROM comments_reply WHERE comments_reply.comment_id =:id"; 
          //prepare 
          $stmt = $this->DB_con->prepare($r); 
          //bind parameters 
          $stmt->bindParam(':id', $obj->comment_id); 
          while($obj=$stmt->fetch(PDO::FETCH_OBJ)) { 
    
           $currentComments['replies'][] = array(
            'id' => $obj->reply_id, 
            'reply_names' => $obj->reply_names, 
            'reply_date' => $obj->reply_date, 
            'reply_time' => $obj->reply_time, 
            'reply_img' => $obj->reply_img, 
            'reply' => $obj->reply 
          ); 
    
          } 
          array_push($json_response, $currentComments); //push the values in the array 
    
         } 
         return json_encode($json_response); 
    
  • 答えて

    0

    $ stmtはと$ OBJを上書きしないでください。このような何か:

     $q = "SELECT * FROM comments WHERE comments.news_id =:id";   
         //prepare 
         $stmt = $this->DB_con->prepare($q); 
    
         $r = "SELECT * FROM comments_reply WHERE comments_reply.comment_id =:id"; 
         //prepare 
         $stmt2 = $this->DB_con->prepare($r); 
    
         //execute with bound parameter 
         $stmt->execute(array(':id'=> $id)); 
    
         //create an array 
         $json_response = array(); 
    
         while($obj=$stmt->fetch(PDO::FETCH_OBJ)) { 
    
          $currentComments = array(
           'id' => $obj->comment_id, 
           'comment_names' => $obj->comment_names, 
           'comment_date' => $obj->comment_date, 
           'comment_time' => $obj->comment_time, 
           'comment_img' => $obj->comment_img, 
           'comments' => $obj->comments, 
           'replies' => array() 
         ); 
    
          // 
          $stmt2->execute(array(':id'=> $obj->comment_id)); 
    
          while($obj2=$stmt2->fetch(PDO::FETCH_OBJ)) { 
    
           $currentComments['replies'][] = array(
            'id' => $obj2->reply_id, 
            'reply_names' => $obj2->reply_names, 
            'reply_date' => $obj2->reply_date, 
            'reply_time' => $obj2->reply_time, 
            'reply_img' => $obj2->reply_img, 
            'reply' => $obj2->reply 
          ); 
    
          } 
          array_push($json_response, $currentComments); //push the values in the array 
    
         } 
         return json_encode($json_response); 
    
    +0

    私はまだ空の配列は[]を返す取得し、私はこの行をコメントアウトする場合// $ json_response =配列()ので、ループが実行されていない間、その最初の間の文のように思えます。その戻り値未定義の変数:json_responseエラー –

    +0

    は、execute() - editedを追加する必要があります。 – olegsv

    +0

    @AlexImenwoあなたのデータベースの例を投稿すると、1つの返信と1つのコメントで十分でしょう... – olegsv

    関連する問題