0

输出错误,我得到了三个相同的评论,而不是只有一个。任何人都可以帮助只发表一条评论。

这里我查看php代码:

$modelid = "5" ;

$query = $this->db->query('SELECT message FROM message WHERE modelid = '.$modelid.' ');

$row = $query->row(); 

echo $row->message;

和我的表:

http://dev.interactive-creation-works.net/Stack/table.png

控制器:

        class Comment extends CI_Controller
{
    function index()
    {
        $data['result'] = $this->db->get('message')->result();
        $this->load->view('commentView',$data);

    }
    function insert()
    {

     $this->load->model('commentjquery');
     echo $this->commentjquery->inserttodb();
    }
}
4

2 回答 2

0

This view should only show one message, even if you do - for whatever reason, correctly or not - get three messages from the query. You may be loading the view three times in your controller. Try adding some static text to the top and bottom of your view, and see if it shows up multiple times.

Edit:

Now that you've posted the controller, I can see that's not the issue. But I notice you're trying to grab the comments from the database twice - once in the controller and once in the model. The one in the controller is actually the wrong way to do it unless you've implemented an ActiveRecord model for your comments, and you're not using the results anyway. Another thing I noticed just now is that you're calling $query->row() instead of running a foreach over $query->result(). Try this:

foreach($query->result() as $row) {
    echo $row->message;
}

Edit 2: Or maybe it is the problem, if the code snippet you posted isn't the entire view, which seems to be the case.

于 2013-05-30T21:13:20.163 回答
0

你没有得到错误真的很奇怪。当你得到三个输出时,它会变得更奇怪。

将您的查询更改为此。

$query = $this->db->query('SELECT message FROM message WHERE modelid = "'.$modelid.'"');
于 2013-05-30T21:34:53.890 回答