Build a Shopping Cart Admin Tool for Your PHP Online Store [con't]
Administering the Online Shop
You need administration to enable a number of important tasks, including:
- Removing redundant data and in so doing freeing up resources
- Adding a new line of product (in this example, books to the Pleasure Reading, Inc. catalog)
- Adjusting prices
These are just some of the reasons why administering your online shop is important. With an administration interface, you can add new books, change the details of current books, and change the prices of books.
An administrative interface is more effective if you have a login script. It can then separate the administrators from other low-level users, and therefore make sure that only the administrator can do certain things.
- Add a new book
- Change book details, such as author and price
The first page is going to look like the index page of the main front page. The administrator will get to choose from a list of categories and then a list of books from that category will be shown. The difference here is that the booklist will give you the option to either change book details or remove the book entirely.
Depending on what you choose, the change.php page will be shown or the delete.php script will be executed. Let's look at the code of the first page in the administration application:
The code is fairly easy to understand. The first part of the code simply retrieves all the genres from the genres table after connecting to the database with the connect.php script:
If the query was successful, the returned rows are stored in the $num
variable:
Otherwise, an error message is stored in the appropriately named $errmsg
variable:
The reason for storing the returned rows in the $num
variable is because it is going to be used to create a dynamic table later.
Let's look at the HTML that actually presents the list of genres. First, the table headers are created:
Then the number stored in the $num
variable is used to iterate through the array of results that ultimately create the dynamic table rows:
The genre name and ID is used to make a hyperlink:
If no genres have been found, an appropriate message is displayed:
And that's basically all there is to the main page of the administration script. The next script that you are going to look at is the booklist.php script. It has exactly the same function as the listbooks.php script in the main application, but with a slight difference in that it provides an option to either remove a book or change book details.
Below is the code for the script:
Let's take a look at the PHP code. First, you need to filter the data before using it in a MySQL query. The main.php page sends over a variable called catid
. Now, based on the database schema of the genre table, you know that the category ID or catid is a number. So, you need to do the usual checks with the is_numeric()
function:
Once you have checked that the category ID is indeed numeric, you can then continue to run the query after escaping the catid with mysq_real_escape_string()
:
If the query returns rows, then you store the value in the $num
variable, which you will use to build a dynamic table with later on:
If there were any errors from the query, then it is stored in the $errormsg
variable:
Now, the booklist script dynamically creates a table with two headers: Title and Action. The former will list the titles of the books in that category, and the latter will list the Remove or Change options as hyperlinks:
Clicking on the "Remove" link takes the user to the delete script, and clicking on the "Change" link takes the user to the change.php script.
The last part of the script displays a message if no books are found:
Conclusion
With an online shopping cart administration tool in place, you can easily create as many administrators and non-administrators for your site as you need and manage their privileges and tasks accordingly.
Original: February 17, 2010