show detail product in online shop (codeigniter)

3.9k views Asked by At

I build online shop using codeigniter. I have product catalogs. When the user clicks each catalog, it must show the detailed information in another view. But, I have no idea on what I suppose to do.

Can anybody give me example code, I mean code in controller and model.

This is my view enter image description here

public function detail($page = 'detail') {
  $data['title'] = ucfirst($page);

  $this->cart_model->test($id);
  $data['produk'] = $this->cart_model->test($id);
  print_r($data['produk']);die();

  $this->load->view('user/templates/header', $data);
  $this->load->view('user/templates/navigation', $data);
  $this->load->view('user/pages/' . $page, $data);
  $this->load->view('user/templates/footer', $data);
}

my model

public function test($id) {
  $query = $this->db->query('SELECT * FROM PRODUK WHERE id_produk = ' . $id);
  return $query->result_array();
}

this is my view

<?php foreach ($produk as $data): ?>

<div class="col-lg-4 col-md-4 col-sm-4">
  <div class="images">
    <a href="" class="img-responsive"><img src="/sjpro/asset/user/img/produk<?php echo $data['gambar'];?>"></a>
  </div>
</div>

<div class="col-lg-8 col-md-4 col-sm-4">

  <h3><?php echo $data['nama_produk']?></h3>
  <div class="price-detail">Harga <span class="price-detail-color"><?php echo $data['harga_produk']?></span></div>

  <span class="label label-success"><?php echo $data['status_produk']?></span>
  <div class="well well-lg">
    <form action="" class="form-horizontal">

      <div class="form-group">
        <label class="col-lg-2 control-label">Jumlah</label>
        <div class="col-lg-3">
          <input type="number" step="1" min="1" name="quantity" value="1" class="form-control" size="2">
        </div>
        <button type="button" class="btn btn-success btn-lg pull-right" data-toggle="modal" data-target="#modal-beli">BELI</button>
      </div>

    </form>
  </div>

enter image description here

2

There are 2 answers

0
Abdulla Nilam On

if you need to get which product user select just use <a> tag with the product id. <a href="<?php echo base_url()?>index.php/controller/function(preview)/<?php echo $data['id']?>">.

so your function know which product to be selected.

<a href="<?php echo base_url()?>index.php/controller/function(preview)/<?php echo $data['id']?>">
        <div class="col-lg-4 col-md-4 col-sm-4">
            <div class="images">
                <a href="" class="img-responsive"><img src="/sjpro/asset/user/img/produk<?php echo $data['gambar'];?>"></a>
            </div>
        </div>

        <div class="col-lg-8 col-md-4 col-sm-4">

            <h3><?php echo $data['nama_produk']?></h3>
            <div class="price-detail">Harga <span class="price-detail-color"><?php echo $data['harga_produk']?></span></div>
            <span class="label label-success"><?php echo $data['status_produk']?></span>
            <div class="well well-lg">
                <form action="" class="form-horizontal">

                    <div class="form-group">
                        <label class="col-lg-2 control-label">Jumlah</label>
                        <div class="col-lg-3">
                            <input type="number" step="1" min="1" name="quantity" value="1" class="form-control" size="2">
                        </div>
                        <button type="button" class="btn btn-success btn-lg pull-right" data-toggle="modal" data-target="#modal-beli">BELI</button>
                    </div>

                </form>
            </div>
        </div>
    </a>

then catch this with tour controller

public function preview($id)//this know only one parameter will be come to this
{
    $data['details']=$this-->Your_Model_Name->get_details($id);//passing the product id to get details of your product

}

in Model

this will select all details form id which you passed to model ($id)

public function get_details($id)
    {
        $query = $this->db->query("SELECT * FROM product WHERE id='$id'");
        $result = $query->result_array();
        return $result; //return as object array
    }

in view

foreach ($details as $details_new) 
{
    # code...
 //you can use your own style dives and all as usual 
}
0
mokNathal On

on click you need to pass that product id to controller then fetch details from model and display them on view