php programming

Create Dynamic Pages with PHP

There is an easy way to change the output of what is on a page depending on what category a user chooses and it will take him to the same page but with different content for each one.

This will require a database and little knowledge of php.

We are going to make a car parts shop.

First we want to create a database and for this we will name it database.

Step 1: Create a table in your database and name it Categories.

Step 2: Then in the categories table add new rows and add a row for “id” “category” and “link” the link row will be like the category row but with no spaces so it can generate into a nice link.

Step 3: Proceed to input data which would be your categories into the table. EX: (first category, second category, and so on.)

Step 4: create a new table call it “products”

Step 5: create rows for “id” “name” “description” “price” and “category”

Step 6: add data being products and make the category match the other tables category as we will be using the category to determine where it will be placed.

It could like something like this.

Your database is now established and now we want to connect it to our file.

Create a host.php file

Create a page shop.php

This will be our categories page.

this selects all the data from our database table categories.

Then we want to create a while loop and link to the products page along with adding an image to the page as well using the c_id as the image value.

This extension in the code in the link is very important

because it allows for us assign a value to the url using a unique id so we can get the cat id from the next page

TIP: Above you will see extensions such as &id=217536291 or &pro=F27_7379021tf922 and this is a security protocol to hide your row names in the url by offering random alternatives to make the url very long and hard to read.

and now we close the connection.

Make a new page shop_cat.php which will display the categories products.

Now we can retrieve $get in the url and display the value based on what the user chooses.

Import your database.

start your php code with this.

Now we want to say if the $cat which we retrieved is equal to a number then we display this else if 2 display this else if 3 display this and so on. We are also going to select the table p_products where the category is any category and it display the products listed under those categories.

First we will display all products from performance category and then we will display all from the interior category.

This part is something I made for admins to be able to edit the products and there is a whole page for editing the product as well that I will not be covering today.

The img tags are using the product id to display the image associated with the product and if the product is the first 1 we would name the image1.png and it would show up on the first product image.

It will display the following information but categorized by the $get in the url.

Hope you enjoyed the tutorial and please leave any questions below. If I forgot to cover something let me know below thank you! 🙂

Rate this article:

[yasr_visitor_votes size=”medium”]