简体   繁体   中英

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

When i clicking on link i want to show respected id data on next page using Codeigniter but i getting error of Undefined variable: id

This is my index view file

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

This is mymodel

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

this is my controller

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

This is my jobdetails view file

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

I am getting following error

undefined variable:id

Invalid argument supplied for foreach()

there is no $this->input->get('id'); used inside the controller to get the value of id , and also the variable $id is not defined inside the controller also , so your controller need to be like below:

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

Update

And your model have an invalid return result variable.

return $data->result(); 

replace : return $data->result(); with return $query->result();

Try this Way

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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