2017-04-03 61 views
-1

在我的routes.php文件的文件我已經添加了路由下面的代碼:如何在數據庫中設置codeigniter中的動態路由?

$route['report/:num'] = "home/reportcard/$1"; 

這裏是我的控制器代碼:

<?php 
    defined('BASEPATH') OR exit('No direct script access allowed'); 

    class Home extends CI_Controller 
    { 

     function __construct() { 
      parent::__construct(); 

     if(empty($this->session->userdata('id_user'))){ 
      $this->session->set_flashdata('flash_data', 'You cannot access'); 
        redirect('login'); 
     } 
     } 
     public function index(){ 
      $this->load->model("item_model"); 
      $data['records'] = $this->item_model->getAllItems(); 
      $this->load->view('home',$data); 
     } 

     function reportcard($id){ 
      $this->load->model("item_model"); 
      $data['report'] = $this->item_model->getReport($id); 
      $this->load->view('report', $data); 
     } 

     function logout(){ 
      $data=['id_user','username']; 
      $this->session->unset_userdata($data); 
      redirect('login'); 

     } 
    } 

這裏是我的模型代碼:

<?php 
    class Item_model extends CI_Model 
    { 
     function getAllItems() 
     { 
      $this->load->database(); 
      $q = $this->db->get("item"); 
      if($q->num_rows() > 0) 
      { 
       return $q->result(); 
      } 
      return array(); 
     } 

     public function getReport($id){ 
      $this->db->select('*'); 
      $this->db->from('item'); 
      $this->db->where('item.id', $id); 
      $query = $this->db->get();  
      if($query->num_rows() > 0) 
       return $data->result(); 
     } 

} 
?> 

這是觀點的代碼。我只是打印數組中的我report_view用於測試目的:

<?php 
defined('BASEPATH') OR exit('No direct script access allowed'); 
print_r($report); 
?> 

這是我home_view代碼:

<div class="row"> 
       <ul class="home-grid"> 
        <?php foreach ($query->result() as $row): ?> 
        <li> 
         <a href="<?php echo base_url() ?>report/<?=$row->item ?>/<?=$row->id ?>" class="btn btn-lg btn-warning view-report"><span class="glyphicon glyphicon-list-alt"></span> <br/> 
         <?=$row->item ?><br/> 
         <small>Click here for see report</small> 
         </a> 
        </li> 
        <?php endforeach; ?> 
       </ul> 

我已經試過多次來查看陣列。但未能獲得數組。我如何加載視圖?當我從家中點擊我的物品時,它會創建一個網址,如http://localhost/super_shop_register/report/1

但是沒有顯示任何數據。顯示「找不到對象!」。這裏是我的看法形象:Home View 點擊後,它會顯示如下:after clicking item

我該如何解決這個問題?

+1

super_shop_register是你的base_url的一部分嗎?在你發佈的視圖代碼中,它看起來應該是report /,$ row-> item和$ row-> id之後的兩個參數,爲什麼只有id出現? – Pacio

+0

我認爲它與另一條路線相匹配,你可以發佈你的完整路線 –

+0

base_url是好的。當我拍攝快照時,代碼只有$ row-id。這就是爲什麼在url中只顯示id。這根本不是什麼大問題。 $ config ['base_url'] ='http:// localhost/super_shop_register'; –

回答

1

$route['report/:num'] = "home/reportcard/$1";
根改爲

$route['report/(:num)'] = "home/reportcard/$1"; 

<a href="<?php echo base_url() ?>report/<?=$row->item ?>/<?=$row->id ?>" class="btn btn-lg btn-warning view-report"> 

這行代碼將創建href="localhost/super_shop_register/report/Chips/1"。 您需要從中刪除<?=$row->item ?>/

+0

網址就是這樣創建的。但仍顯示「找不到對象! 在此服務器上找不到請求的URL。引用頁面上的鏈接似乎是錯誤或過時的,請通知該頁面的作者有關錯誤。」 –

+0

您更改了根目錄像我建議的? –

+0

我改變了你建議的路線。 –

1

在你的模型中這是錯的return $data->result();其實$query->result();是正確的。

<?php 
    class Item_model extends CI_Model 
    { 
     function getAllItems() 
     { 
      $this->load->database(); 
      $q = $this->db->get("item"); 
      if($q->num_rows() > 0) 
      { 
       return $q->result(); 
      } 
      return array(); 
     } 

     public function getReport($id){ 
      $this->db->select('*'); 
      $this->db->from('item'); 
      $this->db->where('item.id', $id); 
      $query = $this->db->get();  
      if($query->num_rows() > 0) 
       return $data->result(); 
     } 

} 
?> 
+0

Opps。錯誤。編輯返回$ data-> result();返回$ query-> result(); 仍然無法正常工作。 –

1

加油的人,你的代碼可以完美運行在我的鍛鍊例如

我的路線

$route['report/(:num)'] = "welcome/workout/$1"; 

我歡迎控制器

public function workout($id) 
    { 

     echo $id; 

    } 

我的網址是

http://localhost/Code/report/1 

我的輸出是

1 

enter image description here

確保您做以下

  1. 刪除索引。PHP的配置像這樣($ config ['index_page'] ='';)。在根文件夾
  2. 檢查用的.htaccess index.php文件

    ,並使其低於

    RewriteEngine敘述在

    的RewriteCond%{} REQUEST_FILENAME!-f

    的RewriteCond%{} REQUEST_FILENAME !-d

    RewriteRule ^(。*)$ index.php/$ 1 [L]

參考:CodeIgniter object not found only the index function works

當我打消了我的.htaccess代碼,您的錯誤發生

enter image description here

希望這有助於:)

+1

@Tanzila你可以檢查網址,http://localhost/super_shop_register/index.php/report/1 評論你的迴應。 –

0

你的路線應該是這樣的:

$route['report/(:num)'] = "home/reportcard/$1"; 

and make s URE你的htaccess文件是存在的,有代碼:

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule .* index.php?/$0 [PT,L] 
0

簡單,你可以試試這個

$route['report/(:num)'] = "home/reportcard"; 

那麼您可以在功能的Reportcard參數與下面的方法獲取ID在控制器和在人間傳遞

$report_id = $this->uri->segment(2); 
+0

問題在於路由。直接函數參數可以實現來自url的數據。 –