Save sqlstyledtable.php as sqllinks.php AND as sqllinks2.php so you have two new copies.

You know what a link looks like in HTML.  You also know that you can add data onto the end of the link which PHP can then use through $_GET.  If you do not remember that it is worth re-reading it now (and look at getsend.php and getget.php) as what comes next is easy but only if you know the basics already.

The instructions below add links to the CD titles so that whichever one you click on you see a page with that CD's tracks.  You have everything you need to do that so feel free to do it yourself OR follow the instructions below.

The target (tracks) page - sqllinks2.phpThe track list

This page needs to show tracks rather than CDs.  For now just show the tracks for the first CD by using this query instead of the existing one:

SELECT trackOrder, trackTitle FROM secondtable WHERE cdReference=1 ORDER BY trackOrder ASC;

In the PHP change the heading, text, table headings, variable names and the index names until the page works (you will need to delete some).  Also delete the if which uses style to change the text colour where prices are below 5 and delete the variable $highlightclass a couple of lines lower.  Try it and you should see the tracks for the CD with the reference 1.

The links - sqllinks.php

The page is also almost fine as it is but you need to add a link to each CD title in the table.  We will do that in two stages to try to keep it simple (create normal links to the second page and then edit those links to put $_GET data in them to select specific CDs).  Do this:

  1. amend the echo line which outputs the CD titles:
    1. add a tags before and after the CD title
    2. add an href attribute to the opening tag with the value sqllinks2.php
    3. you will need to escape the double quotes around the value for href so if you have not done that yet do it now
    4. test those links once that is done (although obviously only the list for the first CD shows for all links)
  2. now add the $_GET data:
    1. change all of the URLs so that they include $_GET data in the format ?ref=$cdReference
    2. test the links by hovering over each one (you should see sqllinks2.php?ref=1 and so on)
  3. in sqllinks2.php you need to receive and use the $_GET data:
    1. add a line to the top of the middle block of PHP (just above the query)
    2. on it extract the CD reference value from $_GET  (as you did in getget.php) and put it into a variable called $cdReference
    3. echo that variable in a paragraph temporarily to test the code so far (you should see the number you clicked on so try clicking on a couple)
    4. delete or comment out that echo line once you are sure the links and the $_GET are working
    5. in the SQL query change cdReference=1 to cdReference=$cdReference

Save both pages.  Load sqllinks.php and click on one of the links.  The second page should now show tracks for that CD and not always the first CD.

The links with $_GET data

If that didn't make sense

The links allow the user to choose a CD.  That choice is passed to the second page in the URL.  You extract that from the URL.  You use that reference in the query.  Each time a different link is clicked on different data is shown.  Amazon and many others use this technique.  This site uses it.

It is worth following the data through your code.  The data starts in the database and then is output to HTML by your PHP.  Some of that is in the form of links.  Clicking on a link asks for the next page but also sends the CD reference to the new page.  That data is extracted into a variable and used to change the SQL query to get the correct data.  This may be the most important thing you can learn about using PHP and SQL together.