Link The Results of a Query - Run Through

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
Jaxamercy
Forum Newbie
Posts: 6
Joined: Sun Jul 01, 2007 1:31 pm

Link The Results of a Query - Run Through

Post by Jaxamercy »

Hi All

I am new to php/mysql.

I have created a page that lists all records in a mysql table under a field headed "subject". I want to activate each of these records as a hyperlink, that when it is clicked displays the detail of each of these subjects in a new page. I have scoured these pages and have realised I need to hyperlink the result to a new php file. I cannot for the life of me get this to work. I would be really gratefull if :

1) Anybody could please give me a run through on how to correctly link to the new php file, and;
2) Explain how to set up the new php file to display the detailed information in a new page.

Many thanks in advance

Jax
User avatar
undecided name 01
Forum Newbie
Posts: 12
Joined: Mon Jul 02, 2007 9:25 am
Contact:

Post by undecided name 01 »

You're very welcome to php/mysql.

You should have a unique ID for each of your subjects.
For example you may have a table with three fields messages=(id, subject, content), where id is a Primary Key and auto_increment.

The first step is print out hyper-linked subjects:

Code: Select all

$db_result = mysql_query("
  SELECT id, subject
  FROM messages
");

while ($db_row = mysql_fetch_assoc($db_result)) {

  printf("<a href=\"view.php?id=%d\">%s</a><br />",
    $db_row['id'], $db_row['subject']
  );

}
Sample output:
<a href="view.php?id=1">My First Subject</a><br />

As you see, while you're printing the subjects, you're passing each subject's id to view.php.

The second step is to make view.php to view the selected subject.

Code: Select all

$subject_id = (isset($_GET['id'])) ? intval($_GET['id']) : 0;
if ($subject_id < 1) {
  die("Invalid Subject ID.");
}

$db_result = mysql_query("SELECT * FROM messages WHERE id = '" . mysql_real_escape_string($subject_id) . "'");
$db_row = mysql_result($db_result, 0);
print_r($db_row);
Hope this helps.
Jaxamercy
Forum Newbie
Posts: 6
Joined: Sun Jul 01, 2007 1:31 pm

Post by Jaxamercy »

Thank you for your kind reply and welcome.

If I may I would like to ask for some explanation of the information you kindly provided.

Within the code you provided that would display the hyperlinked subjects, what is the significance of the % used?

Sorry if I'm beign thick!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

%d and %s are replacement points used by printf() to indicate datatype, formating, etc.
Jaxamercy
Forum Newbie
Posts: 6
Joined: Sun Jul 01, 2007 1:31 pm

Post by Jaxamercy »

Many thanks I'll read up
Post Reply