我使用codeigniter建立在线商店。我有产品目录。当用户单击每个目录时,它必须在另一个视图中显示详细信息。但是,我不知道我想做什么。
任何人都可以给我示例代码,我的意思是控制器和模型中的代码。
这是我的看法
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);
}
我的模特
public function test($id) {
$query = $this->db->query('SELECT * FROM PRODUK WHERE id_produk = ' . $id);
return $query->result_array();
}
这是我的观点
<?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>
答案 0 :(得分:1)
如果您需要选择哪个产品用户,请使用带有产品ID的<a>
标记。 <a href="<?php echo base_url()?>index.php/controller/function(preview)/<?php echo $data['id']?>">
。
所以你的功能知道要选择哪个产品。
<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>
然后使用巡视控制器
来捕获它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
}
模型中的
这将选择您传递给模型($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
}
在视图中
foreach ($details as $details_new)
{
# code...
//you can use your own style dives and all as usual
}
答案 1 :(得分:0)
,您需要将该产品ID传递给控制器,然后从模型中获取详细信息并在视图中显示它们