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
Link The Results of a Query - Run Through
Moderator: General Moderators
- undecided name 01
- Forum Newbie
- Posts: 12
- Joined: Mon Jul 02, 2007 9:25 am
- Contact:
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:
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.
Hope this helps.
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']
);
}<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);