Hello everyone make sure to stop by and introduce yourself and do check out our design and programming sections.
Visit the Den and get some free stuff!

You are not connected. Please login or register

[PHP] Create dynamic Pages (creating a shop)

View previous topic View next topic Go down  Message [Page 1 of 1]

1 [PHP] Create dynamic Pages (creating a shop) on Sun Feb 22, 2015 3:44 pm

george

avatar

Admin
Admin
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

Code:

<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_host = "yourserverhost";
$database_host = "database"; // Your database name
$username_host = "username"; // Database Username
$password_host = "password"; // Database password
$host = mysql_pconnect($hostname_host, $username_host, $password_host) or trigger_error(mysql_error(),E_USER_ERROR);
?>

Create a page shop.php

This will be our categories page.

Code:

$result = mysqli_query($database,"SELECT * FROM categories");

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.

Code:

while($row = mysqli_fetch_array($result)) {
  echo "<div id='categorybox'>";
  echo "<div id='title'>";
  echo "<a href='".$row['link'].'.php'."'>";
  echo ($row['category']);
  echo "</a>";
  echo "</div>";
  echo "<a href='".'shop_cat'.'.php'.'?ref=H28271r821'.'&cat='.$row['c_id'].'&id=217536291'.'&pro=F27_7379021tf922'.'&h=127'.'&p=u_1283'.'&val=1728917b235253G728'.'&sho=186r287h29i'."'>";
  echo "<img style='width: 60%;' src='".'images/cat/'.$row['c_id'].'.png'."'>";
  echo "</a>";
  echo "</div>";
}

This extension in the code in the link is very important
Code:
&cat='.$row['c_id']

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.

Code:
mysqli_close($database);

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.

Code:

   $database=mysqli_connect("host","username","password","databasename");
      // Check connection
      if (mysqli_connect_errno()) {
           echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

start your php code with this.
Code:
$cat=$_GET['cat'];

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.
Code:

if ( $cat == 1)
{
  $result = mysqli_query($database,"SELECT * from p_product WHERE p_cat = 'performance'");
  while($row = mysqli_fetch_array($result)) {
  echo "<div id='categorybox'>";
  echo "<div id='title'>";
  echo "<a href='".$row['link'].'.php'."'>";
  echo ($row['p_name']);
  echo "</a>";
  echo "</div>";
  echo "<a href='".$row['link'].'.php'."'>";
  echo "<img style='width: 50%;' src='".'images/products/'.$row['p_id'].'.png'."'>";
  echo "</a>";
  if ($_SESSION['MM_UserGroup'] == 1) //admin thingy I added for editing
    {
        echo "<br><a href='".'editproduct'.'.php'."?p_id=".$row['p_id']."'> Edit Product</a><br>";
} else
    {
}
  echo "<div id='price'>";
  echo ("Price: ");
  echo ("$"." ".$row['p_price']);
  echo "</div>";
  echo ("<br>");
  echo "<div id='description'>";
  echo ("Product Description: <br> ");
  echo ($row['p_description']);
  echo "</div>";
  echo ("<br>");
  echo "<div id='addcart_box'>";
  echo ("<a class='addcart' href='#'>Add To Cart");
  echo ("</a>");
  echo "</div>";
  echo ("<br>");
  echo "</div>";
}
}
else if ( $cat == 2 ) {
  $result = mysqli_query($database,"SELECT * from p_product WHERE p_cat = 'interior'");
  while($row = mysqli_fetch_array($result)) {
  echo "<div id='categorybox'>";
  echo "<div id='title'>";
  echo "<a href='".$row['link'].'.php'."'>";
  echo ($row['p_name']);
  echo "</a>";
  echo "</div>";
  echo "<a href='".$row['link'].'.php'."'>";
  echo "<img style='width: 50%;' src='".'images/products/'.$row['p_id'].'.png'."'>";
  echo "</a>";
  if ($_SESSION['MM_UserGroup'] == 1)
    {
        echo "<br><a href='".'editproduct'.'.php'."?p_id=".$row['p_id']."'> Edit Product</a><br>";
} else
    {
}
  echo "<div id='price'>";
  echo ("Price: ");
  echo ("$"." ".$row['p_price']);
  echo "</div>";
  echo ("<br>");
  echo "<div id='description'>";
  echo ("Product Description: <br> ");
  echo ($row['p_description']);
  echo "</div>";
  echo ("<br>");
  echo "<div id='addcart_box'>";
  echo ("<a class='addcart' href='#'>Add To Cart");
  echo ("</a>");
  echo "</div>";
  echo ("<br>");
  echo "</div>";
}
}

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.
Code:
if ($_SESSION['MM_UserGroup'] == 1)

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! Smile


_________________
What do I do? Logos, Websites, Custom Graphics, Branding, mockups, and more.

My Portfolio - Blog - Shop
View user profile http://gareygraphics.com

View previous topic View next topic Back to top  Message [Page 1 of 1]

Share this topic!

URL Direct
BBcode
HTML
[PHP] Create dynamic Pages (creating a shop)

Permissions in this forum:
You cannot reply to topics in this forum