首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Codeigniter创建评论系统

用Codeigniter创建评论系统
EN

Stack Overflow用户
提问于 2016-06-13 14:06:53
回答 1查看 2.3K关注 0票数 0

我正在尝试创建一个评论系统,对每个comment.In都有回复功能,过去我用原生php写了这个评论系统,但是现在,当我尝试用codeigniter重写它的时候,它出现了一些问题。

当我用原生php编写这个注释系统时,我有一个包含以下代码的HTML文件:

代码语言:javascript
复制
<?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()函数:

代码语言:javascript
复制
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'>&nbsp;-&nbsp;" . $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

代码语言:javascript
复制
public function detail($product_id){
    $data['comments'] = $this->products_model->get_comments($product_id);
    $this->load->view('pages/product-detail', $data));
}

还有我的Model

代码语言:javascript
复制
  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的评论?

代码语言:javascript
复制
<?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'>&nbsp;-&nbsp;<?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 } ?>
EN

回答 1

Stack Overflow用户

发布于 2016-06-13 15:59:17

有许多方法可以尝试获取注释数据。一种想法是你可以调用你的模型函数来获得视图文件中的注释。

代码语言:javascript
复制
            <?php if ($comment['parent_id']) { ?>
                <ul>
                    $this->products_model->get_comments($parent_id);
                </ul>
            <?php } ?>

另一个想法是对get_comments模型函数进行更改,以包括所有父子数据...

代码语言:javascript
复制
  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函数来获取评论

代码语言:javascript
复制
 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应用中一样。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37782800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档