![](/img/trans.png)
[英]how to display information related to clicked element in next page using 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>
我收到以下錯誤
沒有$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.