I have a problem in codeigniter update cart
this my view cart.
<script>
function update_cart(){
document.forms["updateCart"].submit();
}
</script>
<h1>DAFTAR PRODUCT</h1>
<table style="width:100%" border="1">
<tr style="background-color: yellow;">
<td>Name</td>
<td>Price</td>
<td>Aksi</td>
</tr>
<?php foreach ($products as $data):?>
<tr>
<td><?php echo $data->name;?></td>
<td><?php $hargapro = number_format("$data->price",0,",",".");echo "Rp.".$hargapro; ?></td>
<td><button type="button">Add to Cart</button></td>
</tr>
<?php endforeach ?>
</table>
<h1>DAFTAR KERANJANG SAMPAH</h1>
<table style="width:100%" border="1">
<tr style="background-color: yellow;">
<td>ID</td>
<td>name</td>
<td>qty</td>
<td>price</td>
<td>total</td>
<td>Aksi</td>
</tr>
<?php foreach ($items as $items):?>
<tr>
<td><?php echo $items['id'];?></td>
<td><?php echo $items['name'];?></td>
<td>
<form name="updateCart" method="post" action="<?php echo base_url();?>cart/update" enctype="multipart/form-data">
<input type="text" name="qty" value="<?php echo $items['qty'];?>" maxlength="3" size="2" />
<input type="hidden" name="rowid" value="<?php echo $items['rowid'];?>" />
</form>
</td>
<td><?php $hargapro = number_format("$items[price]",0,",",".");echo "Rp.".$hargapro; ?></td>
<td><?php
$totalitem = $items['qty'];
$price = $items['price'];
$xprice = $totalitem*$price;
$xtotal = number_format("$xprice",0,",",".");
echo "Rp.".$xtotal;
?></td>
<td>
<form method="post" action="<?php echo base_url();?>cart/delete">
<input type="hidden" name="id" value="<?php echo $items['rowid'];?>"/>
<button type="submit" value="<?php echo $items['rowid'];?>">Delete</button>
</form>
</td>
</tr>
<?php endforeach ?>
</table>
<h3><b>Total Item : <?php echo $totalpro; ?></b></h3>
<h3><b>Total Harga : Rp. <?php $harga=number_format("$total",0,",",".");echo $harga; ?></b></h3>
<br>
<button type="button" onClick="update_cart();">Update Chart</button>
<button type="button">Clear All</button>
and this is my controller update cart
// Update Product from Cart
function update(){
$cart = $this->cart->contents();
$data = array(
(foreach ($cart as $c)){
array(
'rowid' => $c['rowid'],
'qty' => $this->input->post('qty'),
),
};
);
echo "<pre>";
print_r($data);
//$this->cart->update($data);
//redirect('cart/show');
}
what I want to ask is:
1. How to update all of the items in the cart CodeIgniter?
2. Or how to insert function "foreach" in "array" as in update function?
I will suggest to send the value of rowid and quantity from your form to the update function .
<form action="" method="POST">
<input type="text" name="quantity" value="" />
<input type = "hidden" name="rowid" value=""/>
<!-- OTHER FORM FIELDS HERE -->
<input type="submit" name="submit" value="UPDATE" />
</form>
And in your controller.
public function update()
{
$data=$this->cart->update(array(
'rowid'=>$this->input->post('rowid'),
'qty'=> $this->input->post('quantity');
));
$this->cart->update($data);
//redirect here
}
Update
To update all row at once you can use multidimensional array . It is well defined in userguide
$data = array(
array(
'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
'qty' => 3
),
array(
'rowid' => 'xw82g9q3r495893iajdh473990rikw23',
'qty' => 4
),
array(
'rowid' => 'fh4kdkkkaoe30njgoe92rkdkkobec333',
'qty' => 2
)
);
$this->cart->update($data);
Related
I'm building a test shopping site using PHP / mySQL
here is my code:
the show_product.php:
<form method ="get" action="addtocart.php">
<input type="hidden" name="id_product" value='.$row['id_product'].'>
<tr>
<th colspan="2">'.$row['name'].'</th>
</tr>
<td>
<img align="center" src='.$row['photo'].' alt="">
</td>
<td>'.mb_substr($row['description'],0,200).'....</td>
<tr></tr>
<td>
'.$row['price'].'€<input type="number" name="quantity" value="1" min="1" max="20">
</td>
<td> <button type="submit" class="btn">add to cart</button></form>
the addtocart.php
session_start()
if(isset($_GET) & !empty($_GET)){
$id_product = $_GET['id_product'];
if(isset($_GET['quantity']) & !empty($_GET['quantity'])){ $quant = $_GET['quantity']; }else{ $quant = 1;}
$_SESSION['cart'][$id_product] = array("quantity" => $quant);
header('location: cart.php');
}else{
header('location: cart.php');
}
echo "<pre>";
print_r($_SESSION['cart']);
echo "</pre>";
?>
and some code of the cart.php
foreach ($cart as $key => $value) {
$cartsql = "SELECT * FROM product WHERE id_product=$key";
$cartres = mysqli_query($con, $cartsql);
$row = mysqli_fetch_assoc($cartres);
?>
<tr><form>
<td colspan="2"><?php echo $row['name'] ?></td>
<td><?php echo $row['price'].' €'; ?></td>
**<td><input type="number" class="quantity" name="quantity" value="<?php echo $value['quantity']; ?>"></td>**
<td><?php echo ($row['price']*$value['quantity']).' €'; ?> </td>
<td><img width="30" height="30" src="icons/delete.png" alt=""></td> </tr>
If i send to addcart an item of some quantity it's working.
the problem is I cannot change the quantity inside on cart.php, I know very few about Javascript.
Please any help how to do this ??
i did it with a submit form inside the cart :)
<form class="userform" method='get' name="change" action="addtocart.php">
<input type='hidden' name='id_product' value="<?php echo $row["id_product"]; ?>" />
<td><input size="5" type="number" name="quantity" value="<?php echo $value['quantity'] ?>" min="1" max="20" onchange="this.form.submit()"></td></form>
I have a problem when I add some products to cart, but then when I want to fetch contents of the cart I find it is empty.
this is my controller :
public function add_to_cart(){
$insert_data = array(
'id' => $this->input->post('id'),
'name' => $this->input->post('name'),
'price' => $this->input->post('price'),
'qty' => $this->input->post('qty') );
// This function add items into cart.
$this->cart->insert($insert_data);
}
and this is my form to add new product to cart :
<div class="button-group">
<form method="POST" action="<?php echo base_url().'add_to_cart'; ?>">
<div style="display:none">
<input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash();?>" />
</div>
<input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash(); ?>">
<input type="number" name="qty" id="<?php echo $ligneBase->id;?>">
<input type="hidden" name="name" value="<?php echo $ligneBase->name;?>">
<input type="hidden" name="price" value="<?php echo $ligneBase->price;?>">
<input type="hidden" name="id" value="<?php echo $ligneBase->id;?>">
<input class="add_cart" type="submit" value="Add to cart">
<div class="add-to-links">
</form>
</div>
and this is my cart in header.php :
<table class="table">
<tbody>
<div id="text">
<?php $cart_check = $this->cart->contents();
// If cart is empty, this will show below message.
if(empty($cart_check)) {
echo '<h4 align="center">No Product in cart, To add products to your shopping cart click on "Add to Cart" Button</h4>';
} ?>
</div>
<?php
$cart = $this->cart->contents();
foreach($cart as $indice => $ligneBase){
?>
<tr>
<td class="text-center"><img class="img-thumbnail" title="Xitefun Causal Wear Fancy Shoes" alt="Xitefun Causal Wear Fancy Shoes" src="image/product/sony_vaio_1-50x50.jpg"></td>
<td class="text-left"><?php echo $ligneBase->id;?></td>
<td class="text-right">x 1</td>
<td class="text-right"><?php echo $ligneBase->name;?> </td>
<td class="text-center"><button class="btn btn-danger btn-xs remove" title="Remove" onClick="" type="button"><i class="fa fa-times"></i></button></td>
</tr>
<?php
}
?>
</tbody>
</table>
Can I suggest a bit improvements in your code structure. You need to use a model for db interactions
Step-1. Controller Function
public function cart()
{
$data['cart_items']=$this->cart_model->getCartItems();
if($_POST)
{
// Perform Validation
if($this->form_validation->run()==false)
{
$data['errors']=validation_errors();
$this->load->view('cart_view',$data);
}
else
{
$row=$this->cart_model->insertToCart($this->security->xss_clean($_POST));
if($row)
{
$data['success']='Item Added';
$this->load->view('cart_view',$data);
}
else
{
$data['error']='Item Could not be Added';
$this->load->view('cart_view',$data);
}
}
}
else
{
$this->load->view('cart_view',$data);
}
}
Step-2. Model Functions
public function getCartItems()
{
$sql='create query';
return $this->db->query($sql)->result_array();
}
public function insertToCart($data)
{
$item=array(
'field' => $data['index']
);
$this->db->insert('cart',$item);
return $this->db->insert_id();
}
Step-3. View
<div class="button-group">
<form method="POST" action="">
<input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash();?>" />
<input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash(); ?>">
<input type="number" name="qty" id="<?php echo $ligneBase->id;?>">
<input type="hidden" name="name" value="<?php echo $ligneBase->name;?>">
<input type="hidden" name="price" value="<?php echo $ligneBase->price;?>">
<input type="hidden" name="id" value="<?php echo $ligneBase->id;?>">
<input class="add_cart" type="submit" value="Add to cart">
<div class="add-to-links">
</form>
</div>
my workout in your code
my controller
public function workout()
{
if($this->input->post())
{
//echo "<pre>"; print_r($this->input->post());
$insert_data = array(
'id' => $this->input->post('id'),
'name' => $this->input->post('name'),
'price' => $this->input->post('price'),
'qty' => $this->input->post('qty') );
$this->cart->insert($insert_data);
echo "<pre>"; print_r($this->cart->contents());
exit();
}
$this->load->view('workout');
}
my view
<form method="POST" action="<?php echo base_url().'welcome/workout'; ?>">
<input type="number" name="qty" id="1002">
<input type="hidden" name="name" value="Mobile">
<input type="hidden" name="price" value="300">
<input type="hidden" name="id" value="1002">
<input class="add_cart" type="submit" value="Add to cart">
<div class="add-to-links">
</form>
and my output is
Array
(
[fba9d88164f3e2d9109ee770223212a0] => Array
(
[id] => 1002
[name] => Mobile
[price] => 300
[qty] => 24
[rowid] => fba9d88164f3e2d9109ee770223212a0
[subtotal] => 7200
)
)
works fine
and your error is in displaying foreach
<table class="table">
<tbody>
<div id="text">
<?php $cart_check = $this->cart->contents();
// If cart is empty, this will show below message.
if(empty($cart_check)) {
echo '<h4 align="center">No Product in cart, To add products to your shopping cart click on "Add to Cart" Button</h4>';
} ?>
</div>
<?php
$cart = $this->cart->contents();
foreach($cart as $indice => $ligneBase){
?>
<tr>
<td class="text-center"><img class="img-thumbnail" title="Xitefun Causal Wear Fancy Shoes" alt="Xitefun Causal Wear Fancy Shoes" src="image/product/sony_vaio_1-50x50.jpg"></td>
<td class="text-left"><?php echo $ligneBase->id;?></td>
<td class="text-right">x 1</td>
<td class="text-right"><?php echo $ligneBase->name;?> </td>
<td class="text-center"><button class="btn btn-danger btn-xs remove" title="Remove" onClick="" type="button"><i class="fa fa-times"></i></button></td>
</tr>
<?php
}
?>
</tbody>
</table>
change it to
<table class="table">
<tbody>
<div id="text">
<?php $cart_check = $this->cart->contents();
// If cart is empty, this will show below message.
if(empty($cart_check)) {
echo '<h4 align="center">No Product in cart, To add products to your shopping cart click on "Add to Cart" Button</h4>';
} ?>
</div>
<?php
$cart = $this->cart->contents();
foreach($cart as $indice => $ligneBase){
?>
<tr>
<td class="text-center"><img class="img-thumbnail" title="Xitefun Causal Wear Fancy Shoes" alt="Xitefun Causal Wear Fancy Shoes" src="image/product/sony_vaio_1-50x50.jpg"></td>
<td class="text-left"><?php echo $ligneBase['id'];?></td>
<td class="text-right">x 1</td>
<td class="text-right"><?php echo $ligneBase['name'];?> </td>
<td class="text-center"><button class="btn btn-danger btn-xs remove" title="Remove" onClick="" type="button"><i class="fa fa-times"></i></button></td>
</tr>
<?php
}
?>
</tbody>
</table>
I am passing a table data value within a for each loop to the php script. The problem here is that when I try to echo $exchange;. I don't get any values of the specific product name. The product name being displayed in the form is item1.
<form action="exchangeItem" method="post">
<tbody>
<?php
$query = "SELECT * FROM product";
$data = $MySQLi_CON->query($query);
foreach ($data as $key ) {
?>
<tr>
<td class="id"><h3><input type="hidden" name="productName" ><?php echo $key['product_Name'];?></h3>
</td>
<input name = "id" type = "hidden" value="<?php echo $id; ?>">
<td id="exchange_button">
<input value="Exchange" name="exchange_submit" type="submit" class="btn btn-primary btn-md raised exchange_submit">
</td>
</tr>
<?php
}
?>
</tbody>
</form>
exchangeItem.php
<?php
$exchange = $_POST['productName'];
echo $exchange;
exit;
?>
This
<td class="id"><h3><input type="hidden" name="productName" ><?php echo $key['product_Name'];?></h3>
Should be:
<td class="id"><h3><input type="hidden" name="productName" value="<?php echo $key['product_Name'];?>" ></h3>
first, edit your td
<td class="id"><h3><input type="hidden" name="productName[]" value="<?php echo $key['product_Name'];?>" ></h3>
your name => productName should be in array thats why you need bracket [ ], because you use foreach. then your post will be in array too.
exchangeItem.php
<?php
print_r($_POST['productName']);
exit;
?>
I have added & changed your 2 input name productName[] , id[] and your form action exchangeItem.php if you get product name must be used input name with array like [] because you are used foreach loop.
<form action="exchangeItem.php" method="post">
<tbody>
<?php
$query = "SELECT * FROM product";
$data = $MySQLi_CON->query($query);
foreach ($data as $key ) {
?>
<tr>
<td class="id">
<h3><input type="hidden" value="<?php echo $key['product_Name'];?>" name="productName[]" ><?php echo $key['product_Name'];?></h3>
<input name="id[]" type="hidden" value="<?php echo $id; ?>">
</td>
</tr>
<?php
}
?>
</tbody>
<tfoot>
<tr>
<td id="exchange_button">
<input value="Exchange" name="exchange_submit" type="submit" class="btn btn-primary btn-md raised exchange_submit">
</td>
</tr>
</tfoot>
</form>
exchangeItem.php
<?php
if(isset($_POST['exchange_submit'])) {
$exchange = $_POST['productName'];
foreach($exchange as $product_name) {
echo $product_name;
}
}
?>
The problem you facing here is with this code
<td class="id">
<h3><input type="hidden" name="productName" ><?php echo $key['product_Name'];?></h3>
</td>
here in the input field you didn't have added the value, that's why when you trying to print the posted value in your action file it's showing none. So just add add value to the input field
<td class="id">
<h3><input type="hidden" name="productName" value="<?php echo $key['product_Name'];?>"></h3>
</td>
Set value for you hidden element... No need to place the hidden element in the header tag. Use productName[] for posting array of values.
<td class="id">
<h3><?php echo $key['product_Name'];?></h3>
<input type="hidden" name="productName[]" value="<?php echo $key['product_Name'];?>">
</td>
foreach all posted values to print.
<?php
$exchange = $_POST['productName'];
foreach($exchange as $key) {
echo $key;
}
exit;
?>
Update for select
Change yout html for select values..
<td class="id">
<h3><input type="checkbox" name="productName[]" value="<?php echo $key['product_Name'];?>"><?php echo $key['product_Name'];?></h3>
</td>
i want to make a cart and my product is pizza.
so i'm listing all the menu to the table, and add a button to submit menu to cart.
here's my index.php
<?php
require("connect.php");
$result = mysqli_query($con,'select * from menu');
?>
<form action="cart.php" method="get">
<table border="1" style="width:100%">
<tr>
<th>Pizza Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Buy</th>
</tr>
<?php while($product = mysqli_fetch_object($result)){ ?>
<tr>
<td><?php echo $product->nama_menu; ?></td>
<td><?php echo $product->harga_menu; ?></td>
<td><input type="number" name="quantity" min="1" max="20"></td>
<td><button type="submit" name="id" value="<?php echo $product->id_menu; ?>">Add To Cart</button></td>
</tr>
<?php } ?>
</table>
</form>
But when i pressed the button "Add To Cart", it sends all the quantity from the menu listing and can't be read in my cart.php
can anyone help me how to get the right quantity value when i pressed the button add to cart.
Make separate form for each of the item. Try below code.
<?php
require("connect.php");
$result = mysqli_query($con,'select * from menu');
?>
<table border="1" style="width:100%">
<tr>
<th>Pizza Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Buy</th>
</tr>
<?php while($product = mysqli_fetch_object($result)){ ?>
<form action="cart.php" method="get">
<tr>
<td><?php echo $product->nama_menu; ?></td>
<td><?php echo $product->harga_menu; ?></td>
<td><input type="number" name="quantity" min="1" max="20"></td>
<td><button type="submit" name="id" value="<?php echo $product->id_menu; ?>">Add To Cart</button></td>
</tr>
</form>
<?php } ?>
</table>
Try to use array in name then submit it will give you seperate quantity.
<td><input type="number" name="quantity[<?php echo $product->nama_menu; ?>]" min="1" max="20">
Output:
quantity['pizza1'] = 10
quantity['pizza2'] = 20
....
Another Option is use dynamic name for number.
<td><input type="number" name="quantity_<?php echo $product->nama_menu; ?>" min="1" max="20">
Output:
quantity_pizza1 = 10
quantity_pizza2 = 20
....
Im having a page that shows monthly subscriptions of a user which is created using codeigniter. what i want to do is when a the user clicks on make payment pass the values in the hidden files to the controller.
<?php echo form_open('options/done');?>
<table class="tables">
<thead>
<tr>
<th>Ref Code</th>
<th>Month</th>
<th>Year</th>
<th>action/th>
</tr>
</thead>
<tbody>
<?php foreach ($payments as $s =>$payment):?>
<?php $month = $payment['month'];?>
<input type="hidden" value="<?php echo $month;?>" name="month_<?php echo $s;?>" />
<input type="hidden" value="<?php echo $payment['ref_code'];?>" name="ref_<?php echo $s;?>" />
<tr>
<td><?php echo $payment['ref_code'];?></td>
<td><?php echo $month;?></td>
<td><?php echo $payment['year'];?></td>
<td><input type="submit" value="MAKE PAYMENT" class="red" /></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php echo form_close();?>
so when someone hits the submit button how can i pass only the hidden values which are relevant to that table row?
add form just inside the <tr> elements inside your loop (see below with your code)
<?php foreach ($payments as $s =>$payment):?>
<?php $month = $payment['month'];
?>
<tr>
<form action="target.php" method="post" name="formName_<?php echo $s;?>" >
<input type="hidden" value="<?php echo $month;?>" name="month_<?php echo $s;?>" />
<input type="hidden" value="<?php echo $payment['ref_code'];?>" name="ref_<?php echo $s;?>" />
<td><?php echo $payment['ref_code'];?></td>
<td><?php echo $month;?></td>
<td><?php echo $payment['year'];?></td>
<td><input type="submit" value="MAKE PAYMENT" class="red" /></td>
</form>
</tr>
<?php endforeach; ?>