Professional CodeIgniter

(singke) #1

Chapter 5: Building a Shopping Cart


115


Notice that in the code below, the $TOTALPRICE variable is taken from the totalprice session variable.
One more thing that ’ s important to note: You may be wondering why there ’ s a class assignment of
“ process ” on each form field that contains the product count. In the upcoming sections, you ’ re going to
be adding some Ajax handlers to update the form, and you ’ re going to filter the form fields by class
name. It ’ s a handy trick made available to you by the Prototype library — more on this in the section
“ Adding Update Functionality to the Shopping Cart ” later in this chapter.


$TOTALPRICE = $_SESSION[‘totalprice’];
if (count($_SESSION[‘cart’])){
foreach ($_SESSION[‘cart’] as $PID = > $row){
$data = array(
‘name’ = > “li_id[$PID]”,
‘value’= > $row[‘count’],
‘id’ = > “li_id_$PID”,
‘size’ = > 5,
‘class’ = > ‘process’
);
echo “ < tr valign=’top’ > \n”;
echo “ < td > ”. form_input($data).” < /td > \n”;
echo “ < td id=’li_name_”.$PID.”’ > ”. $row[‘name’].” < /td > \n”;
echo “ < td id=’li_price_”.$PID.”’ > ”. $row[‘price’].” < /td > \n”;
echo “ < td id=’li_total_”.$PID.”’ > ”.$row[‘price’] * $row[‘count’].” < /td > \n”;
echo “ < /tr > \n”;
}

$total_data = array(‘name’ = > ‘total’, ‘id’= > ’total’, ‘value’ = > $TOTALPRICE);
echo “ < tr valign=’top’ > \n”;
echo “ < td colspan=’3’ > & nbsp; < /td > \n”;
echo “ < td > $TOTALPRICE “.form_hidden($total_data).” < /td > \n”;
echo “ < /tr > \n”;

echo “ < tr valign=’top’ > \n”;
echo “ < td colspan=’3’ > & nbsp; < /td > \n”;
echo “ < td > ”.form_submit(‘submit’, ‘checkout’).” < /td > \n”;
echo “ < /tr > \n”;

}else{
//just in case!
echo “ < tr > < td > No items to show here! < /td > < /tr > \n”;
}//end outer if count
? >
< /table >
< /form >
< /div >

At this point, you should have something similar to the view illustrated in Figure 5 - 4.

Free download pdf