CodeIgniter Forums
cart codeigniter - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: cart codeigniter (/thread-65160.html)



cart codeigniter - udin_ordinary - 05-08-2016

dear all developers CI,

i have a view_cart.php page and the code is below

Code:
<?php $this->load->view('top') ?>
<?php $this->load->library('cart'); ?>
<div id="show_back"></div>

<div class="main">
<div class="wrap">
<script src="<?php echo base_url() ?>files/js/jquery.min.js"></script>

<script>
       function change_img(url){
$('#on_chenge-img').html('<img src="'+url+'" style="width:263px;">');
}

function minquantity(rowid){
var minquan = parseInt($('#txtquantity_' + rowid).val()) - 1;
if(minquan > 0) {
//alert(quan);
$.ajax({
type: "POST",
url: "<?php echo base_url();?>application/controllers/ajax",
data: { action: "product_model.update_cart", row_id: rowid, new_qty: minquan}
}).done(function( msg ) {
var total = parseInt(msg);
if(total > 0){
$('#cart_count').html(total);
$('#txtquantity_'+rowid).val(minquan);
}
});

}
}

function plusquantity(rowid){
var plusquan = parseInt($('#txtquantity_' + rowid).val()) + 1;
//alert(quan);
$.ajax({
type: "POST",
url: "<?php echo base_url();?>application/controllers/ajax",
data: { action: "product_model.update_cart", row_id: rowid, new_qty: plusquan}
}).done(function( msg ) {
var total = parseInt(msg);
if(total > 0){
$('#cart_count').html(total);
$('#txtquantity_' + rowid).val(plusquan);
}
});
}


function del_item($rowid){
if(confirm('Remove this item from your cart?')) {
$.ajax({
type: "POST",
url: "<?php echo base_url();?>application/controllers/ajax",
data: { action: "product_model.del_item", row_id: rowid, new_qty: 0}
}).done(function( msg ) {
var total = parseInt(msg);
if(total > 0){
$('#cart_count').html(total);
$('#items_' + rowid).remove();
} else {
location.assign('<?php echo base_url('home/cart'); ?>');
}
});
}
}
function new_cart() {
if (window.confirm('Update The Cart?')){
                               window.location.href = "/some/url"

               }
               else
               {

               }            
       }
function show_inquiry(){
$('#inline1').css('display','block');
               $('#show_back').css('display','block');
}
function close_inq(){
if($('#btnSave').is(":visible")) {
$('#inline1').css('display','none');
$('#show_back').css('display','none');
} else {
location.assign('<?php echo base_url('home/cart'); ?>');
}
}
       










$(document).ready(function() {

             $("#submit_btn").click(function() {
     
   var proceed = true;
       
$("#contact_form input[required=true], #contact_form textarea[required=true]").each(function(){
$(this).css('border-color','');
if(!$.trim($(this).val())){ //if this field is empty
$(this).css('border-color','red'); //change border color to red  
proceed = false; //set do not proceed flag
}
//check invalid email
var email_reg = /^([\w-\.][email protected]([\w-]+\.)+[\w-]{2,4})?$/;
if($(this).attr("type")=="email" && !email_reg.test($.trim($(this).val()))){
$(this).css('border-color','red'); //change border color to red  
proceed = false; //set do not proceed flag
}
});
     
       if(proceed)
       {

           post_data = {
'user_name' : $('input[name=name]').val(),
'user_email'        : $('input[name=email]').val(),
'phone'                : $('input[name=phone]').val(),
'subject' : $('input[name=subject]').val(),
'address' : $('textarea[name=address]').val()
};
           
           //Ajax post data to server
           $.post('../contact_me.php', post_data, function(response){  
if(response.type == 'error'){ //load json data from server and output message    
output = '<div class="error">'+response.text+'</div>';
}else{
   output = '<div class="success">'+response.text+'</div><br><a href="clear_cart">Close</a>';
//reset values in all input fields
$("#contact_form  input[required=true], #contact_form textarea[required=true]").val('');
$("#contact_form #contact_body").slideUp(); //hide form after success
}
$("#contact_form #contact_results").hide().html(output).slideDown();
           }, 'json');
       }
   });
   
   //reset previously set border colors and hide all message on .keyup()
   $("#contact_form  input[required=true], #contact_form textarea[required=true]").keyup(function() {
       $(this).css('border-color','');
       $("#result").slideUp();
   });
});

function refresh_inquiry(){
/* location.reload(); */
}


</script>
<div class="login">
        <div class="wrap">
<?php if (empty($this->cart->contents() )){ ?>
<div class="login">
        <div class="wrap">
        <h4 class="title">Shopping cart is empty</h4>
        <p class="cart">You have no items in your shopping cart.<br>Click<a href="<?php echo base_url() ?>"> here</a> to continue shopping</p>
      </div>
    </div>
    <div class="clear"></div>
</div>
</div>
<br/><br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/><br/>
   <?php } else {?>
<div class="single">


<link href="<?php echo base_url();?>files/css/inquiry.css" rel="stylesheet" type="text/css" />
<!-- show popup inquiry -->
<div id="inline1">
<div id="inquery" align="center" style="margin-top:-120px;">
<div id="sub-inquery">
<div>
<div class="pull-left" style="width:84.5%; background-color:#939496; padding:2.7%; text-align:left; font-size:16px;min-height: 23px;">
<span style="color: rgb(255, 255, 255); font-family:Verdana;font-weight: bold; font-size: 15px;">INQUIRY FORM</span>
</div>
<div style="float: left; font-size: 13pt; font-weight: bold; width: 10.1%; padding-top: 2.7%;padding-bottom: 2.7%; background-color: rgb(98, 99, 102);min-height: 13px;">
<a href="javascript:void(0);" onclick="close_inq()" style="color:#fff;text-decoration:none;">x</a>
</div>
</div>
<br/>
<div class="form-style" id="contact_form">
   
   <div id="contact_results"></div>
   <div id="contact_body">
       <label><span>Name <span class="required">*</span></span>
           <input type="hidden" name="subject" id="subject" value="Inquiry Form"/>
           <input type="text" name="name" id="name" required="true" class="input-field"/>
       </label>
       <label><span>Email <span class="required">*</span></span>
           <input type="email" name="email" required="true" class="input-field"/>
       </label>
       <label><span>Phone <span class="required">*</span></span>
          <input type="text" name="phone" maxlength="15"  required="true" class="input-field" />
       
       </label>
       <label for="field5"><span>Address <span class="required">*</span></span>
           <textarea name="address" id="address" class="textarea-field" required="true"></textarea>
       </label>
       <label>
           <span>&nbsp;</span><input type="submit" id="submit_btn" value="Submit" />
       </label>
   </div>
</div>
</div>
</div>
</div>

<div class="container" style="padding-right: 20px;">
       <div class="pull-left" style="font-size:14px;font-weight: bold;padding-left:10px;color:#676767;margin-top: -2px;">
       <img src="<?php echo base_url() ?>files/images/cart.png" style="margin-top: -4px;">
<span>&nbsp;CART LIST</span>
</div>
<div class="pull-right font-hcart" style="font-size:14px;font-weight: bold;color:#676767;">
<?php echo strtoupper(date('j F Y'));?> : <a href="javascript:void(0);" onclick="show_inquiry()" style="text-decoration:none; color:#939496;">CHECKOUT</a>
</div>
<br><br>

<table style="margin-right: -3px; width:100%; float:right; font-size:14px; text-align:center; ">
<tr align="center">
<td width="10px">&nbsp;</td>
<td width="200px" valign="middle"><span style="font-size:12px;font-weight: bold;color:#676767;">PICTURE</span></td>
<td width="250px"><span style="font-size:12px;font-weight: bold;color:#676767;">PRODUCT NAME</span></td>
<td width="250px"><span style="font-size:12px;font-weight: bold;;color:#676767;">DESCRIPTION</span></td>
<td width="20px"><span style="font-size:12px;font-weight: bold;color:#676767;">QUANTITY</span></td>
<td width="20px"><span style="font-size:12px;font-weight: bold;color:#676767;">PRICE</span></td>
<td width="20px"><span style="font-size:12px;font-weight: bold;color:#676767;">SUB TOTAL</span></td>
</tr>
<?php
$i=1;
foreach ($this->cart->contents() as $items) :
$options = $this->cart->product_options($items['rowid']);


?>

       <tr id="items_<?php echo $items['rowid']; ?>" style="text-align:center;" >
<td >
<div style="color:#FFFFFF; background-color:#939496; padding:2px;width:22px;"><?= $i++ ?></div>
</td>
<td class="siz-cart_table"  style="padding-top:15px;">
<img src="<?php echo base_url() ?>uploads/<?=$items['image']?>" width="80px" height="auto" style="display:table-cell; vertical-align:middle; " >
</td>
<td class="border-table_style-right siz-cart_table">
<span style="font-size:12px;font-weight: bold;color:#676767;"><?= $items['mainid'] ?>.<?= $items['number'] ?></span> <br>
<span style="font-size:12px;color:#646464;" ><?= $items['name'] ?></span>
</td>

<td class="border-table_style-right siz-cart_table">
<span class="" style="font-size:12px;color:#646464;"><?= $items['note'] ?></span>
</td>
<td class="border-table_style-right siz-cart_table">
                             
                             
[color=#ff3333]<input type="text" id="qty" name="qty<?= $items['rowid'] ?>" value="<?= $items['qty'] ?>" style="width: 20px; margin-bottom: 0px; text-align:center; background-color: #FFFFFF;border-radius: 0;color:#aaa">[/color]

                             
</td>
<td class="border-table_style-right siz-cart_table">
<span class="" style="font-size:12px;color:#646464;">Rp. <?= number_format($items['price'],0,',','.') ?></span>
</td>
<td class="border-table_style-right siz-cart_table" style="text-align:center;border-right: 1px solid #cdcccc;">
<span class="" style="font-size:12px;color:#646464;">Rp. <?php echo $this->cart->format_number($items['subtotal']); ?></span>
                               <div style="position: relative; height: 50px; margin-top: 10px; margin-bottom: -50px; text-align: right;right: 10px;">
<a href="javascript:void(0);" onclick="del_item('<?= $items['qty'] ?>')" style="font-size:12px;text-decoration:none;color:#676767;">
Delete X
</a>
</div>
</td>
</tr>
<tr>
<td colspan="7"><hr style="margin-left: 0px;border: 1px solid #cdcccc; margin-bottom: 5px;margin-left: 0;margin-top: 5px;">
                       <?php echo form_hidden($i.'[rowid]', $items['rowid']); ?></td>
</tr>
<?php endforeach; ?>

<tr>
<td colspan="6" style="font-weight:bold;font-size:16px;">Total</td>
                       <td align="right" style="font-weight:bold;font-size:16px;">Rp. <?= number_format($this->cart->total(),0,',','.'); ?></td>

</tr>
</table>

<div class="">
<div class="font-body pull-right" style="padding-right:0px; padding-top: 10px;width:280px;">
<div class="text-avilable pull-left" style="width:54%;height:15px;margin-left:30px;font-weight: bold;color:#676767;">
<a href="<?php echo base_url(); ?>" class="" style="text-decoration:none;">
<span>CONTINUE SHOPPING</span>
</a>
</div>

<!---<div style="height: 15px; width: 3%;font-weight: bold;" class="text-avilable pull-left"><span>|</span></div>
<div class="text-avilable pull-right" style="width:21%;">
<a href="javascript:void(0);" onclick="new_cart()" style="text-decoration:none;font-weight: bold;color:#676767;">
<span>SUBMIT</span>
</a>---!>
</div>
</div>
</div>
<div class="clear"></div>
</div>
      </div>
<br/>

<?php
}
?>

<?php $this->load->view('footer') ?>


that i want is how to change the quantity when i click +/- and that also will change the quantity cart it self.

many thanks before

udin


RE: cart codeigniter - albertleao - 05-08-2016

Dude! Modularize your code! It's close to impossible to follow what is going on Smile


RE: cart codeigniter - udin_ordinary - 05-09-2016

(05-08-2016, 12:36 PM)albertleao Wrote: Dude! Modularize your code! It's close to impossible to follow what is going on Smile


dear albertleao

here is the main code for increase and decrease the quantity, but its not working..

function minquantity(rowid){
var minquan = parseInt($('#txtquantity_' + rowid).val()) - 1;
if(minquan > 0) {
//alert(quan);
$.ajax({
type: "POST",
url: "<?php echo base_url();?>ajax",
data: { action: "product_model.update_cart", row_id: rowid, new_qty: minquan}
}).done(function( msg ) {
var total = parseInt(msg);
if(total > 0){
$('#cart_count').html(total);
$('#txtquantity_'+rowid).val(minquan);
}
});

}
}

function plusquantity(rowid){
var plusquan = parseInt($('#txtquantity_' + rowid).val()) + 1;
//alert(quan);
$.ajax({
type: "POST",
url: "<?php echo base_url();?>ajax",
data: { action: "product_model.update_cart", row_id: rowid, new_qty: plusquan}
}).done(function( msg ) {
var total = parseInt(msg);
if(total > 0){
$('#cart_count').html(total);
$('#txtquantity_' + rowid).val(plusquan);
}
});
}


RE: cart codeigniter - kirasiris - 01-27-2018

(05-09-2016, 06:55 AM)udin_ordinary Wrote:
(05-08-2016, 12:36 PM)albertleao Wrote: Dude! Modularize your code! It's close to impossible to follow what is going on Smile


dear albertleao

here is the main code for increase and decrease the quantity, but its not working..

function minquantity(rowid){
var minquan = parseInt($('#txtquantity_' + rowid).val()) - 1;
if(minquan > 0) {
//alert(quan);
$.ajax({
type: "POST",
url: "<?php echo base_url();?>ajax",
data: { action: "product_model.update_cart", row_id: rowid, new_qty: minquan}
}).done(function( msg ) {
var total = parseInt(msg);
if(total > 0){
$('#cart_count').html(total);
$('#txtquantity_'+rowid).val(minquan);
}
});

}
}

function plusquantity(rowid){
var plusquan = parseInt($('#txtquantity_' + rowid).val()) + 1;
//alert(quan);
$.ajax({
type: "POST",
url: "<?php echo base_url();?>ajax",
data: { action: "product_model.update_cart", row_id: rowid, new_qty: plusquan}
}).done(function( msg ) {
var total = parseInt(msg);
if(total > 0){
$('#cart_count').html(total);
$('#txtquantity_' + rowid).val(plusquan);
}
});
}

dude you have to paste as a code block not just a simple text. Is almost impossible to detect the PHP in all that.


RE: cart codeigniter - InsiteFX - 01-28-2018

His methods are not going to update anything he posting to the base_url() and no method
to change the values.