douji9816 2015-05-01 10:19
浏览 42
已采纳

CodeIgniter:如何通过ID获取数据并进行预览(其中ID)

I have 2 table :

  1. product_group
  2. product

which is every product have group_id as Foreign Key in the table.

I try to call every product by group_id.

this is my list_group view :

 <a href="<?= base_url('list_group/get_product_by_group/'. $value->group_id);?>" class="right-icon"><i class="icon-line-ellipsis"></i></a>

I try to attach the group_id in URL.

this is my controller :

    public function get_product_by_group($group_id)
    {

            if($this->uri->segment(3))
            {
                    $this->load->database();
                    $group_id = $this->input->get('group_id', TRUE);
                    $data['product_data'] = $this->group_model->list_product_by_group($group_id);
                    $this->load->view('fend/list_product', $data);

            }
            else
            {
                    redirect('list_group');
            }

    }

This is my model :

        function list_product_by_group($group_id)
        {
                $this->db->select('*');
                $this->db->where('group_id', $group_id);
                $query = $this->db->get('product');
                return $query->result();
        }

and the last is list_product view :

 <?php
      foreach($product_data as $value)
      {
 ?>

   <div class="masonry-thumbs col-3" data-big="2" data-lightbox="gallery">
      <a href="<?= base_url('uploads/'. $value->product_picture);?>" data-lightbox="gallery-item"><img class="image_fade" src="<?= base_url('uploads/'. $value->product_picture);?>" alt="Gallery Thumb 1"></a>
   </div>

 <?php
     }
 ?>

I got no error, but the data did not appear in product_view.

Thanks for your help :))

  • 写回答

1条回答 默认 最新

  • duanlu1908 2015-05-01 11:00
    关注

    If you remove the line $group_id = $this->input->get('group_id', TRUE); it will solve your problem.

    Here is the explanation:

    Your controller function name is public function get_product_by_group($group_id) so when you go to this link localhost/norwin/list_group/get_product_by_group/1 1 will be passed as $group_id into your get_product_by_group function.So your final function will be like this

    public function get_product_by_group($group_id='')
    //write this way so that you can call the url like
    //localhost/norwin/list_group/get_product_by_group
    {
    
            if($group_id)//no need to check uri->segment
            {
                    $this->load->database();                    
                    $data['product_data'] = $this->group_model->list_product_by_group($group_id);
                    $this->load->view('fend/list_product', $data);
    
            }
            else
            {
                    redirect('list_group');
            }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线