【问题标题】:Create comment system with Codeigniter使用 Codeigniter 创建评论系统
【发布时间】: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'>&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 文件中出现了一些问题。但首先请查看我的 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'>&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 } ?>

【问题讨论】:

  • 你能给出你的表结构和那个表的十个演示数据吗?

标签: php codeigniter frameworks


【解决方案1】:

您可以通过多种方式尝试获取 cmets 数据。一个想法是您可以调用模型函数来获取视图文件中的 cmets,例如

            <?php if ($comment['parent_id']) { ?>
                <ul>
                    $this->products_model->get_comments($parent_id);
                </ul>
            <?php } ?>

另一个想法是更改您的 get_cmets 模型函数以包含所有父子数据...

  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;
}

请注意我没有测试它,所以它可能有错误。只需根据您的需要进行更改即可。

还有一个想法是简单地创建一个辅助函数来获取 cmets

 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 应用程序中一样在视图中调用此函数。

【讨论】:

    猜你喜欢
    • 2014-12-21
    • 1970-01-01
    • 2019-02-23
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    • 1970-01-01
    • 2011-08-16
    相关资源
    最近更新 更多