繁体   English   中英

如何使用Codeigniter在下一页显示数据,但在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);          
    }

这是我的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>

我收到以下错误

未定义变量: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 $data->result(); return $query->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 } ?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM