Professional CodeIgniter

(singke) #1

Chapter 10: Launch


300


Now that you have that in place, finish up the controller function:

function checkout(){
$this- > MOrders- > verifyCart();
$data[‘main’] = ‘confirmorder’;
$data[‘title’] = “Claudia’s Kids | Order Confirmation”;
$data[‘navlist’] = $this- > MCats- > getCategoriesNav();
$this- > load- > vars($data);
$this- > load- > view(‘template’);
}

For right now, create a blank view called confirmorder , which you ’ ll fill in below in the “ Integrating
Checkout ” section. Right now, you need to do a bit more cleanup before moving on.

What kind of cleanup? Well, for starters, did you notice that dollar amounts aren ’ t properly formatted if
the pennies round to the nearest 10? For example, 19.90 is displayed as 19.9 , and that won ’ t do. The
easiest way to fix this is to go into the MOrders model and address it in every function where you do
some math.

Because this would be a repetitive, error - prone process, the best thing to do is to create a function in your
MOrders model that does the clean - up work and then use that function to implement the fix.

So here ’ s a function that will convert any number into something that more closely approximates
currency:

function format_currency($number){
return number_format($number, °2,°‘.’,°‘,’);
}

The number_format() function is a handy PHP function that lets you format a number just about any
way you want. You pass in the number as your first argument. The second argument is how many
decimal places you want to display (it does all the work of rounding up or down). The third argument is
the decimal point separator (and here you can make changes for your own locale — e.g., some European
countries use a comma instead of a period). The fourth argument is the thousands separator (again,
conveniently changeable to your locale).

Your new format_currency() function can now be used in your other functions. Just don ’ t forget to
invoke it as $this - > format_currency(). Therefore, for example, in MOrders, you would do
something like this throughout:

$_SESSION[‘totalprice’] = $this- > format_currency($totalprice);

Don ’ t forget to update your shoppingcart view. There ’ s one place in there in which you are doing some
math on the fly, and that still needs to be covered by this process. You won ’ t be able to easily use the
function you created in the MOrders model, but you can use the number_format() function:

echo “ < td id=’li_total_”.$PID.”’ > ”.
number_format($row[‘price’] * $row[‘count’], 2,’.’,’,’)
.” < /td > \n”;
Free download pdf