如何在数据库中设置codeigniter中的动态路由?

时间:2017-04-03 08:46:47

标签: php html mysql codeigniter

在我的routes.php文件中,我添加了以下代码用于路由:

$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

的网址

但没有显示任何数据。显示&#34;找不到对象!&#34;。以下是我的观点图片:Home View 点击项目后,它将显示如下:after clicking item

我该如何解决这个问题?

5 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分: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();
        }

}
?>

答案 2 :(得分:1)

来吧,你的代码在我的锻炼示例中非常完美

我的路线

public function workout($id)
    {

        echo $id;

    }

我的欢迎控制器

http://localhost/Code/report/1

我的网址是

1  

我的输出是

long heapFreeSize = Runtime.getRuntime().freeMemory();
URL url = new URL("http://example.com");
URLConnection myUrlConnection = url.openConnection();
GZIPInputStream gZIPInputStream = new GZIPInputStream(myUrlConnection.getInputStream());
StringBuffer decompressedStringBuffer = new StringBuffer();
int bytes_read;
int bufferSize;
heapFreeSize = Runtime.getRuntime().freeMemory();
while ((bytes_read = gZIPInputStream.read(buffer)) > 0) {

    String part = new String(buffer, 0 ,bytes_read, "UTF-8");
    heapFreeSize = Runtime.getRuntime().freeMemory();
    decompressedStringBuffer.append(part);
    bufferSize = decompressedStringBuffer.length();
    heapFreeSize = Runtime.getRuntime().freeMemory();
}

enter image description here

确保您完成了以下操作

  1. 删除了配置上的index.php($ config ['index_page'] ='';)。
  2. 使用index.php文件检查根文件夹中的.htaccess

    并在下面

    RewriteEngine On

    RewriteCond%{REQUEST_FILENAME}!-f

    RewriteCond%{REQUEST_FILENAME}!-d

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

  3. 参考:CodeIgniter object not found only the index function works

    当我删除了我的.htaccess代码时,发生了错误

    enter image description here

    希望这有助于:)

答案 3 :(得分:0)

你的路线应该是这样的:

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

并确保你的htaccess文件在那里并且有代码:

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

答案 4 :(得分:0)

很简单,你可以试试这个

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

然后你可以在控制器中使用以下方法获取ID,并且没有传递函数reportcard参数

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