如何在下一页显示数据,但使用Codeigniter在PHP中出错?

时间:2016-07-18 07:50:11

标签: php mysql codeigniter

当我点击链接时,我希望使用Codeigniter在下一页显示受尊重的ID数据,但我收到错误Undefined variable: id

这是我的索引视图文件

<a itemprop="title" style="text-transform: capitalize;color:#29aafe" href="<?=site_url('jobdetails?#'.$row->JPostID);?>"><?=$row->JTitle;?></a>

这是mymodel

public function getRowByJob($id){
        $query = $this->db->get_where('jobs', array('JPostID' => $id));
        if($query->num_rows() > 0)
            return $data->result();                    
    }

这是我的控制器

public function jobdetails(){
        $data = array();
        $data['jobdata'] = $this->mymodel->getRowByJob($id); // it is model method to fetch the record of that users having id = $id
        $this->load->view('jobdetails',$data);          
    }

这是我的jobdetails视图文件

<?php foreach($jobdata as $row){?>                                        
                 <div class="block-section box-item-details" itemscope itemtype="http://schema.org/JobPosting">
                    <h2 class="title" itemprop="title" style="text-transform: capitalize;"><?=$row->JTitle;?></h2></div>

我收到以下错误

undefined variable:id

Invalid argument supplied for foreach()

2 个答案:

答案 0 :(得分:0)

控制器内部没有使用$this->input->get('id');来获取id的值,并且控制器内部也没有定义变量$id,所以你的控制器需要像下面:

public function jobdetails(){
        $data = array();
        $id = $this->input->get('id'); // getting $_GET['id'] id from url. and passing it to $id
        $data['jobdata'] = $this->mymodel->getRowByJob($id); // it is model method to fetch the record of that users having id = $id
        $this->load->view('jobdetails',$data);          
    }

<强>更新

您的模型的返回结果变量无效。

return $data->result(); 

return $data->result();替换为return $query->result();

答案 1 :(得分:0)

尝试这种方式

// Model.   
public function getRowByJob($id){
    $this->db->select('*');
    $this->db->from('jobs');
    $this->db->where('JPostID', $id);
    $query = $this->db->get();

    if($query->num_rows() > 0)
        return $query;                    
}

// Controller.
// Address should be - localhost/Sample/index.php/Controller/jobdetails/5643
// 5643 is $id
public function jobdetails($id){
    $data['jobdata'] = $this->mymodel->getRowByJob($id); // it is model method to fetch the record of that users having id = $id
    $this->load->view('jobdetails', $data);          
}

// View
<?php foreach($jobdata->result() as $row) { ?>                                        
        <div class="block-section box-item-details" itemscope itemtype="http://schema.org/JobPosting">
            <h2 class="title" itemprop="title" style="text-transform: capitalize;"><?php echo $row->JTitle; ?></h2>
        </div>
<?php } ?>