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


How to display data on next page but getting error in PHP using Codeigniter?

当我点击链接时,我想使用Codeigniter在下一页上显示受尊重的id数据,但我得到了Undefined variable: id 的错误

这是我的索引视图文件

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

这是我的型号

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

这是我的作业详细信息查看文件

<?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>

我得到以下错误

未定义变量:id

为foreach()提供的参数无效

控制器内部没有使用$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 $query->result(); 替换:return $data->result();

试试这种方法

// 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 } ?>