2017-09-18 10 views
-1

私が書いたこのモデルを使用することによって、私は各コ​​メントのデータベースから回答を得ることしかできません。データベースからのすべてのコメントの回答を表示

今、コメントのすべての回答を表示したい。どうすればよいですか?

ポストモデル

<?php 

class Model_post extends Model 
{ 
    function postComment($url) 
    { 
     $sql = "select * from tbl_postcomment where posttitle=? and parent=0"; 
     $result = $this->doSelect($sql, [$url]); 
     $sql = "select * from tbl_postcomment where parent!=0"; 
     $all_answers = $this->doSelect($sql); 
     $all_answer_new = []; 
     foreach ($all_answers as $answer) { 
      $question_id = $answer['parent']; 
      $all_answer_new[$question_id] = $answer; 
     } 
     return [$result, $all_answer_new]; 
    } 
} 

?> 

ポストコントローラ

<?php 

class Post extends Controller 
{ 
    function id($url) 
    { 
     $postComment = $this->model->postComment($url); 
     $qestions=$postComment[0]; 
     $answers=$postComment[1]; 
     $data = [$qestions,$answers]; 
     $this->view('post/index', $data); 
    } 
} 

?> 
+1

それぞれの質問に複数の回答があると思われる場合は、おそらく '$ all_answer_new [$ question_id] [] = $ answer;'が必要です。以前のものを上書きするのではなく、答えの配列を構築する。 – mr12086

+0

'$ all_answer_new [$ question_id] = $ answer;を' $ all_answer_new [$ question_id] [] = $ answer; 'に置き換えてください。 plsはコーディングに役立ちます! – Komeil

答えて

0

$question_idは、質問の答えの上に、ループ内で同じ値を持っています。あなたのコードは以前の回答を上書きします。そして、あなたは1つの質問につき1つの答えしか見ることができません。

は、(それ自体で配列され、どの$all_answer_new質問のすべてのすべての答えを保持する2次元配列である。。)$all_answer_new[$question_id]への回答を追加してみ

このようなもので$all_answer_new[$question_id] = $answer;を置き換えます

if (empty($all_answer_new[$question_id])) { 
    $all_answer_new[$question_id] = array(); 
} 
$all_answer_new[$question_id][] = $answer; 
関連する問題