Copy the folder containing the five PHP practise pages (phppractise) and rename it sqlpractise.

Currently there is a list of products on the first page.  The client wants to have that list come from database so it can be easily added to without editing HTML.

Database definition

Table definition

Displaying the dataindex.php with SQL data

Load the page to test for errors but don't expect a list as there is no data yet.  You should just see the paragraph text.

Inserting the data

View index.php in a browser and you should see five things in the list just like it used to look (but with a paragraph before them).  You could add more products.


The first item in the list was a link and the client would like all products in the list to be links.  At the moment there is only a page for the first product but don't worry about that for now.

You need to find a way to create a unique link href for each product.  The easiest way is to use the product name as the page name.  To do this:

  1. Find the line which echoes the list items in index.php (inside the loop)
  2. just before that line create a variable called $url and put the text product1.php into it for now ($url="product1.php")
  3. in the echo line below add a link element (<a> and </a>) around the variable names (one link element around both of them not two link elements)
  4. add an href attribute to the opening a tag with the value in escaped quotes being $url (href=\"$url\")
  5. just for testing save the page and try any of the links (they should all take you to the one existing product page)
  6. rename the file product1.php to springy.php
  7. now change the $url line to create the page name for the link as $url="{$productName}.php";
  8. save and load the page and you should find each link is different when you hover over them and the first one still works

You could now create the other product pages and as long as you name them properly the links will work no matter how many products are added.