Displaying results as hyperlinks to an additional query?
I've been looking for a way to display MySQL results as hyperlinks that will perform another query when clicked.
Say I have a 2 tables in my database linked by ALTER TABLE topics ADD FOREIGN KEY(topic_cat) REFERENCES categories(cat_id) ON DELETE CASCADE ON UPDATE CASCADE;
categories:
+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| cat_id | int(8) | NO | PRI | NULL | auto_increment |
| cat_name | varchar(255) | NO | UNI | NULL | |
| cat_description | varchar(255) | NO | | NULL | |
+-----------------+--------------+------+-----+---------+----------------+
topics:
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| topic_id | int(8) | NO | PRI | NULL | auto_increment |
| topic_subject | varchar(255) | NO | | NULL | |
| topic_date | datetime | NO | | NULL | |
| topic_cat | int(8) | NO | MUL | NULL | |
| topic_by | int(8) | NO | MUL | NULL | |
+---------------+--------------+------+-----+---------+----------------+
I also have a PHP search form that queries the table "categories":
$var = @$_GET['search'] ;开发者_Go百科
$trimmed = trim($var);
$query = "select * from categories where cat_name like \"%$trimmed%\"
order by cat_name";
I want to be able to display the results of the above query as hyperlinks and when clicked, I want to display results that are linked to "cat_name" by "topic_cat" and "cat_id". I can't seem to find an example of this anywhere online. Any suggestions?
When you output results, do
print ("<a href='show_category_topics.php?id=".$row["cat_id"]."'>".$row["cat_name"]."</a>");
On a new page (show_category_topics.php
) run the query
$query = "SELECT * FROM topics WHERE topic_cat ='".mysql_real_escape_string($_GET["id"])."'";
and print results.
You can also use ajax calls to php pages.
精彩评论