Professional CodeIgniter

(singke) #1

Chapter 5: Building a Shopping Cart


114


< ?php
echo form_open(“welcome/search”);
$data = array(
“name” = > “term”,
“id” = > “term”,
“maxlength” = > “64”,
“size” = > “30”
);
echo form_input($data);
echo form_submit(“submit”,”search”);
echo form_close();
? >
< /div >

Displaying the Shopping Cart to the User


Remember the check you did in the cart() controller function? If there wasn ’ t a third URI segment, the
code displays every item in the user ’ s Shopping Cart? In this section, you learn how to do that. In
essence, the task breaks down into two major parts:

Display all items in the user ’ s Shopping Cart.

Create some JavaScript functions that allow the visitor to make changes to the Shopping Cart
(i.e., delete items, add more of a certain item, etc.).

In the previous section, you set up $data[ ’ main ’ ] to be blank if you were just displaying the Shopping
Cart for the user. Right now, you ’ re going to set this variable to a value of shoppingcart , as the plan is
to loop through the cart session variable in that view.

$data[‘main’] = ‘shoppingcart’;

Because creating a useful Shopping Cart is a bit complicated, you ’ re going to want to tackle the work in
several major steps.

Creating the Home Page View


The shoppingcart view is entirely based on the contents of the user ’ s Shopping Cart, which is stored in a
CodeIgniter session. In this view, you want to generate the HTML necessary by looping through the
session data. The idea is to create a form that contains a combination of text and only one column of
user - changeable form fields.

To keep things from getting too complicated, you ’ ll want to make good use of form_input() and other
handy shortcuts provided by the form helper.

If you need to brush up on forms, see “ The Form Helper ” section of Chapter 3.


Free download pdf