how to update cart quantity in CodeIgniter? - codeigniter

I want to update the quantity of a shopping cart. I've google it a lot but couldn't do it.
Maximum pages are showing which i don't want. Exactly solution i'm not getting. Hopefully i will get here. Please help me to do it. Here is my view page.
**<table class="table table-hover table-bordered table-striped snipcart-details "> <thead>
<tr>
<th style="color:#FF0033; font-weight:bolder; text-align:center;" >Delete Cart</th>
<th style="color:#FF0033; font-weight:bolder; text-align:center;" >Product Name</th>
<th style="color:#FF0033; font-weight:bolder; text-align:center;" >Image</th>
<th style="color:#FF0033; font-weight:bolder; text-align:center;" >Price</th>
<th style="color:#FF0033; font-weight:bolder; text-align:center;" >Quantity</th>
<th colspan="2" style="color:#FF0033; font-weight:bolder; text-align:center;" >Total</th>
</tr>
</thead>
<tbody>
<?php foreach ($this->cart->contents() as $items) { ?>
<tr>
<td style="color:#000000; font-weight:bolder; text-align:center;" >
<a href="#" class="remove_cart" title="delete" row_id="<?php echo $items['rowid']; ?>" rel="1">
<i class="fa fa-times fa-2x" style="color:red;" aria-hidden="true"></i> </a></td>
<td style="color:#000000; font-weight:bolder; text-align:center;" ><?php echo $items['name']; ?></td>
<td align="center"><img src="<?php echo base_url('resource/allproduct/'.$items['productImage']);?>" height="50px;" /></td></td>
<td style="color:#000000; font-weight:bolder; text-align:center;"><?php echo $items['price']; ?></td>
<td align="center"><input type="number" name="qty" id="qty" value="<?php echo $items['qty']; ?>"></td>
<td colspan="2" style="color:#000000; font-weight:bolder; text-align:center;">TK <?php echo $this->cart->format_number($items['subtotal']); ?></td>
</tr>
<?php } ?>
</tbody>
<tbody>
<tr>
<th scope="row"> <input type="button" name="submit" value="Update" class="button" /> </th>
<td align="center"> <input type="button" name="submit" value="Continue Shopping" class="button" /></td>
<td colspan="3" class="button" align="center" >
<?php if(!empty($userid)){?>
<input class="button" type="submit" value="Place Order">
<?php } else {?>
<input class="button" type="submit" value="Place Order">
<?php }?>
</td>
<td align="center" style="color:#000000;" > <h4>Grand Total</h4> </td>
<td style="font-size:24px; font-weight:800; color:green;">TK <?php echo $this->cart->format_number($this->cart->total()); ?></td>
</tr>
</tbody>
</table>**
And here is controler.
public function index()
{
$data['basicinfo'] = $this->M_cloud->basicall('basic_info');
$where = array('status' => 1);
$data['categoryinfo'] = $this->M_cloud->categoryinfo('item_manage', $where);
$data['rows'] = count($this->cart->contents());
$data['userid'] = $this->session->userdata('user_id');
$data['subcategoryinfo'] = $this->M_cloud->findAll2('sub_category', array('status' => 1));
$data['menuinformation'] = $this->M_cloud->findReport('our_service', array('serviceType'=> 2), 'menuname asc');
$data['menuservice'] = $this->M_cloud->findReport('our_service', array('serviceType'=> 1), 'menuname asc');
$data['socialmedia'] = $this->M_cloud->findAll('social_tbl', 'name asc');
$data['newsinfo'] = $this->M_cloud->findAll('news_table', 'newstitle DESC');
$this->load->view('cartPage', $data);
}
public function buy()
{
$proId = $this->input->post('proId');
$Qty = $this->input->post('Qty');
$prosize = $this->input->post('prosize');
$result = $this->M_cloud->find('product_manage', array('proid' => $proId));
$data2 = array(
'id' => $proId,
'qty' => $Qty,
'name' => $result->proName,
'price' => $result->price,
'prosize' => $prosize,
'productImage' => $result->proimg1,
'product_code' => $result->procode
);
$this->cart->insert($data2);
redirect('cart');
}
public function deleteCartItem() {
$row_id = $this->input->post('row_id');
$data = array(
'rowid' => $row_id,
'qty' => 0
);
$this->cart->update($data);
}
Please help me how to update cart quantity. Thanks in Advance.

To update cart Item :
function updateCartItem(){
$data=array(
'rowid'=>$this->input->post('rowid',TRUE),
'qty'=> $this->input->post('quantity',TRUE)
);
if ($this->cart->update($data)) {
echo "Success";
}else{
echo "Faliure";
}
}
To remove the item from cart :
function deleteCartItem(){
$row_id = $this->input->post('row_id',TRUE);
if ($rowid) {
return $this->cart->remove($rowid);
}
}

Related

Edit Pages - Invalid argument supplied for foreach()

I wonder why I still having this error message appearing.
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/editpages.php
Line Number: 48
Backtrace:
File: C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\masterlinkci2\application\admin\views\editpages.php
Line: 48
Function: _error_handler
controllers/Cpages.php
public function editpagesupdate() {
$data['pagessuccess'] = '';
$pages_id = $this->uri->segment(3);
$data['pages'] = $this->Mpages->update_pages($pages_id);
$this->load->view('editpages', $data);
}
models/Mpages.php
public function update_pages($pages_id)
{
$data = array(
'pages_name' => $this->input->post('pages_name'),
'create_date' => $this->input->post('create_date'),
'pages_order' => $this->input->post('pages_order'),
'pages_content' => $this->input->post('pages_content')
);
$this->db->where('pages_id', $pages_id);
$query = $this->db->update('pages', $data);
//return $query->result_array();
return $query;
}
views/editpages.php
<div class="widget-box">
<div class="widget-title"><h5>Pages</h5></div>
<div class="widget-content">
<?php echo form_open('cpages/editpagesupdate'); ?>
<table border="0" style="width: 100%; height: 90px;">
<tr>
<td>Pages Name</td>
<td><input type="text" name="pages_name" value="<?php echo $pages_item['pages_name']; ?>"></td>
</tr>
<tr>
<td>Create Date</td>
<td><input type="text" name="create_date" value="<?php echo $pages_item['create_date']; ?>"></td>
</tr>
<tr>
<td>Order</td>
<td><input type="text" name="pages_order" value="<?php echo $pages_item['pages_order']; ?>"></td>
</tr>
<tr>
<td>View Content</td>
<td><textarea rows="3" cols="20" name="pages_content"><?php echo $pages_item['pages_content']; ?></textarea></td>
</tr>
<tr>
<td></td>
<td><input type="submit" class="edit" name="submit" value="SUBMIT"></td>
</tr>
</table>
</div>
</div>
<?php endforeach; ?>
controllers/Cpages.php
public function editpagesupdate() {
$data['pagessuccess'] = '';
$pages_id = $this->uri->segment(3);
$update = $this->Mpages->update_pages($pages_id);
$data = array();
/* get updated pages here */
if($update){
$data['pages'] = $this->Mpages->get_pages();
}
$this->load->view('editpages', $data);
}
models/Mpages.php
public function get_pages()
{
$query = $this->db->get('pages');
return $query->result_array();
}
views/editpages.php
<div class="widget-box">
<div class="widget-title"><h5>Pages</h5></div>
<div class="widget-content">
<?php echo form_open('cpages/editpagesupdate'); ?>
<table border="0" style="width: 100%; height: 90px;">
<?php foreach($pages as $pages_item) : ?>
<tr>
<td>Pages Name</td>
<td><input type="text" name="pages_name" value="<?php echo $pages_item['pages_name']; ?>"></td>
</tr>
<tr>
<td>Create Date</td>
<td><input type="text" name="create_date" value="<?php echo $pages_item['create_date']; ?>"></td>
</tr>
<tr>
<td>Order</td>
<td><input type="text" name="pages_order" value="<?php echo $pages_item['pages_order']; ?>"></td>
</tr>
<tr>
<td>View Content</td>
<td><textarea rows="3" cols="20" name="pages_content"><?php echo $pages_item['pages_content']; ?></textarea></td>
</tr>
<?php endforeach; ?>
<tr>
<td></td>
<td><input type="submit" class="edit" name="submit" value="SUBMIT"></td>
</tr>
</table>
</div>
</div>

Adding student in only specific class list using codeigniter

I have a problem in viewing only a class list that I choose and add student but when I select other class it will also appear/view that I select in previous class
here I select the class that I select
Here is the class page that I select and then I add student
but when I choose another class it will also appear the student
here is my controller:
public function viewspecificclass($id)
{
if($this->session->userdata('logged_in'))
{
$this->output->set_header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
$this->output->set_header('Cache-Control: no-cache, no-store, must-revalidate, max-age=0');
$this->output->set_header('Cache-Control: post-check=0, pre-check=0', FALSE);
$this->output->set_header('Pragma: no-cache');
$session_data = $this->session->userdata('logged_in');
$data['Username'] = $session_data['Username'];
$row=$this->model_adminlogin->getonerowclass($id);
$data['r']=$row;
$data['classid'] = $id;
$this->load->view('individualviewclass',$data);
} else{
redirect('welcome', 'refresh');
}
if((!isset($session_data) || $session_data !=TRUE)) {
redirect('welcome', 'refresh');
}
}
public function addstudentnow($classid, $studentid){
$data= array(
'ClassId'=>$classid,
'Id'=> $studentid
);
$this->db->insert('enroll',$data);
$this->session->set_flashdata('category_success', 'Successfully Add!');
redirect('enrollment/viewspecificclass ');
}
here is my model:
function statusofenrollment(){
$this->db->select('*');
$this->db->from('studentinformation');
$this->db->join('statusofenrollment', 'statusofenrollment.Id = studentinformation.Id', 'left');
$query = $this->db->get();
return $query->result();
}
function studentinclasslist(){
$this->db->select('*');
$this->db->from('statusofenrollment');
$this->db->join('studentinformation', 'studentinformation.Id = statusofenrollment.Id', 'right');
$this->db->join('enroll', 'enroll.Id = studentinformation.Id', 'right');
$query = $this->db->get();
return $query->result();
}
here is the view:
<div class="z table-responsive">
<table class=" table table-striped ">
<thead>
<tr>
<th class="text-center">LRN</th>
<th class="text-center">First name</th>
<th class="text-center"> Middle name</th>
<th class="text-center">Last name</th>
<th class="text-center">Gender</th>
<th class="text-center">Status of Enrollment</th>
<th class="text-center"> </th>
</tr>
</thead>
<?php
foreach($this->model_adminlogin->studentinclasslist() as $row){
?>
<tr>
<td class="text-capitalize text-center"><?php echo $row->Idnumber ?></td>
<td class="text-capitalize text-center"><?php echo $row -> Firstname ?></td>
<td class="text-capitalize text-center"><?php echo $row->Middlename ?></td>
<td class="text-capitalize text-center"><?php echo $row->Lastname ?></td>
<td class="text-capitalize text-center"><?php echo $row->Sex ?></td>
<td class="text-capitalize text-center"><?php echo $row->Statusofenrollment ?></td>
<td class="text-capitalize text-center">
</td>
</tr>
<?php
}
?>
</table>
</div>
<div id="myModalpick" class="modal fade" role="dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title text-center">Student Status</h4>
</div>
<div class="modal-body">
<div class="z table-responsive">
<table class=" table table-striped ">
<thead>
<tr>
<th class="text-center">LRN</th>
<th class="text-center">First name</th>
<th class="text-center"> Middle name</th>
<th class="text-center">Last name</th>
<th class="text-center">Gender</th>
<th class="text-center">Status of Enrollment</th>
<th class="text-center"> </th>
</tr>
</thead>
<?php
foreach($this->model_adminlogin->statusofenrollment() as $row){
?>
<tr>
<td class="text-capitalize text-center"><?php echo $row->Idnumber ?></td>
<td class="text-capitalize text-center"><?php echo $row -> Firstname ?></td>
<td class="text-capitalize text-center"><?php echo $row->Middlename ?></td>
<td class="text-capitalize text-center"><?php echo $row->Lastname ?></td>
<td class="text-capitalize text-center"><?php echo $row->Sex ?></td>
<td class="text-capitalize text-center"><?php echo $row->Statusofenrollment ?></td>
<td class="text-capitalize text-center">
Add To Class List
</td>
</tr>
<?php
}
?>
</table>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-default" >Ok</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
because of the fact that your code is totally unorganized it makes hard to help you - but i'll give it a shot
first of all in your models Function studenClassList you've to pass an id from the specific class because other wise you'll get on any request the students of all of your classes - change your model like this
function studentinclasslist($classId){
$query = $this->db
->select('studentinformation.*')
->from('statusofenrollment')
->join('studentinformation', 'studentinformation.Id = statusofenrollment.Id', 'right')
->join('enroll', 'enroll.Id = studentinformation.Id', 'right')
->where("class.Id",$classId)
->get();
return $query->result();
}
be aware that you have to change in the where construct the actual class.Id field because i wasn't able to see where it is
and after that in your view simply pass the ID to your model function
foreach($this->model_adminlogin->studentinclasslist($classId) as $row){
but in any case i must say, in my opinion it isn't a good idea in CI to use a model in your view because the controller should pass the prepared model data to the view

Error update row in codeignaitor

i have error when i update row to database.
my problem is not update the row and call me when i upload pic with update the file upload not define in index.
my controller :
public function addedit(){
$events = new events_model();
$prodID = $this->input->post('hID');
$text = $this->input->post('ev_text');
$pic = $this->do_upload('ev_pic');
$submit = $this->input->post('submit');
if($submit){
if(!empty($prodID)){
$this->data['events'] = $events->UpdatePost($prodID, $text , $pic );
$this->data['pagetitle'] = "Show Info";
$this->data['success']= "Update Done";
}else{
$this->data['success'] = "Add Done";
}
$this->data['status'] = "1";
}else{
$this->data['status'] = "0";
$this->data['errors'] = "Error";
}
$this->template->build('admin/events/add',$this->data);
}
and model :
function UpdatePost($prodID, $text , $pic ) {
if ($pic == ""){
$vales = array('ev_id' => $prodID , 'ev_text' => $text);
}else{
$vales = array('ev_id' => $prodID , 'ev_text' => $text , 'ev_pic' => $pic);
}
$query = $this->db->update($this->table_name, $vales) or die (mysql_error());
$result = array();
$result["process"] = "ok";
echo "1";
}
and view :
<div class="widget-body">
<table class="data-table" action="./administrator/categories/delete/"><!-- Table Conversion: See Section E in custom.js -->
<thead>
<tr>
<th class="sorting no-background" rowspan="1" colspan="1" style="width: 35px;"><input type="checkbox" id="checkall" /></th>
<th class="sorting" rowspan="1" colspan="1" style="width: 343px;">Tittle</th>
<th class="sorting" rowspan="1" colspan="1" style="width: 100px;">Date</th>
<th class="sorting no-background" rowspan="1" colspan="1" style="width: 70px;">#</th>
</tr>
</thead>
<tbody>
<?php $ctr = 0; ?>
<?php foreach ($events as $n): ?>
<tr>
<td><input class="checkrow" type="checkbox" value="<?php echo $n->ev_id; ?>" /></td>
<td class=" sorting_1"><?= $n->ev_text; ?></td>
<td class="center"><?= $n->ev_date; ?></td>
<td>
<span>Edit</span>
<span>Del</span>
</td>
</tr>
<?php endforeach ?>
</tbody>
</table>
<div class="clear"></div>
</div>
<div class="clear"></div>
</div>
<div id="table-extra">
<div id="table-action">
<input action="./administrator/<?php echo $this->uri->segment(2)."/deleteitems" ?>" class="btn-theme table-button btn-hover-black DeleteItems" id="submit" type="button" value="Delete" />
</div>
<div class="paginate">
<ul>
<?= $this->pagination->create_links(); ?>
</ul>
<div class="clear"></div>
</div>
<div class="clear"></div>
</div>
</div>
</div>
where problem in my code , and i wont to update success.
you don't load models like this $events = new events_model(); in CI, you should do it like this :
$this->load->model('Model_name');
$this->Model_name->function();
ref : http://ellislab.com/codeigniter/user-guide/general/models.html#loading

store session cart to database

Sorry for asking this old questions, and I know that I've read before I ask here, it's can use database for adding more cart without limitation. I already try to use ci_sessions table to store session but still no luck, I only can adding 6 items maximum.
please help me, I looking for some example for this almost two days and result is nothing
EDITED
this is my view
<table id="box-table-a" summary="Employee Pay Sheet">
<thead>
<tr>
<th scope="col">Description</th>
<th scope="col">Price</th>
<th class="centered" scope="col">Options</th>
</tr>
</thead>
<tbody>
<?php foreach ($foto_produk->result() as $key => $value) {?>
<tr>
<td><?php echo $value->description;?></td>
<td><?php echo $value->price;?></td>
<td class="centered"><input type="checkbox" name="produk_foto[]" value="<?php echo $value->id;?>" /></td>
</tr>
<?php }?>
</tbody>
</table>
here's my controller code
if($this->input->post('produk_foto')){
$id_foto = $this->input->post('produk_foto');
foreach ($id_foto as $key => $value) {
$this->db->where('id', $value);
$query = $this->db->get('foto_product');
if($query->num_rows() > 0){
foreach($query->result() as $ids => $rows){
echo $rows->id.'<br />';
$data_produk = array(
'user_data'=> array(
'id' => $rows->id,
'price' => $rows->price,
'name' => $rows->description,
'qty' => $rows->aantal
)
);
$this->cart->insert($data_produk);
}
}
}
}
and this my view code
<?php if(!$this->cart->contents()):?>
<div class="alert-box warning">The regular products are empty.</div>
<div class="clearfix"></div>
<?php else:?>
<hr>
<h4>REGULAR PRODUCTS</h4>
<div class="order_detail" id="Display">
<table>
<thead>
<tr>
<th>DESCRIPTION</th>
<th>QUANTITY</th>
<th>PRICE PER ITEM(S)</th>
<th>TOTAL</th>
<th>REMOVE</th>
</tr>
</thead>
<tbody>
<?php foreach($this->cart->contents() as $rows):?>
<tr>
<td style="font-weight:bold;"><?php echo $rows['name'];?></td>
<td>
<?php echo form_open(current_url());?>
<input type="text" size="3" name="quantity" value="<?php echo $rows['qty'];?>" />
<input type="hidden" size="3" name="rowid" value="<?php echo $rows['rowid'];?>" />
<input type="submit" name="update" value="Update" />
<?php echo form_close();?>
</td>
<td><?php echo $rows['price'];?></td>
<td><?php echo $this->cart->format_number($rows['subtotal']);?></td>
<td>delete</td>
</tr>
<?php endforeach;?>
</tbody>
<tfoot>
<tr>
<td colspan="3">Total Products</td>
<td colspan="3">€ <?php echo $this->cart->format_number($this->cart->total());?></td>
</tr>
<tr>
<td colspan="3">Total Shipping</td>
<td colspan="3"></td>
</tr>
<tr>
<td colspan="3"></td>
<td colspan="3" style="padding:0;text-align:center;">
<p>TOTAL :</p>
<span class="tot">€ <?php echo $this->cart->format_number($this->cart->total());?></span>
</td>
</tr>
</tfoot>
</table>
</div>
<?php endif;?>
with this code I want to insert using checkbox with array, and I have more than 6 checkbox
thank you in advance
Ok it's solved by my self..
I don't know that in the name of product there are special characters..
and shame of me..
but thank you anyway

A Database Error Occurred Error Number: 1146 in codeigniter

I am new to codeigniter where I tried form validation.
Here is my View Code:
<?php echo validation_errors(); ?>
<?php echo form_open('enquiry'); ?>
<table width="580" border="0" cellspacing="0" cellpadding="5">
<tr>
<td height="30" align="right" valign="middle" class="normal-text-style"> *Full Name:<br /></td>
<td height="30" align="left" valign="middle" class="normal-style"><input type="text" name="name" value="<?php echo set_value('name'); ?>" class="textbox-style01" /></td>
</tr>
<tr>
<td height="30" align="right" valign="middle" class="normal-text-style"> *Company: <span id="mndCompany"> </span><br /></td>
<td height="30" align="left" valign="middle" class="normal-style"><input type="text" name="company" value="<?php echo set_value('company'); ?>" class="textbox-style01" /></td>
</tr>
<tr>
<td height="30" align="right" valign="middle" class="normal-text-style"> *E-mail:</td>
<td height="30" align="left" valign="middle" class="normal-style"><input type="text" name="email" value="<?php echo set_value('email'); ?>" class="textbox-style01" /></td>
</tr>
<tr>
<td height="30" align="right" valign="middle" class="normal-text-style"> *Phone: <br /></td>
<td height="30" align="left" valign="middle" class="normal-style"><input type="text" name="phone" value="<?php echo set_value('phone'); ?>" class="textbox-style01" /></td>
</tr>
<tr>
<td height="30" align="right" valign="middle" class="normal-text-style"> *Fax: <br /></td>
<td height="30" align="left" valign="middle" class="normal-style"><input type="text" name="fax" value="<?php echo set_value('comments'); ?>" class="textbox-style01" /></td>
</tr>
<tr>
<td width="179" height="30" rowspan="2" align="right" valign="top" class="normal-text-style"> *Comments: <br /></td>
<td width="421" height="12" align="left" valign="middle" class="normal-style"><textarea name="comments" class="msgebox-style01"><?php echo set_value('comments'); ?></textarea></td>
</tr>
<tr>
<td height="50" align="left" valign="top" class="normal-style"><input type="image" name="submit" src=" <?php echo base_url(); ?>/public/images/submit01.png" alt="submit" /></td>
</tr>
</table>
</form>
And this is my controller
function index()
{
$this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean');
$this->form_validation->set_rules('company', 'Company', 'trim|required|xss_clean');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[users.email]');
$this->form_validation->set_rules('phone', 'phone', 'trim|required|xss_clean');
$this->form_validation->set_rules('fax', 'fax', 'trim|required|xss_clean');
$this->form_validation->set_rules('comments', 'comments', 'trim|required|xss_clean');
$data['header_menu']=$this->load->view('header_menu', '', TRUE);
$this->load->view('inner_header',$data);
$data['left_menu']=$this->load->view('left_menu', '', TRUE);
if ($this->form_validation->run() == FALSE)
{
$this->load->view('enquiry',$data);
}
else
{
$this->load->view('enquiry/formsucess',$data);
}
$this->load->view('footer');
//$this->load->view('enquiry',$data);
}
function formsucess()
{
$name = $this->input->post('name');
$company = $this->input->post('company');
$email = $this->input->post('email');
$phone = $this->input->post('phone');
$fax = $this->input->post('fax');
$comments = $this->input->post('comments');
$this->db->query("INSERT INTO `st_enquiry`
(name, company,email,phone,fax,comments,status,created_on)
VALUES
('$name','$company','$email','$phone','$fax','$comments','1',now())");
$data['header_menu']=$this->load->view('header_menu', '', TRUE);
$this->load->view('inner_header',$data);
$data['left_menu']=$this->load->view('left_menu', '', TRUE);
$this->load->view('media',$data);
$this->load->view('footer');
}
After submitting the form I got an error like:
A Database Error Occurred
Error Number: 1146
Table 'table.users' doesn't exist
checkout this link
Here
$data = array(
'title' => 'My title' , // column_name => value
'name' => 'My Name' ,
'date' => 'My date'
);
$this->db->insert('tablename', $data);
must work in any condition.
$this->form_validation->set_rules('email','Email','required|valid_email|is_unique[users.email]');
Here will be some change
is_unique[users.email]
01.here users mean your table name
02. email means email column in your table.
so it will be
$this->form_validation->set_rules('email','Email','required|valid_email|
is_unique[table name.email column in your table]');
A Database Error Occurred
Error Number: 1146
Table 'table.users' doesn't exist
I solved this problem in Codeigniter by changing name of table in phpmyadmin
i hope this can help you
Create a function in Model like:
$this->db->insert($this->table_name,$db_data);
return $this->db->insert_id();
and send data from Controller to Model

Resources