| Creating a Basic Shopping List Script with PHP | |
By Jordie Bodlay |
Published
01/29/2007
|
Web Technologies
|
Rating:
![]() ![]() ![]() ![]()
|
|
|
The ArticleBeing healthy is the "new" thing around the office at Interspire. Rodney and I have taken up the challenge of Mitch and Ed and have been keeping to a strict routine of exercise and a healthy diet. Instead of scouring the local take-aways for lunch everyday, we now pop on down to the supermarket to buy fresh produce and meats to make our own lunches. However, we always found ourselves in a situation where we would forget something. Sure we could be old fashion and make a list and put it on the office fridge, but then I asked myself, "why do that when I could whip up an online shopping list?" Our list became a hot little item around the office, so I thought I'd spend an hour explaing how you too can build your very own shopping list with. Keep in mind that you don't have to use this just as a shopping list – it can also be a To-Do list or practically a list of anything you need to remember. In this article I do assume you have a small amount of PHP and MySQL experience, so if you don't then you might have to stick to a written shopping list for now.
Let's get busy! First up we're going to create the MySQL database table. Here is the schema for the table we are going to use. CREATE TABLE `thelist` ( `itemid` int(11) NOT NULL auto_increment, `name` varchar(255) NOT NULL default '', `owner` varchar(255) default '', `purchased` int(11) NOT NULL default '0', `quantity` int(11) NOT NULL default '0', `reoccuring` int(11) NOT NULL default '0', PRIMARY KEY(`itemid`) ) TYPE=MyISAM;
We have 6 fields in 1 table for this script.
Now that we've setup the database we will set up our PHP script. To make it easy we'll keep everything in one file – unlike how we code our applications, mind you ;) Let's start off with the database connection: <?php $hostname = 'localhost'; $username = 'username'; $password = 'password'; $databasename = 'database'; if (!$connection_result = mysql_connect($hostname, $username, $password)) { die('Error Connecting to MySQL Database: ' . mysql_error()); } if (!$db_result = mysql_select_db($databasename, $connection_result)) { die('Error Selecting the MySQL Database: ' . mysql_error()); } // if we made it here, we are one with the database ?>
So far, so good. This code makes sure we're connected to the database. Once that's done we can do what we want with the table. Continuing on from there we are going to use a switch statement to select what action to take. We have all of our actions inside separate functions so that the code is easy to maintain and update at a later time. (this code follows on from above) <?php // check what action we are performing switch($_GET['action']){ case 'delete': DeleteItem(); break; case 'edit': EditItem(); break; case 'new': NewItem(); break; case 'save': SaveItem(); break; default: ShowList(); break; } ?> The functions are pretty self explanatory so I wont go into too much detail. You can download the shopping list script here to try it yourself. Just make sure you create the database table before running the script or you'll receive a nasty error! |
|
or 02-9262-7770 



Author/Admin)
