Postingan lainnya
[Codeigniter] Data Cart tidak masuk
Mohon pencerahanya saya bingung ini knp data cart gak masuk e cart->contents(); sayanya pusing udah 5 hari ngakalin :v
Controller
<?php
class Cart extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->library("cart");
$this->load->model("m_cart");
}
function index(){
$data["produk"] = $this->m_cart->get_all();
$this->load->view("template-h/cart/index", $data);
}
function add(){
$data = array(
'code_products' => $this->input->post('cd'),
'title_products' => $this->input->post('titel'),
'qty' => $this->input->post('qty'),
'ukuran' => $this->input->post('ukuran'),
'gambar' => $this->input->post('img'),
'price_products' => $this->input->post('prc'),
);
// print_r($data);
$this->cart->insert($data);
redirect('/cart', 'refresh');
}
View cart
<?php
//for($i=1;$i<=2;$i++){
error_reporting(0);
if ($cart = $this->cart->contents()):
$grand_total = 0;
foreach ($cart as $item):
?>
<div class="cart-header">
<div class="close1">
<a href="<?php echo base_url(); ?>cart/remove/<?php echo $item['rowid']; ?>">
<img src="<?php echo base_url(); ?>assets/home/images/close.png"/>
</a>
</div>
<div class="cart-sec">
<div class="cart-item cyc">
<img src="<?php echo base_url(); ?>assets/home/<?php echo $item['picture']; ?>"/>
</div>
<div class="cart-item-info">
<h3><?php echo $item['name']; ?><span></span></h3>
<h4><span>Rp.</span> <?php echo number_format($item['price'], 0, ',', '.'); ?>,00</h4>
<p class="qty">Qty ::</p>
<input min="1" type="number" id="quantity" name="quantity" value="1"
class="form-control input-small">
<?php
$grand_total = $grand_total + $item['price'];
//echo "Total :".$grand_total;
?>
</div>
<div class="clearfix"></div>
<div class="delivery">
<p></p>
<span></span>
<div class="clearfix"></div>
</div>
</div>
</div>
<?php
endforeach;
endif;
?>
Form Produk
<?= form_open('cart/add') ?>
<div class="row">
<div class="col-md-6">
<div class="form-group" style="margin-top:10px; text-align: center!important;">
<label class="form-control-label">Ukuran</label>
<?php
$options = array('xl'=> 'XL','m' => 'M','s' => 'S');
$att = array('class' => 'form-control');
echo form_dropdown('ukuran', $options, 'XL', $att);
?>
</div>
</div>
<div class="col-md-6">
<div class="form-group" style="margin-top:10px; text-align: center!important;">
<label class="form-control-label">Jumlah</label>
<input id="qty" type="number" name="qty" class="form-control" value="1">
</div>
</div>
</div>
<div class="form-group">
<?php
$cd = $produk->code_products;
$name = $produk->title_products;
$picture = $produk->gambar;
$price = $produk->price_products;
echo form_hidden('cd', $cd);
echo form_hidden('titel', $name);
echo form_hidden('img', $picture);
echo form_hidden('prc', $price);
?>
</div>
<button type="submit" name="add" class="btn btn-light" style="margin-top: -15px; margin-bottom: -20px;color: #5b6366"> Add To Cart</button>
<?= form_close(); ?>
mohon pencerahanya ya suhu beneran saya pusing 5 hari berturut2 ngakalin ini doang >_<
1 Jawaban:
Jawaban Terpilih
CASE CLOSE !!!
How ?
Controller saya pada add(){} <pre> $data = array(
'code_products' =&gt; $this-&gt;input-&gt;post('cd'),
'title_products' =&gt; $this-&gt;input-&gt;post('titel'),
'qty' =&gt; $this-&gt;input-&gt;post('qty'),
'ukuran' =&gt; $this-&gt;input-&gt;post('ukuran'),
'gambar' =&gt; $this-&gt;input-&gt;post('img'),
'price_products' =&gt; $this-&gt;input-&gt;post('prc'),
); </pre>
Ternyata arraynya itu harus ada 4 yang wajib, ada 4 field yang wajib diisi
<pre>
$data = array( 'id' => 'sku_123ABC', 'qty' => 1, 'price' => 39.95, 'name' => 'T-Shirt', ); </pre>
4 field diatas adalah field wajib diisi untuk array Library cart, kalau library cart tidak dapat diinput coba chek dulu apakah sudah ada 4 field tersebut atau belum. karena akan gagal bila tidak terdapat 4 field tersebut
Tanggapan
Kalau insert lebih dari 10 item bisa ga ?
bisa gan, lebih juga bisa asalkan 4 field itu wajib diisi, jika tidak maka gak akan berfungsi di function cart nya