0

此应用程序的目标是能够从一个视图单击链接以从另一个视图获取数据。第一个视图效果很好,我也得到了正确的 PK。当我单击链接时,我遇到了问题。

从此视图获取“消息:为 foreach() 提供的参数无效”:

<?php
    echo "<h2>View</h2>";
    foreach ($rows as $r){
        echo '<br /><h3>'; echo $r->tHandle; echo'</h3>';
        echo "<li>Sent at:  "; echo $r->content; echo"</li><br />";
        echo'<li>Created:  '; echo $r->created; echo'</li><br />';
    }
?>

这是发生数据库查询的模型和函数:

<?php
class Tweets_model extends CI_Model{

    public function __construct() {

        $this->load->database();

    }

    public function getTweetDetails($id){
    $this->db->select('*')->from('tweets')->where('tweetId', $id);
    $q = $this->db->get();
        if($q->num_rows > 0){

            foreach($q->result() as $row){

                $data[]=$row;

            }
            return $data;

        }

    }
}
?>

控制器:

<?php

class Tweets extends CI_Controller{

    public function __construct() {
        parent::__construct();
        $this->load->model('tweets_model');
    }

    public function details(){

        $data['rows'] = $this->tweets_model->getTweetDetails($this->uri->segment(2));
        $this->load->view('header');
        $this->load->view('tweet_details', $data);
        $this->load->view('footer');
    }
}

?

>

谁能帮我解决我的错误信息?

谢谢。

4

2 回答 2

1

尝试这个:

看法:

<?php
    echo "<h2>View</h2>";
    foreach ($rows as $r){
        echo '<br /><h3>'; echo $r['tHandle']; echo'</h3>'; // EDIT THIS LINE
        echo "<li>Sent at:  "; echo $r['content']; echo"</li><br />";
        echo'<li>Created:  '; echo $r['created']; echo'</li><br />';
    }
?>

模型:

<?php
class Tweets_model extends CI_Model{

    public function __construct() {

        $this->load->database();

    }

    public function getTweetDetails($id){
    $this->db->select('*')
    $this->db->from('tweets')
    $this->db->where('tweetId', $id);
    return $this->db->get()->result_array();

    }
}
?>

控制器:

<?php

class Tweets extends CI_Controller{

    public function __construct() {
        parent::__construct();
        $this->load->model('tweets_model');
    }

    public function details(){

        $data['rows'] = $this->tweets_model->getTweetDetails($this->uri->segment(2));
        $this->load->view('header');
        $this->load->view('tweet_details', $data);
        $this->load->view('footer');
    }
}

?>

更新:

段从左到右编号。例如,如果您的完整 URL 是这样的:

http://example.com/index.php/news/local/metro/crime_is_up

段号是这样的:

$this->uri->segment(1) // with return -> news
$this->uri->segment(2) // with return -> local
$this->uri->segment(3) // with return -> metro
$this->uri->segment(4) // with return -> crime_is_up

在您的情况下$this->uri->segment(2)将返回details。并且查询将返回 0 行。只是为了测试,你可以这样做:

public function details(){
            $tweet_id = 1 // for example
            $data['rows'] = $this->tweets_model->getTweetDetails($tweet_id);
            $this->load->view('header');
            $this->load->view('tweet_details', $data);
            $this->load->view('footer');
        }

或者您可以从 url 获取它,例如:

public function details($id){ // Here you will have the $tweet_id

                $data['rows'] = $this->tweets_model->getTweetDetails($this->uri->segment(3));
                $this->load->view('header');
                $this->load->view('tweet_details', $data);
                $this->load->view('footer');
            }
于 2013-08-01T16:45:45.970 回答
0

您需要在此处进行一些故障排除。您所有的错误都是因为数据库没有返回任何数据。

  1. 确保 tweetId 实际上在段 2 中,段在 base_url 之后开始计数,因此如果您的基本 url 是 example.com,那么段 2 将类似于 example.com/tweet/2,其中 2 是正确的段。

  2. 将您知道存在的 tweetId 传递给此行中的模型:

    $data['rows'] = $this->tweets_model->getTweetDetails($this->uri->segment(2));

所以像:

$data['rows'] = $this->tweets_model->getTweetDetails(1);
于 2013-08-01T17:03:29.773 回答