Reputation: 337
I would like to display SQL results, each line with html link, where each link contains the table id.
My table name : fruits
table_id content description price
---------------------------------------------------
1 apple tastes good 3 usd
2 peach grows on tree 4 usd
3 plump very purple 1 usd
It should display
Fruits
apple | tastes good
peach | grows on tree
plump | very purple
(Note: I haven't fetched price at this point)
My code so far:
$result = mysql_query("SELECT table_id,content,decription FROM fruits",$conn);
echo table
while ($row = mysql_fetch_row($res)) {
echo "<tr>";
echo "<td>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "</tr>";
}
echo "</table>";
What I want to achieve instead of echo :
a href="link to fetch id 1">apple | tastes good
a href="link to fetch id 2">peach | grows on tree
a href="link to fetch id 3">plump | very purple
When i mean "link to fetch id 1", i mean like href="www.application.com/another.php and some way to pass fetch id 1 or 2 or 3 to another php file.
How can i make these links ?
How can I catch the passed id in another php file ?
Thank you for your help.
Upvotes: 0
Views: 1015
Reputation: 96159
Something like
while ( false!=($row=mysql_fetch_assoc($res)) ) {
echo '
<tr>
<td><a href="details.php?id=', urlencode($row['table_id']), '">',
htmlspecialchars($row['content']),
'</td>
<td>', htmlspecialchars($row['decription']), '</td>
</tr>
';
}
you'd fetch the table_id in details.php from $_GET['id']
. Test its existence first via isset()
For making the entire row "clickable" see Link entire table row?
Btw: the mysql_* extension is deprecated. Pick another API: mysqli or PDO_mysql.
see http://docs.php.net/manual/en/mysqlinfo.api.choosing.php
And while you're at it and since you most likely are going to use _GET['id'] in another query, take a look at prepared statements and (named) parameters.
Upvotes: 1