【发布时间】:2016-06-13 06:06:53
【问题描述】:
我正在尝试创建一个评论系统,对每个评论都有回复功能。过去 O 使用本机 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 文件中出现了一些问题。但首先请查看我的 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();
}
但问题就在这里。我应该编写什么代码而不是 ??? ?我 php native 我只是重新调用我的 getComments 函数,但我能在这里做什么?我该怎么做才能获得具有parent_id 的 cmets?
<?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 } ?>
【问题讨论】:
-
你能给出你的表结构和那个表的十个演示数据吗?
标签: php codeigniter frameworks