Create a new table called productinfo and create these fields with suitable data types and other attributes:

  • productID (this will be the same as the existing $_GET data used in productinfo.php e.g. glutenfree)
  • productName (the full name of the product e.g. Gluten free)
  • productDescription
  • productCalories

In a real system you would probably have many more fields recording nutritional information but this is enough to demonstrate the principles.

Now add in the data which will be the same as in productinfo.php but add a very short description and a random figure for calories.  This data is for testing so the description and calories are unimportant.

Database connection

In functions.php add the two lines of code which connect to a database.  No need to put them into a function - functions.php is included on all pages (make sure that it is) and so these two lines will be read automatically.

Changing productinfo.php

You currently have a list of products.  It is probably easiest to delete it and start again (leave the if and it's condition and the else but delete everything inside them).

Now re-create the list but this time based on data from the new table you just created:

  1. inside the if part (don't change the old if just the stuff inside it):
    1. echo the opening and closing ul tags (probably the same as your old version of this page)
      1. between those tags set up an SQL SELECT query to get all of the product IDs and names and put them into variables
    2. loop through those results outputting (echo) list items (<li>) with links in just like the original version but using data from the table in variables this time
    3. test it and it should look exactly the same as the original version
  2. inside the else show information about the product the user clicked on:
    1. store $_GET["product"] in a variable after making it safe with mysqli_real_escape_string()
    2. set up a SELECT query to get the data for the product matching the one in $_GET["product"] by using WHERE
    3. extract the data from the result and put it into four variables (no need for a loop as there is only one result)
    4. echo those variables with some HTML tags to get a reasonable layout
    5. test it by clicking on a link and you should see that product's details

Layout of HTML generated by PHP

Look at the HTML generated by your page when the list is visible (view source in the browser).  It will probably be a mess.  Use \t and \r\n to lay it out more sensibly.

Properly indented HTML

The page itself should not change when you do this.