Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6686303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:09:22+00:00 2026-05-26T05:09:22+00:00

I’m trying to get edit functionality on my Shopping cart. I want it to

  • 0

I’m trying to get edit functionality on my Shopping cart.
I want it to be basically go to a page called users/view_cart, then the user selects whether they are only updating the cart(changing quantities) or are checking out (purchasing through a transaction).

I’m following the display the cart page provided at this location:

http://codeigniter.com/user_guide/libraries/cart.html

public function view_cart(){
    $this->load->model('purchases_model');
    $this->load->model('transactions_model');

    $this->load->helper('form');
    $this->load->library('form_validation');

    $this->form_validation->set_rules('action', 'action', 'required');

    $num = $this->cart->total_items();

    for($i = 1; $i <= $num; $i++){
        $this->form_validation->set_rules($i.'[qty]',
                                'Quantity of the '.$i.'th element of the cart',
                                            'required');

    }

    if($this->form_validation->run() === FALSE){
        $data['title'] = 'View your cart!';
        $this->load->view('templates/LI_header', $data);    
        $this->load->view('users/view_cart');
        $this->load->view('templates/LI_footer');
    } elseif($this->input->post('action') == 'checkout') {
        $data['title'] = 'Buy cart!';
        $id = $this->session->userdata('uid');
        $transactionsData = array(
                                'amount' => $this->cart->format_number($this->cart->total())
                                );  
        $tid = $this->transactions_model->insert($transactionsData);

        foreach($this->cart->contents() as $items){
            $wsid = $items['id'];
            $purchaseData = array(
                                'wsid'  =>  $wsid,
                                'uid'   =>  $id,
                                'tid'   =>  $tid
                                );
            $this->purchases_model->insert($purchaseData);
        }

        $this->load->view('templates/LI_header', $data);    
        $this->load->view('users/buy_cart_success');
        $this->load->view('templates/LI_footer');
        $this->cart->destroy();         
    } else {
        $data['title'] = 'Edit Cart!';

        /* Manual input of the rowid and new quantity work
        $cartData = array('rowid' => 'c4ca4238a0b923820dcc509a6f75849b','qty'=>5);
        $this->cart->update($cartData);
        */
        /*
        for($i = 1; $i <= $num; $i++){
            $rowid = $this->input->post($i.'[rowid]');
            $newQty = $this->input->post($i.'[qty]');
            $cartData = array(
                            'rowid' => $rowid, 
                            'qty'   => $newQty
                            );
            $this->cart->update($cartData);
        }
        print_r($formData);
        $this->load->view('templates/LI_header', $data);    
        $this->load->view('users/edit_cart_success');
        $this->load->view('templates/LI_footer');
    }
}

Start of the view_cart view:

<?php echo form_open('users/view_cart'); ?>

<table cellpadding="6" cellspacing="1" style="width:100%" border="0">

<?php echo validation_errors(); ?>

<tr>
  <th>QTY</th>
  <th>Item Description</th>
  <th style="text-align:right">Item Price</th>
  <th style="text-align:right">Sub-Total</th>
</tr>

<?php $i = 1; ?>

<?php foreach ($this->cart->contents() as $items): ?>

    <?php echo form_hidden($i.'[rowid]', $items['rowid']); ?>

    <tr>
      <td><?php echo form_input(array('name' => $i.'[qty]', 'value' => $items['qty'], 'maxlength' => '3', 'size' => '5')); ?></td>
      <td>
        <?php echo $items['name']; ?>

            <?php if ($this->cart->has_options($items['rowid']) == TRUE): ?>

                <p>
                    <?php foreach ($this->cart->product_options($items['rowid']) as $option_name => $option_value): ?>

                        <strong><?php echo $option_name; ?>:</strong> <?php echo $option_value; ?><br />

                    <?php endforeach; ?>
                </p>

            <?php endif; ?>

      </td>
      <td style="text-align:right"><?php echo $this->cart->format_number($items['price']); ?></td>
      <td style="text-align:right">$<?php echo $this->cart->format_number($items['subtotal']); ?></td>
    </tr>

<?php $i++; ?>

<?php endforeach; ?>

<tr>
  <td colspan="2"> </td>
  <td class="right"><strong>Total</strong></td>
  <td class="right">$<?php echo $this->cart->format_number($this->cart->total()); ?></td>
</tr>

</table>
</br>
<?php echo form_radio('action','update',FALSE); ?> Update your cart
</br>
<?php echo form_radio('action','checkout', FALSE) ?> Check out your cart
</br>
<p><?php echo form_submit('users/view_cart', 'Process'); ?></p>

I tried writing some debugging code but the results I got were inconclusive.
In particular:

for($i = 1; $i <= $num; $i++){
    $this->form_validation->set_rules($i.'[qty]','Quantity of the '.$i.'th element of the cart','required');
    $formData[$i]['qty']    = $this->input->post(intval($i).'[qty]');
    $formData[$i]['rowid']  = $this->input->post(intval($i).'[rowid]');
    }
    print_r($formData);

The above snippet outputs:

Array ( [1] => Array ( [qty] => [rowid] => ) [2] => Array ( [qty] => [rowid] => ) [3] => Array ( [qty] => [rowid] => ) ) 

Which is weird because it seems to map ” $i.'[qty]’ ” from the previous lines to the proper form validations but otherwise doesn’t map me the correct data from the hidden fields.

Help, please!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:09:23+00:00Added an answer on May 26, 2026 at 5:09 am

    You are generating array based form names. e.g.

    <input type="hidden" name="1[rowid]" value="1starrthing" />, causes your hidden form values to show in the post data as a sub array like so:

    [1] => Array
        (
            [rowid] => 1starrthing
            [qty] => 1
        )
    
    [2] => Array
        (
            [rowid] => 1starrthing222
            [qty] => 1
        )
    
    [3] => Array
        (
            [rowid] => 1starrthing3333
            [qty] => 1
        )
    

    So unless you’re going for that, try changing your hidden form name to rowid[] which will give you an array of rowids like so:

    [rowid] => Array
        (
            [0] => 1starrthing
            [1] => 1starrthing222
            [2] => 1starrthing3333
        )
    
    [1] => Array
        (
            [qty] => 1
        )
    
    [2] => Array
        (
            [qty] => 1
        )
    
    [3] => Array
        (
            [qty] => 1
        )
    

    OR prepend your counter to the name like this: name="rowid_.$i" which will give you:

    [rowid_1] => 1starrthing
    [1] => Array
        (
            [qty] => 1
        )
    
    [rowid_2] => 1starrthing222
    [2] => Array
        (
            [qty] => 1
        )
    
    [rowid_3] => 1starrthing3333
    [3] => Array
        (
            [qty] => 1
        )
    

    Also, it’s likely your values in your hidden inputs are empty unless for some reason your $items array has ‘qty’ as the key for each item. Try

    <?php echo form_hidden('rowid[]', $items); ?>
    

    For added debugging help, try turning profiling on; this will give you more info on post’s/queries and other stuff.

    Bottom of view file:

    $this->output->enable_profiler(TRUE);

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.