nce upon a time building a Web front to a store was an enormous task, involving expensive specialists. However, over time, technologies have developed that allow anybody to build their own store, and handle transaction fulfillment through the well-known, open access payment system of PayPal. In this article, you’ll build a fictional store that stocks and sells sportswear. You’ll need a PHP/MySQL-enabled Web server or site. If you don’t have one, don’t worry?all the details for building your own are in this article. If you don’t know any PHP or MySQL, the same article is also a good place to start.
To build the store, you need a database that contains information about the products that you have for sale and their current stock levels.
To build the table for products, use this SQL code:
CREATE TABLE `products` ( `id` int(11) NOT NULL auto_increment, `description` varchar(100) NOT NULL default '', `picurl` varchar(100) NOT NULL default '', `price` decimal(10,0) NOT NULL default '0', `stockcount` int(11) NOT NULL default '0', `weight` int(11) NOT NULL default '0', `category` int(11) NOT NULL default '0', `detail` text NOT NULL, PRIMARY KEY (`id`)) TYPE=MyISAM AUTO_INCREMENT=1 ;
This creates a table with the following fields:
- ID: a unique ID for the product type
- Description: a textual description of the product
- PicURL: a URL to a picture of the product
- Price: the cost of the product
- Stockcount: the number of items of this product presently in stock
- Weight: how heavy the product it (in pounds), used to calculate shipping
- Category: the category of the product (see more on this later)
- Detail: a detailed description of the product
Figure 1. The Products Table: This is data in the products table using PHPMY ADMIN. |
Load this database with some dummy information (a sample is available in the download) as described. You don’t need to set the ID field as it is an auto_increment type.
Next, you’ll need a table of categories for your data. This will make it easier for your customers to browse related data. For example, if your store sells soccer merchandise (as this one does), the category could be team-based. So when customers enter your site, they can browse based on team, or search for a specific item. Assigning a category ID to the team enables this, and a separate table that associates that category ID with some friendly text makes it easier for your shoppers.
The code below shows the SQL used to create the Categories table. Figures 1 and 2 show data in these fields using PHPMYADMIN.
Author’s Note: PHPMYADMIN is an open source PHP tool for managing online databases. If you want to use it, you can check it out in this article, or you can visit its homepage. |
CREATE TABLE `categories` ( `id` int(11) NOT NULL auto_increment, `description` varchar(100) NOT NULL default '', PRIMARY KEY (`id`)) TYPE=MyISAM AUTO_INCREMENT=1 ;
Figure 2. The Categories Table: This is data in the products table using PHPMY ADMIN. | Â | Figure 3. The Storefront in Action: A simple PHP page introduces customers to your store and allows them to browse or search. |
The next thing you will need is your storefront. This is a simple PHP page introducing people to your store and allowing them to browse or search your products. You can see some of the HTML code for this in the following code, and a screenshot of a sample one in Figure 3.
This example is very simple and straightforward. It allows your users to access your products from the storefront in one of two ways.
- They can browse the products using browse.php. The storefront has hyperlinks that allow you to look at the merchandise for each of the teams with stocked goods. You will notice that even though there are 10 hyperlinks, they all link to the same PHP page! They are differentiated by the URL parameter that they pass to this page (browse.php?teamid=x). When PHP runs the page, it pulls in that parameter and generates the data accordingly. You will see this in more detail later on. What is also nice about this page is that these links and parameters are generated automatically based on the categories information in the database. Therefore, if a new team joins the league and has products, or a team folds or changes its name, you simply edit the database and the Web page will follow. You don’t need to rewrite your page. The following page will show you how it’s done.
- Your users can also search your database for the specific item that they want. For a small DB like this one, the advantage isn’t obvious, but if you can imagine a larger store where they stock hundreds of different items for each team?from mugs to baby bibs?you may not want to page through hundreds of these to find that elusive calendar. Instead you would search for ‘Seattle Calendar’ or something like that, and get taken straight to a list of matching items. This functionality is achieved using an HTML form, that POSTs information to a PHP page (search.php) that runs the search against the data and renders the results. This form is shown in the next code block, and the code for the search results page is shown later.
Building the First Page
The first page that you will build is the browse page, browse.php. The easiest way to do this is to use an HTML designer like FrontPage or DreamWeaver to design a template HTML page that has the layout and details of the page. Browse.htm, which can be found in the source code for this article, was designed like this, and can be seen in Figure 4. You can then take this HTML and turn it into a PHP page by changing the file extension to .php, and adding the code to drive this page from a database.
If you look under the hood of this page, you will see that it has four tables.
The first table is at the top of the page with Team 1, Team 2, and Team 3 as columns. For the PHP, you will want to write X number of columns where X is the total number of categories, and the contents of each column will be the text associated with that category. Finally, you will want that text to link back to this page, passing the ID of the category to the page so that the rest of the page will display the products in that category. Take a look at the PHP/HTML mixture that achieves this:
$sql = "SELECT * from Categories"; $result = @mysql_query($sql); while ($row = mysql_fetch_array($result)) {?> '> }?>
As you can see, the code is quite straightforward. You first output the
(table column), embedding the contents of each record in turn within this TD. You want to embed two things: first, the ID, so that the URL link will read browse.php?ID=X where X is the ID of the current record. Second is the text within the TD, which you should set to the description of the current record. The other three tables on the template page are three examples of how the product details would be rendered. The real ones will be generated using an iteration through the products of that category within the database. The code for this is in Listing 1.
This may look a little difficult to read, but once you understand the HTML that constructed the template table you will see that it is quite straightforward. It simply queries the DB for the full list of products within the specific category, and then, looping through each one outputs a table with the picture, description and price details. It also has some logic to calculate the shipping cost based on the weight, and if the product is currently out of stock, it doesn’t give a link to buy that product. You can see this screen in action in Figure 5. You may notice that the teams I have used in this example are fictitious, so no pictures of their jerseys are available, and the pictures in the sample come from other teams. Searching
Looking back at the home page of the Web site, you will see that the form submits a text field called T1K to the search page. Your search PHP will then take this as a $_POST parameter, and can build the query from it as shown in below:
From here, the rest proceeds exactly like the browse screen (Listing 1) to produce identical results (Figure 5). Buying the Product with PayPal PayPal uses a Forms submission mechanism to process payments. All you have to do is put a form on your Web site that links to their site to send the payment to you. An example of a PayPal form that sends $75.00 to a PayPal account called '[email protected]':
Therefore, if you write a PHP page that generates this HTML, preloaded for the product ID, your PayPal e-mail address and relevant pricing, you can sell your goods online. If you remember back to the browse/search pages, they gave a hyperlink to buyit.php?id=X where X depends on which product you link. Using this information, you can query the DB for the details of X, and generate the correct button (see Listing 2). It would be much more efficient and user friendly if this code was part of browse.php and search.php, embedding the PayPal button on the search results instead of having another click. It was separated here for readability only. A user now clicking on this button will be taken through the workflow of PayPal to send you their hard earned money. The next steps could be to use the Instant Payment Notification scheme on PayPal to detect that the payment has been received, and as the ID of the product is known, this could call your shipping service to note that the goods need to be shipped to the buyer as well as automatically removing the inventory levels for the purchased products. This article hopefully gave you a good start into building your own online store, loading it with products and making those products browseable and searchable by your customers as well as offering a methodology whereby they can pay you for those products! Have fun, and happy selling! About Our Editorial ProcessAt DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere. See our full editorial policy. About Our JournalistCharlie Frank Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience. View Author BBVA makes unexpected $12.4b bid for Sabadell Johannah Lopez May 10, 2024 3:18 PM Upcoming CPI report could disrupt steady gold prices Johannah Lopez May 10, 2024 3:07 PM Apple unveils iPad models with eSIM technology Cameron Wiggins May 10, 2024 1:56 PM StartUp Hutch aids in revitalizing cherished local eatery April Isaacs May 10, 2024 1:14 PM How College Students Can Shape the Future of Tech Responsibility Kyle Lewis May 10, 2024 12:40 PM Future Trends in SaaS for Education: The Next Wave of Innovative Learning Apps Rashan Dixon May 10, 2024 12:23 PM Ethical dilemmas surface with digital afterlife technology April Isaacs May 10, 2024 11:55 AM Apple’s iPad ad stirs debate in art community Rashan Dixon May 10, 2024 11:31 AM BBVA’s €11.5 billion bid for Sabadell sparks concerns Rashan Dixon May 10, 2024 8:59 AM Oyo negotiating new funding for global expansion April Isaacs May 10, 2024 8:00 AM Deep tech startups thrive with strategic support and investment Cameron Wiggins May 10, 2024 7:39 AM USD/JPY approaches key 156.00 Forex threshold Johannah Lopez May 10, 2024 7:15 AM Food startups still thrive even after pandemic challenges Cameron Wiggins May 9, 2024 3:46 PM Biden hints at upgrading Vietnam’s economic status Rashan Dixon May 9, 2024 1:25 PM Leading corporations form alliance for significant industry shift Johannah Lopez May 9, 2024 11:22 AM Ex-Flexport executive launches Saga Ventures for seed-stage investments Noah Nguyen May 9, 2024 8:21 AM Disney shares suffer due to pandemic impacts April Isaacs May 9, 2024 7:44 AM Boca Raton emerges as Wall Street competitor Cameron Wiggins May 9, 2024 7:37 AM Tesla initiates cost-cutting layoffs amid strategic shift Johannah Lopez May 8, 2024 5:22 PM Venture capitalists form group to accelerate startup growth Johannah Lopez May 8, 2024 5:18 PM Workday’s user interface criticized despite popularity Rashan Dixon May 8, 2024 3:25 PM Sequoia develops framework for startup success Cameron Wiggins May 8, 2024 3:17 PM Island cybersecurity lands $175 million in Series D funding Noah Nguyen May 8, 2024 3:12 PM Former Starbucks CEO suggests turnaround strategies Cameron Wiggins May 8, 2024 11:55 AM Small businesses maneuver amid rising inflation April Isaacs May 8, 2024 7:56 AM |