PHP Hyperlinks

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Thomas-T
Forum Newbie
Posts: 7
Joined: Thu Jul 24, 2008 11:32 am

PHP Hyperlinks

Post by Thomas-T »

Hi!

I'm in the early stages of creating a website for product reviews. I've set up an SQL database in PHPMyAdmin and have successfully managed to select the data and display it in the browser. Here is the code:

Code: Select all

<?php
$con = mysql_connect("host","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("database", $con);
 
$result = mysql_query("SELECT Row FROM Table");
 
while($row = mysql_fetch_array($result))
  {
  echo $row['Row'];
  echo "<br /><hr />";
  }
 
mysql_close($con);
?>
 
Now, I'm having a bit of trouble trying to figure out how to turn the query results into hyperlinks that each link to a different page. Is this something that I need to do in PHPMyAdmin or do I need to make some changes to the code? If someone could point me in the right direction, I'd really appreciate it. Please keep in mind that I'm new to PHP and SQL.

Thanks!
User avatar
ghurtado
Forum Contributor
Posts: 334
Joined: Wed Jul 23, 2008 12:19 pm

Re: PHP Hyperlinks

Post by ghurtado »

Instead of

Code: Select all

 
  echo $row['Row'];
 
try

Code: Select all

 
echo "<a href=\"review_detail.php?id=" . $row['row_id'] . "\">";
 
Thomas-T
Forum Newbie
Posts: 7
Joined: Thu Jul 24, 2008 11:32 am

Re: PHP Hyperlinks

Post by Thomas-T »

I've done that, and that's all good. I've also created a PHP document called 'reviews.php' which will be used as the template on which to display the different reviews. However, I honestly don't know what I'm supposed to code in this document. How do I code it so that the data pulled out of the database is determined by what the visitor just clicked before being directed to this page. In the previous page I managed to list each of the products, and by using this code:

Code: Select all

echo "<a href=\"review.php?id=" . $row['product_id'] . "\">";
I was able to generate a hyperlink for each query result. So if the visitor selects 'Product Number 1' which in the database is given an ID of '1', they will be taken to http://www.---.com/review.php?id=1

At the moment, this page is empty, because I don't know what I'm supposed to code to pull the correct data from the database, as it's not always going to be the same. Does that make sense? Could someone please explain to me what I need to do to get this to work properly. Remember, I'm completely new to PHP. I started learning last night. Thanks in advance.
User avatar
ghurtado
Forum Contributor
Posts: 334
Joined: Wed Jul 23, 2008 12:19 pm

Re: PHP Hyperlinks

Post by ghurtado »

Makes total sense. It is one of the most common ways to get started in PHP: you have a "master" page with links to a "detail" page and you want to be able to select data based on what link was clicked on the master page.

You already have the product_id in the URL, now you simply have to read it in your target (detail) page, like this:

Code: Select all

 
$product_id = $_GET['product_id'];
$sql = "SELECT * from reviews where product_id = $product_id";
// now execute the query and display the results on the page
 
Now the data that the detail page displays is fully dependent on the link that was clicked on the first page.

Does that make sense?
Thomas-T
Forum Newbie
Posts: 7
Joined: Thu Jul 24, 2008 11:32 am

Re: PHP Hyperlinks

Post by Thomas-T »

100%

Thanks very much! :)
Post Reply