duanliang789262 2017-04-03 08:46
浏览 55

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

In my routes.php file I have added the following code for routing:

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

Here is my controller code:

<?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');

        }
    }

Here is my model code:

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

}
?>

this is the code of view. I just print the array in my report_view for testing purpose:

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

this is my home_view code:

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

I have tried multiple times to view the array. But failed to get array. How can I load the view? When I click my items from home, it will create a url like http://localhost/super_shop_register/report/1

But didn't show any data. Show "Object not found!". Here is the image of my view: Home View After Clicking item, it will show like this: after clicking item

How can I solve this?

  • 写回答

5条回答 默认 最新

  • doucheyi1347 2017-04-03 09:14
    关注

    change your root from $route['report/:num'] = "home/reportcard/$1";
    to

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

    and

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

    this line of code will create href="localhost/super_shop_register/report/Chips/1". You need to remove <?=$row->item ?>/ from it.

    评论

报告相同问题?

悬赏问题

  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 python进程启动打包问题
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题