»  Home  »  Web Technologies  »  Creating a Basic Shopping List Script with PHP
Creating a Basic Shopping List Script with PHP
By Jordie Bodlay | Published 01/29/2007 | Web Technologies | Rating: ratingfullratingfullratingfullratingfullratingfull Unrated |
The Article

Being 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.

  1. itemid - Our primary field, used for making each row unique.
  2. name - The name of the item. e.g. 'Milk'
  3. owner - Sometimes with our list an item was for a specific person rather than the entire group, such as Eddie with his Pitted Olives or Rod with his shower caps Swiss cheese.
  4. purchased - This will be a binary field, 1 or 0. Used to indicate whether the item has been picked up yet.
  5. quantity - More often than not, with 2-6 of us eating this way, we'll want more than one of the things on the list.
  6. reoccuring - We find that we need to pick up the same things each week. This binary flag allows the item to stay on the list every week.

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!

3 Responses to "Creating a Basic Shopping List Script with PHP"


 
annette taylor Rating: ratingfullratingfullratingfullratingfullratingfull Unrated
said this on 31 Jan 2007 4:26:37 PM CDT
Great little script can wait to try it.
Thank you

 
ORamos Rating: ratingfullratingfullratingfullratingfullratingempty Unrated
said this on 01 Feb 2007 10:54:47 AM CDT
I'm going to try!
Thanks

 
web Rating: ratingfullratingfullratingfullratingfullratingfull Unrated
said this on 18 Jan 2008 3:50:59 PM CDT
it is great and will try it



Rate this article and leave a reply:
1 2 3 4 5
Poor Excellent
Your Name *: Email (private) *: Website:
Please copy the characters from the image below into the text field below. Doing this helps us prevent automated submissions.
Security Code: img

Web designers: Learn how to attract more clients and profit like the big guys. Subscribe to our newsletter.