Doe Doe - 3 months ago 7
HTML Question

Generating links dinamically in php mysql

I'm creating a php and mysql program which outputs dinamically a table with data from my mysql database. As you can see in my code below the output is a table with 2 columns where each element of one of them is a link.When the user clicks this link a page with info about that product appears.As I have a really big table with many rows it is very difficult to write the link of every page. I want to ask if there is way to generate the link for each product the user will click. So I want a dynamic link to be generated instead of this "#" which I have inserted instead of a link.
Here is my code which generates the table with rows which are clickable from the user.



<?php $sql="SELECT * FROM presta_prova " ; $result=mysqli_query($con,$sql); while($row=mysqli_fetch_array($result))
{
echo "<tr>";
echo '<td class="tab1">
<a href="#"><b>' . $row[ "marca"] . '</b></a></td>'; echo '<td class="tab2">' . $row[
"descrizione"] . "</td>"; echo "</tr>";
}
echo "</table>"; ?>





Thanks!

Answer

You have to create a dynamic link by any unique identifier like as your ID

    <?php 
    $sql="SELECT * FROM presta_prova"];
    $result=mysqli_query($con,$sql)
    while($row=mysqli_fetch_array($result))
    { ?>
    <tr>
     <td class="tab1">
      <a href="YOUR_URL/product_detail?id=<?php echo $row[ "id"]; ?>">
       <b><?php echo $row[ "marca"]; ?></b>
      </a>
    </td>
    <td class="tab2"><?php echo $row["descrizione"]; ?>
    </td>
    </tr>
    <?php } ?> 
    </table>

Now you can get a product detail by getting id like $id = $_GET['id'] in product_detail page and query it by ID

 $sql="SELECT * FROM presta_prova WHERE id = ".$id ;