How to generate ids for tables made by php

I am trying to create a page to allow toggling on and off certain forums in website, created from raw data in an SQL server. However I need each to have an individual ID so I can show/hide them based on user preference. I am not sure how to go about it. Here is my existing code, disregard the connection values, I am hiding them on purpose. thanks.

$db_host = "host";
$db_username = "username";
$db_pass = "pass";
$db_name = "name";
$db = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$query = $db->query('SELECT p.name, p.company, o.prodtype AS Type
FROM ownedproducts AS o
JOIN product as p ON p.productID = o.productID
WHERE o.usersID = 2');
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<title> User Forum Selection </title>
</head>
<body>
    <div>
<input type="text" id="userid">
    </div>
    <div>
<hr>
<table id=table border = '2'>
<tr id=table-row>
<th id=table-header>Name</th>
<th id=table-header>Company</th>
<th id=table-header>Type</th>
</tr>
    
        
<?php
while ($row = $query->fetch()) 
{
    echo "<tr id=table-row >";
    echo "<td>" . $row['name'] ."</td>";
    echo "<td>" . $row['company'] . "</td>";
    echo "<td>" . $row['Type'] . "</td>";
    echo "</tr>";
}
?>

</table>
</div>
</body>
</html>

1 thought on “How to generate ids for tables made by php”

  1. What about using the product id ? That sounds logical here.

    Select it :

    $query = $db->query('SELECT p.productID, p.name, p.company, o.prodtype AS Type ...
    

    Then use it in your rows :

    while ($row = $query->fetch()) 
    {
        
        echo "<tr id='table-row-" . $row['id'] . "' >";
        echo "<td>" . $row['name'] ."</td>";
        echo "<td>" . $row['company'] . "</td>";
        echo "<td>" . $row['Type'] . "</td>";
        echo "</tr>";
    }
    
    Reply

Leave a Comment