コントローラーとビューのみを使用しています。
次のコードを使用して、返信のある投稿と投稿を表示しています。
次のコントローラーとビューを使用して、投稿と返信を表示しました。
クエリ 1 を使用して、投稿テーブルからトピック ID (eg.topic id=34) の投稿を選択し、これを使用して投稿 ID 30 および 31 を取得しています。
クエリ 2 を使用して、返信テーブルから投稿 ID ごとに返信を選択しています。
返信 ID 1 を持つ投稿 ID 30 と
返信 ID 2 を持つ投稿 ID 31 とします。
2番目のforループを使用して、返信テーブルのparent_id列を使用して別の返信があるかどうかを選択しています(showreply()関数への再帰呼び出しを使用します)
ビューで再帰関数 showreply() から結果を渡す方法に行き詰まっています。
私のコードを使用して、私は表示します:
投稿 30
--- 投稿 30 の最初の返信
投稿 31
--- 投稿 31 の返信
しかし、私は好きに見せたいです。
投稿 30
--- 投稿 30 の最初の返信--- 最初
の返信
投稿 31への返信 --- 投稿 31
の最初の返信 --- 最初の返信
への返信
親IDを使用して返信の返信を取得するために再帰関数呼び出しを使用しましたが、これをビューに渡す方法がわかりません。
コントローラ:
<?php
public function viewpost()
{
//Query 1
$topicid = trim($this->input->get('topicid'));
$q =$this->db->select(array(
'fp.id as id',
'fp.postdata',
'fp.topicid'))
->from('forum_post AS fp')
->where('fp.topicid',$topicid)
->order_by('fp.id DESC')->limit(0,10)->get();
$resultq1 = $q->result_array();
$data['resultq1'] = $resultq1;
//$data['resultq1'] = $res;
$resultq2 = array();
foreach ($resultq1 as $rec)
{
//Query 2
$postid = $rec['id'];
$q1 =$this->db->select(array(
'fr.id as id',
'fr.reply_data'))
->from('forum_reply AS fr')
->where('fr.postid',$postid)
->order_by('fr.id ')->get();
$resultq2[$postid] = $q1->result_array();
$data["resultq2"][$postid] = $resultq2[$postid];
foreach($q1->result_array() as $row1)
{
//second for loop
$reply_id = $row1['id'];
$resultq3[$reply_id] = $this->showreply($reply_id); // call to function
$data["resultq3"] = $resultq3[$reply_id];
}//inner for loop
} //outer for loop
$this->load->view('viewpost',$data);
}
public function showreply($reply_id)
{
$reply_id1 = $reply_id;
$q1 =$this->db->select(array(
'fr.id as id',
'fr.reply_data',
'fr.parent_id'))
->from('forum_reply AS fr')
->where('fr.parent_id',$reply_id1)
->order_by('fr.id ')->get();
//print $this->db->last_query();
$resultq4[$reply_id1] = $q1->result_array();
$data["resultq4"]= $resultq4[$reply_id1];
$i=0;
foreach($q1->result_array() as $row4)
{
print_r($q1->result_array());
echo "id".$id = $row4['id'];
$parent_id = $row4['parent_id'];
if($parent_id!=0)
{
//$data['nested'][$i] = $q1->result_array();
$this->showreply($id); //recursive call to function
$i++;
}
}
return $resultq4;
}
?>
応答テーブルのテーブル構造:
Rep_id rep_text post_id Parent_ID
-------------------------------------------------------------------------
1 Reply for post 30 30 null
2 Reply for post 31 31 null
3 reply to Rep_id 1 null 1
4 Rep_id 3 have Rep_id 4 null 3
5 Reply for post 31 null 2
6 Reply for Rep_id 5 null 5
----------------------------------------------------------------------------
ポストテーブル:
post_id topic id post_title post_desc
-----------------------------------------
30 34 xyz sssss
31 34 pqr tyyyu
----------------------------------------
*ビュー: *
<div>
<?php foreach($resultq1 as $row)
{ ?>
<ul>
<li></li> // used to display post
</ul>
<?php foreach($resultq2 as $rows)
{
foreach($rows as $row1)
{
if($row['id']==$row1['postid'])
{ ?>
<ul>
<li></li> // used to display reyly for post
</ul>
<?php foreach($resultq3 as $rows)
{
foreach($rows as $row2)
{
if($row1['id']==$row2['parent_id'])
{ ?>
<ul>
<li></li> // used to display reply for reply
</ul>
<?php
}//if
} //inner for $row2
} // outr for $resultq3
} //if
} //inner for of $row1
}//outer for $resultq2
} ?>
</div>