I use only the controller and view.
using the following code, I display a message and a response message.
I used the following controller and View to display the message and response.
Using query 1, I select the post for the topic id (eg.topic id = 34) from the mail table and using this get post id 30 and 31.
Using query 2, I select the response for each message identifier from the response table.
let's say message id 30 with response id 1 and
message id 31 with response ID 2.
using the 2nd loop, I choose if any answer has anothr response using the parent_id column from the answer table. (use the showreply () function recursive call)
I am stuck in the way I pass the result from the showreply () recursive function.
Using my code, I say:
Post 30
--- first reply for message 30
message 31
--- response for message 31
but I want to show how.
Post 30
--- first response for message 30
--- response to first response
message 31
--- first response for message 31
--- response to first response
I used a recursive function call to get the response for the response using the parent id, but I don’t understand how to pass this for viewing.
Controller:
<?php
public function viewpost()
{
$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;
$resultq2 = array();
foreach ($resultq1 as $rec)
{
$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)
{
$reply_id = $row1['id'];
$resultq3[$reply_id] = $this->showreply($reply_id);
$data["resultq3"] = $resultq3[$reply_id];
}
}
$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();
$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)
{
$this->showreply($id);
$i++;
}
}
return $resultq4;
}
?>
Table structure for response table:
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
----------------------------------------------------------------------------
Message table
post_id topic id post_title post_desc
-----------------------------------------
30 34 xyz sssss
31 34 pqr tyyyu
----------------------------------------
* View: *
<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
}
}
}
}
}
}
} ?>
</div>