php - Add link to echo'd HTML from SQL data -
back quick question. have code below echo's out product names database. want make echoed out product names link page called product.php, each link needs have unique id, example
<a href="product.php?id=1">product name</a>
how go doing this? many thanks. point out new php.
<?php //create ado connection , open database $conn = new com("adodb.connection"); $conn->open("provider=microsoft.jet.oledb.4.0;data source=c:\webdata\northwind.mdb"); //execute sql statement , return recordset $rs = $conn->execute("select product_name products"); $num_columns = $rs->fields->count(); echo "<table border='1'>"; echo "<tr><th>name</th></tr>"; while (!$rs->eof) //looping through recordset (until end of file) { echo "<tr>"; ($i=0; $i < $num_columns; $i++) { echo "<td>" . $rs->fields($i)->value . "</td>"; } echo "</tr>"; $rs->movenext(); } echo "</table>"; //close recordset , database connection $rs->close(); $rs = null; $conn->close(); $conn = null; ?>
assuming products table has unique id field called "id", change select to:
$rs = $conn->execute("select id, product_name products");
and when want create link, use field , pass url. you'd have product.php?id=<?= $thatidfield; ?>
.
example code:
echo "<table border='1'>"; echo "<tr><th>name</th></tr>"; while (!$rs->eof) //looping through recordset (until end of file) { echo "<tr>"; ($i=0; $i < $num_columns; $i++) { echo "<td><a href=\"product.php?id=" . $rs->fields('id').value . "\">" . $rs->fields($i)->value . "</a></td>"; } echo "</tr>"; $rs->movenext(); } echo "</table>";
Comments
Post a Comment