Add and show list product in cart codeigniter - php

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>

Related

Paypal payment with sandbox accounts, TESTING

I implemented a shopping cart to checkout with PayPal. For a single item, it works, but for multiple items, it does not. Pressing the paypal Pay Now! button I got an error like this => "https://www.sandbox.paypal.com/webapps/shoppingcart/error?flowlogging_id=3451c32fea2df&code=LACK_OF_BASIC_PARAMS"
"Things don't appear to be working at the moment. Please try again later."
wow, nice! missing something? showing a little code here, 'checkout.php':
<?php require_once('../resources/includes/initialize.php'); ?>
<?php
$products = Product::find_product_items();
$count = count($products);
$items = 0;
$total = 0;
?>
<?php include(TEMPLATE_FRONT.DS."header.php"); ?>
<!-- Page Content -->
<div class="container">
<!-- /.row -->
<div class="row">
<h1>Checkout</h1>
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<table id="myTable" class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Product</th>
<th>Price</th>
<th>Quantity</th>
<th>Sub-total</th>
</tr>
</thead>
<?php
$i = 0;
foreach($products as $product){
$i = ++$i;
?>
<tbody id="<?php echo "t".$i ?>">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="business" value="agardo#business.example.com">
<input type="hidden" name="item_name_<?php echo $i; ?>" value="<?php echo $product->title; ?>">
<input type="hidden" name="item_number_<?php echo $i; ?>" value="<?php echo $product->id; ?>">
<input type="hidden" name="quantity_<?php echo $i; ?>" value="<?php echo $product->quantity; ?>">
<input type="hidden" name="amount_<?php echo $i; ?>" value="<?php echo $product->price; ?>">
<input type="hidden" name="currency_code_<?php echo $i; ?>" value="USD">
<!--<input type="hidden" name="return" value="https://localhost/public/thank_you.php">-->
<tr>
<td><?php echo $product->id; ?></td>
<td><?php echo $product->title; ?></td>
<td id="<?php echo "p".$i ?>"><?php echo "$".format_number($product->price); ?></td>
<td id="<?php echo "q".$i ?>"><?php echo $product->quantity; ?></td>
<td id="<?php echo "s_t".$i ?>"><?php echo "$".format_number($product->price*$product->quantity); ?></td>
<td>
<span class="btn btn-warning glyphicon glyphicon-minus" data-id="<?php echo $product->id; ?>"></span>
<span class="btn btn-success glyphicon glyphicon-plus" data-id="<?php echo $product->id; ?>"></span>
<span class="btn btn-danger glyphicon glyphicon-remove" data-id="<?php echo $product->id; ?>"></span>
</td>
</tr>
</tbody>
<?php $items+=$product->quantity;
$total+=$product->price*$product->quantity;
} ?>
</table>
<input type="image" src="http://www.paypal.com/en_US/i/btn/x-click-but01.gif" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
</form>
<!-- ***********CART TOTALS*************-->
<div class="col-xs-4 pull-right ">
<h2>Cart Totals</h2>
<table class="table table-bordered" cellspacing="0">
<tr class="cart-subtotal">
<th>Items:</th>
<td><span id="count" class="amount"><?php echo $items; ?></span></td>
</tr>
<tr class="shipping">
<th>Shipping and Handling</th>
<td>Free Shipping</td>
</tr>
<tr class="order-total">
<th>Order Total</th>
<td><strong><span id="total" class="amount"><?php echo "$".format_number($total); ?></span></strong> </td>
</tr>
</tbody>
</table>
</div><!-- CART TOTALS-->
</div><!--Main Content-->
<?php include(TEMPLATE_FRONT.DS."footer.php"); ?>
Any particular reason you are trying to integrate a form post to /cgi-bin/webscr in 2020? Please use https://developer.paypal.com/demo/checkout/#/pattern/client , or the server version there with two server side routes if you need to record the result in a database.
( If you do need to record the result in a database, you'll need a route for 'Set Up Transaction' and one for 'Capture Transaction', documented here: https://developer.paypal.com/docs/checkout/reference/server-integration/ -- and pair these routes with the above JavaScript approval flow )
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="business" value="agardo#business.example.com">
must be outside of the loop! END of it! :)

how to display contents from another table (codeigniter)?

I want to do crud on my page and I have to get content from another table and display it in a select tag. my plan was to make an update page wherein it will get the data from another table to display (in this case the category_name, cat_id was the foreign key at the menu table).
I already tried to join because that's what other forums have said but I think I was wrong.
help would really be appreciated.
this is my model:
function inputMenuToUpdate($menu_id){
$this->db->select('cat_id, category.category_id, category.category_name AS category_name');
$this->db->from('menu');
$this->db->join('category', 'menu.cat_id = category.category_id');
$this->db->where('menu_id',$menu_id);
return $query->row();
}
this is my controller:
public function inputToUpdate($menu_id){
$data['row'] = $this->adminMenuModel->inputMenuToUpdate($menu_id);
$this->load->view('adminEdit', $data);
}
this is my view:
<tbody>
<!-- $result is from the $data in CrudController-->
<?php foreach($result as $row){?>
<tr>
<td><?php echo $row->menu_name; ?></td>
<td><?php echo $row->category_name; ?></td>
<td><?php echo $row->price; ?></td>
<td><?php echo $row->description; ?> </td>
<td>
<i class="fa fa-pencil"></i>
</td>
</tr>
<?php } ?>
</tbody>
and this is my update page:
<div class="container" style="width: 40rem;">
<form method="POST" action="<?php echo site_url('adminMenuController/update'); ?> / <?php echo $row->category_id; ?>">
<button type="button" class="btn btn-link"> Go Back </button>
<h1> UPDATE MENU </h1>
<div class="border-bottom"></div>
<br>
<br>
<label for="upMName">New Drink Name</label>
<input type="text" name="mn" class="form-control" id="upMName" value="<?php echo $row->menu_name ?>"></input>
<!-- <label for="cat"> Category </label>
<select id="cat" class="form-control">
<option> <?php echo $row->category_name; ?> </option>
</select> -->
<label for="upPrice">New Price</label>
<input type="number" name="price" class="form-control" id="upPrice" value="<?php echo $row->price ?>"> </input>
<label for="updesc">New Description </label>
<input type="text" name="desc" class="form-control" id="updesc" value="<?php echo $row->description ?>"> </input>
<br>
<button type="submit" class="btn btn-info"> Update </button>
<button type="reset" class="btn btn-danger"> Cancel </button>
</form>
</div>

Codeigniter click the radio button and open modify model which displays data from the database

In my code there is a table that displays search results. When a user clicks on the radio button and then clicks on the modify item button, it should open up the modal with information about the item gotten from the database based on item id. Currently the modal opens up with information about all the items on the result page, not just the item selected using the radio button I don't know why it's not taking the value of the radio button and displaying everything.
Here's my code:
Controller:
class Search extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load->helper('form');
$this->load->model('searchModel');
$this->load->model('itemModal');
}
public function index(){
$this->load->view('base');
$this->load->view('search');
}
public function displayItem(){
//modify item button is clicked
if(isset($post['#modifyItem'])){
//radio button is checked
if(isset($post['id'])){
//value from the radio button
$id=$this->input->post("id");
$data['results'] = $this->itemModal->get_item_by_id($id);
//open modal with the results
$this->load->view('searchResult/#modifyItem',$data);
}
}
}}
Model:
<?php
class ItemModal extends CI_Model {
function __construct(){
parent::__construct();
}
function get_item_by_id($id){
$this->db->select('*');
$this->db->where('inventoryID =',$id);
// Execute the query.
$query = $this->db->get('inventory');
// Return the results.
return $query->result_array();
}
}
View:
<body>
<h1><center>Item List</center></h1>
<hr>
<div class="container">
<form method="post" action="<?php echo site_url('itemView/viewItems'); ?>">
<table>
<tr>
<th><center><input type="radio" name="id"></center></th>
<th>Inventory ID</th>
<th>Master Code</th>
<th><center>Item Name</center></th>
<th>Color Name</th>
<th><center>Location</center></th>
<th><center>Checkout Allowed</center></th>
</tr>
<?php foreach($results as $rows):?>
<tr>
<td><input type="radio" name="id" value="<?php echo $rows['inventoryID'] ?>" <?php echo set_radio('id', '$rows[inventoryID]'); ?>></td>
<td><?php echo $rows['inventoryID'] ?></td>
<td><?php echo $rows['masterCode'] ?></td>
<td><?php echo $rows['itemName'] ?></td>
<td><?php echo $rows['colorName'] ?></td>
<td><?php echo $rows['location'] ?></td>
<td><input type="checkbox" <?php if($rows['checkoutAllowed'] == 'Yes') echo " checked='checked' "; ?>></td>
</tr>
<?php endforeach; ?>
</table>
</form><br><br>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modifyItem" data-title="Modify an Item" onclick="<?php echo site_url("Search/displayItem"); ?>">Modify an Item</button>
<!-- Modify an Item Modal -->
<div id="modifyItem" class="modal fade">
<div class="modal-dialog">
<form action="<?php echo site_url("Search/updateItem"); ?>" method='POST'>
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Modify an Item</h4>
</div>
<div class="modal-body">
<form role="form">
<?php foreach($results as $rows):?>
<table>
<tr>
<td><input type="text" name="rfid" value="<?php echo $rows['inventoryID']?>"/></td>
<td><input type="text" name="itemCode" placeholder="Item Code"/></td>
<td><input type="text" name="masterCode" value="<?php echo $rows['masterCode']?>"/></td>
</tr>
<tr>
<td><input type="text" name="itemName" value="<?php echo $rows['itemName']?>" /></td>
<td><input type="text" name="colorCode" placeholder="Color Code" /></td>
<td><input type="text" name="colorName" placeholder="Color Name" /></td>
</tr>
<tr>
<td><input type="text" name="location" placeholder="Location" /></td>
<td><input type="text" name="makelocation" placeholder="Location Made"/></td>
<td><input type="text" name="itemCategory" placeholder="Item Category" /></td>
</tr>
<tr>
<td><input type="text" name="materialDescription" placeholder="Material Description" /></td>
<td><input type="text" name="supplier" placeholder="Supplier/Vendor" /></td>
<td><input type="text" name="checkoutAllowed" placeholder="Checkout Allowed" /></td>
</tr>
</table>
<div class="row personal-info">
<div class="col-sm-4">
<div class="form-group">
<textarea name="itemDescription" placeholder="Insert information regarding the weather this item is suitable for and where it is used"></textarea>
<textarea name="Comments" placeholder="Additional Coments on the Item"></textarea>
</div>
</div>
</div>
<?php endforeach; ?>
</form>
</div>
<div class="modal-footer" style="text-align:center;">
<input type="submit" class="btn btn-primary" name="modifyItem" value="Modify Item">
</div>
</div>
</form>
</div>
</div>
<!-- Modify an Item Modal -->
</div></body>
Why you load two time foreach its not good way when multiple record on database for load all data its take to more time use display record pagination library of codei-gniter and for edit make dynamic model code using ajax
your function in controller 'displayItem' write code for modify item and make edit form html
<table>
<tr>
<th><center><input type="radio" name="id"></center></th>
<th>Inventory ID</th>
<th>Master Code</th>
<th><center>Item Name</center></th>
<th>Color Name</th>
<th><center>Location</center></th>
<th><center>Checkout Allowed</center></th>
</tr>
<?php foreach($results as $rows):?>
<tr>
<td><input type="radio" name="id" value="<?php echo $rows['inventoryID'] ?>" <?php echo set_radio('id', '$rows[inventoryID]'); ?>></td>
<td><?php echo $rows['inventoryID'] ?></td>
<td><?php echo $rows['masterCode'] ?></td>
<td><?php echo $rows['itemName'] ?></td>
<td><?php echo $rows['colorName'] ?></td>
<td><?php echo $rows['location'] ?></td>
<td><input type="checkbox" <?php if($rows['checkoutAllowed'] == 'Yes') echo " checked='checked' "; ?>></td>
</tr>
<?php endforeach; ?>
</table>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modifyItem" data-title="Modify an Item" onclick="updateItem();">Modify an Item</button>
<!--Bootstrap model for edit start-->
<div id="myModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content" id="model_data">
//append form data here
</div>
</div>
</div>
<!--Bootstrap model for edit end-->
<script>
function updateItem()
{
var CHECKBOXIDS = PASS_CHECKBOX_CHECKEDVALUE;
$('#model_data').html('');
$.ajax({
url: "<?php echo site_url('Search/displayItem');?>",
type: "POST",
dataType: "html",
data: {'<?php echo $this->security->get_csrf_token_name(); ?>': '<?php echo $this->security->get_csrf_hash(); ?>', 'checkids': CHECKBOXIDS, },
catch : false,
success: function (data) {
$('#model_data').append(data);
}
});
}

value echoing only last value in the array in codeigniter

I have an array with values from a table. When var_dumped it shows also the expected values in the array. But when it is echoed, value in the last array is echoed. How can I echo all the values that are present in the array. My view is as below:
<div class="col-md-12">
<div class="col-md-6">
<div class="form-group">
<label>Appoinment Date</label>
<?php $attributes = array("name" => "Subscriptions add");
echo form_open("Admin/show_appoinment", $attributes);?>
<div class="input-group date">
<div class="input-group-addon">
<i class="fa fa-calendar"></i>
</div> <input type="hidden" name="chair" value="1">
<input type="date" name="n1" id="n1" class="form-control" value="<?php echo set_value('n1'); ?>" onChange="copy();">
</div>
</div> </div>
<div class="col-md-6 " style="margin-top:25px;">
<input type="submit" class="btn btn-sm btn-info ">
</div>
<?php echo form_close(); ?>
</div>
<?php $temp_array=[]; foreach($list as $row){array_push($temp_array, $row['time'], $row['op_no']); } ?>
<?php $attributes = array("name" => "Subscriptions add");
echo form_open("Admin/Add_Appointment", $attributes);?>
<?php $check=9.30; if(in_array($check, $temp_array)){
?>
<tbody id="table1">
<tr>
<td class="bg-danger"><input type="text" name="time" class="form-control" id="Dept_Id" value="9.30" readonly></td>
<td class="bg-danger"><input type="text" class="form-control" value=" <?php echo $row['op_no']; ?> " readonly></td>
<td class="bg-danger"><input type="text" class="form-control" value="<?php echo $row['p_name'];?>" readonly> </td>
<td class="bg-danger"><input type="text" class="form-control" value="<?php echo $row['name'];?>" readonly> </td>
<td class="bg-danger"><input type="button" value="Booked" class="btn btn-sm btn-danger glyphicon col-sm-offset-0">
<input type="button" value="Cancel" name="s1" class="btn btn-sm btn-warning glyphicon col-sm-offset-0"></td>
<?php } else{?>
<td class="bg-success">
<input type="hidden" name="chair" value="1">
<input type="hidden" name="date" id="n2" value="<?php echo set_value('n1');?>"/>
<input type="text" class="form-control" class="form-control" name="time" id="Dept_Id" value="9.30" readonly></td>
<td class="bg-success"><input type="text" class="form-control" name="op_no" id="op_no" value="" ></td>
<td class="bg-success"><select name="p_name" class="form-control" type="text" id="p_name"/>
<option></option>
<?php
foreach($patient->result() as $row)
{
echo '<option value="'.$row->patient_id.'">'.$row->p_name.'</option>';
}
?>
</select></td>
<td class="bg-success"><select class="form-control" name="dr_name" type="text" id="dr_name"/>
<option></option>
<?php
foreach($chair->result() as $row)
{
echo '<option value="'.$row->user_id.'">'.$row->name.'</option>';
}
?></select></td>
<td class="bg-success">
<input type="submit" value="Booking" class="btn btn-sm btn-info glyphicon col-sm-offset-0"></td>
<?php } ?>
</tr>
<?php echo form_close(); ?>
My Controller:
public function show_appoinment()
{
$date=$this->input->post('n1');
$chair=$this->input->post('chair');
$data['list']=$this->Appointment_model->get_appointment_by_date($date,$chair);
$data['chai'] =$this->Appointment_model->getdoctors_chair();
$data['chair'] =$this->Appointment_model->getdoctors_chair1($date);
$data['chairs'] =$this->Appointment_model->getdoctors_chair2($date);
$data['patient'] =$this->Appointment_model->getpatient();
$count['alert_count'] = $this->Stock_model->get_stock_for_alert_msg();
$this->load->view('Admin/header', $count);
if($data['list']){ //var_dump($data['list']); die();
$this->load->view('Admin/time_slot_view',$data);
}
else{
$this->load->view('Admin/time_slot_view2',$data);
}
$this->load->view('Admin/footer');
}
My issue is: if I have two rows fetched from db in the array, when I try to echo these data one in the last row echoes repeatedly. How could I solve this..Please help.

Codeigniter Update cart - how to foreach indise array?

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

Categories