简体   繁体   中英

CodeIgniter : How to get data by ID and preview it (Where 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 :))

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM