我正在尝试创建一个评论系统,对每个comment.In都有回复功能,过去我用原生php写了这个评论系统,但是现在,当我尝试用codeigniter重写它的时候,它出现了一些问题。
当我用原生php编写这个注释系统时,我有一个包含以下代码的HTML文件:
<?php
$q = "SELECT * FROM productcomments WHERE productId = '$getProductId' AND parentid='0' AND isconfrim!=0";
$r = mysqli_query($xcon, $q);
while ($row = mysqli_fetch_assoc($r)):
getComments($row);
endwhile;
?>这是getComments()函数:
function getComments($row){
global $xcon;
echo "<li class='comment' id='" . $row['commentId'] . "'>";
echo "<div class='commentInfo'>";
echo "<div class='aut'>" . $row['userName'] . "</div>";
echo "<div class='timestamp'> - " . $row['date'] . "</div>";
echo "</div>";
echo "<div class='comment_body'>" . $row['comment'] . "</div>";
echo "<a href='#comment_form' class='reply' id='" . $row['commentId'] . "'>پاسخ به این نظر</a>";
$q = "SELECT * FROM productcomments WHERE parentid = " . $row['commentId'] . " AND isconfrim=1";
$r = mysqli_query($xcon, $q);
if (mysqli_num_rows($r) > 0) {
echo "<ul>";
while ($row = mysqli_fetch_assoc($r)) {
getComments($row);
}
echo "</ul>";
}
echo "</li>";
}现在,当我尝试用codeigniter框架重写上面的代码时,我在我的view file.But中遇到了一些问题,首先看看我的Controller:
public function detail($product_id){
$data['comments'] = $this->products_model->get_comments($product_id);
$this->load->view('pages/product-detail', $data));
}还有我的Model
public function get_comments($product_id){
$this->db->where('product_id', $product_id);
$this->db->where('is_confirm', '1');
$query = $this->db->get('xbl_product_comments');
return $query->result_array();
}但问题是,我应该用代码而不是???来做here.What吗?我本机我只是重新调用我的getComments函数,但是我能做什么呢?我该怎么做才能得到带有parent_id的评论?
<?php foreach ($comments as $comment) { ?>
<li class='comment' id='<?php echo $comment['id'] ?>'>
<div class='commentInfo'>
<div class='aut'><?php echo $comment['user_name'] ?></div>
<div class='timestamp'> - <?php echo $comment['date'] ?></div>
</div>
<div class='comment_body'><?php echo $comment['comment'] ?></div>
<a href='#comment_form' class='reply' id='<?php echo $comment['id'] ?>'>reply</a>
<?php if ($comment['parent_id']) { ?>
<ul>
??? // What should I code here ?
</ul>
<?php } ?>
</li>
<?php } ?>发布于 2016-06-13 15:59:17
有许多方法可以尝试获取注释数据。一种想法是你可以调用你的模型函数来获得视图文件中的注释。
<?php if ($comment['parent_id']) { ?>
<ul>
$this->products_model->get_comments($parent_id);
</ul>
<?php } ?>另一个想法是对get_comments模型函数进行更改,以包括所有父子数据...
public function get_comments($product_id){
$this->db->where('product_id', $product_id);
$this->db->where('is_confirm', '1');
$query = $this->db->get('xbl_product_comments');
$data = array();
foreach($query->result_array() as $row) {
$data[] = $row;
if($row['parent_id'] != 0) {
$data = $this->get_comments($parent_id);
}
}
return $data;
}请注意,我没有测试它,所以它可能有错误。只要根据您的需要进行更改即可。
另一个想法是简单地创建一个helper函数来获取评论
function get_comments($product_id){
$ci = &get_instance();
$ci->db->where('product_id', $product_id);
$ci->db->where('is_confirm', '1');
$query = $ci->db->get('xbl_product_comments');
return $query->result_array();
}然后简单地在你的视图中调用这个函数,就像在你的‘原生’php应用中一样。
https://stackoverflow.com/questions/37782800
复制相似问题