WebProWorld
Dev Forum |
Online Directory I am trying to figure out how to build an online directory for this organization I am working with. What they are looking for is just like the one you will find at napps.com.
What are web scrapes? Can someone explain to me what "web scrapes" are and give me examples of where previous API's or methods have been created that implement web scrape technology.
Will Firefox change the way we program? Well, with more and more people turning to alternate browsers (especially Firefox) is it time for people to get into gear and make accessible, standards-supporting websites?
|
| Recent Articles | Navision Customization: C/SIDE, C/ODBC, C/FRONT, XBRL – development options Microsoft bought Navision, Denmark based software development company, along with Great Plains Software.
JavaScript: Redirecting URL Whenever you want to generate dynamic pages you often have to resort to using some server side scripting language such as PHP or ASP.
Microsoft CRM Development: SDK, C#, SQL, Exchange, Integration, Crystal Reports – overview for programmer Microsoft CRM is new player on the CRM software market.
Crystal Reports for Microsoft RMS – Overview for Developer/report Designer Microsoft Retail Management System (MS RMS), former QuickSell 2000 is very popular retail system, which is capable to automate the whole spectrum of retail businesses - from single store with inventory count needs up to the chain of supermarkets. |
| | 09.22.04
A Shopping Cart Class In PHP
By Amrit Hallan
Classes have eluded me (rather I have eluded them) since the beginning of my primitive programming days (remember Turbo C++?).
They have always hovered over the horizons. This hasn't nagged me much, for I've been managing without classes pretty well, but sooner or later I had to come in contact with them. So in the morning (and I hadn't slept the entire night) I executed a gut-wrenching plunge into the depths of classes, and the shopping cart class was the first thing I wrote. Although I have made content management systems and algorithmic quote generations in PHP, I had not added a shopping cart class code to my code repository. It was not as tough as I had envisaged. I could have done something simpler but scripts like "Hello world!" seem silly after a point.
A class, as what I make of it is, a container of sorts that not only contains the variables required to perform a task, but even the sub-tasks to achieve the main objective. So what do we need in a cart class? We need an array that can store item ids and their quantities, and we need some functions that help us add items, delete them, and change their quantities. It is always better to write down a list of things to be done and a list of things needed to accomplish that.
| Make more money from your site traffic, on your terms. >> start here |
|
We begin our class like this:
<?php
class ShoppingCart {
     var $items;
}
?>
$items is going to be an associative array to hold the precious information. Now that we have declared the container array, we need a function to add items to it.
<?php
class ShoppingCart {
     var $items;
     function add_items($product_id, $qty)
     {
       $this->items[$product_id]=$qty;
     }
}
?>
This is as simple as it looks. It accepts a product id (here in this case we can store anything, even the name of the product), makes it a key element of the associative array $items and gives it a value $qty.
How do we use this class? Interestingly, whenever we declare a variable when programming, we declare it as a "type", for instance, an integer type, or a char type, or a string type. You can perform certain operations using these types. These types are nothing but pre-defined classes carrying their intrinsic characteristics. In this case (our shopping cart class), we'll define a variables as "ShoppingCart" in the following manner:
Read the Full Article
About the Author: Amrit Hallan is a web designer and developer. He has experience in developing
PHP-MySQL applications as well as simple HTML websites. Visit his website --
http://www.bytesworth.com to read more articles and see the kind of work
he does. |
|