Hanimarkab
Hanimarkab

Reputation: 21

Make HTML row clickable then show that rows users profile from database, for a table that was created with a while loop

I created an HTML table of users using a while loop to get the tables data from the database (dynamic table), but I want to make the rows or at least the first cell of the row clickable, and when clicked it opens another page that displays information about the user from the clicked row kinda like a user profile. the page that displays the information is also dynamic and retrieves data from the database depending on which row was clicked. this my table code


<table class="container">
    <thead>
        <tr>
            <th><h1>Name</h1></th>
            <th><h1>Code</h1></th>
            <th><h1>Votes Today</h1></th>
            <th><h1>Total Votes</h1></th>
        </tr>
    </thead>
    <?php
$conn=mysqli_connect("localhost","root","","voting");
    if(!$conn)
    die("connection error");
$sql = "SELECT `name`,`code`,`votetoday`,`total` from contestent";
$result = $conn-> query($sql);
if ($result-> num_rows > 0) {
    while ($row = $result-> fetch_assoc())
    {
    echo "<tr><td>" . $row["name"] . "</td><td>" . $row["code"] . "</td><td>" . $row["votetoday"] . "</td><td>".
     $row["total"] . "</td><tr>";
}
echo "</table>";
}
?>
    
</table>

and this is a part of the code for the page that the link should take to. to show the user's information

<?php
$sql1 = mysqli_query($result, "SELECT name, code, Age from contestent,images where 
contestent.code = images.cont_id and images.cont_id=3"); 
// the number three in cont_id=3 shoud be a variable with a value depending on which row was clicked from the table.
while($rows1 = mysqli_fetch_array($sql1))
{
$contName = $rows1 ['name'];
$contAge = $rows1 ['Age'];
$contCode = $rows1 ['code'];
echo "<h3> Name: ". $contName."<br>";
echo "Age: ".$contAge."<br>";
echo "Code: ".$contCode;
}
?>

Upvotes: 2

Views: 123

Answers (1)

Optimus Prime
Optimus Prime

Reputation: 308

You can use the GET method for this issue.

The GET method sends the encoded user information appended to the page request. The page and the encoded information are separated by the ? character.

http://www.test.com/profile.php?name=Adam&code=25

In the profile.php page you can get the values,

<?php
   if( $_GET["name"] || $_GET["code"] ) {
      echo "Welcome ". $_GET['name']. "<br />";
      echo "Your Code ". $_GET['code'];
      
      exit();
   }
?>

So the HTML code segment will be following,

<table class="container">
    <thead>
        <tr>
            <th><h1>Name</h1></th>
            <th><h1>Code</h1></th>
            <th><h1>Votes Today</h1></th>
            <th><h1>Total Votes</h1></th>
        </tr>
    </thead>
    <?php
$conn=mysqli_connect("localhost","root","","voting");
    if(!$conn)
    die("connection error");
$sql = "SELECT `name`,`code`,`votetoday`,`total` from contestent";
$result = $conn-> query($sql);
if ($result-> num_rows > 0) {
    while ($row = $result-> fetch_assoc())
    {
    echo "<tr><td>" . $row["name"] . "</td><td>" . $row["code"] . "</td><td>" . $row["votetoday"] . "</td><td>".$row["total"] . "</td>
<td><a href='profile.php?name=".$row["name"]."&code=".$row["code"]."'>View Profile</a></td><tr>";
}
echo "</table>";
}
?>
</table>

You can add more GET variable as you want.

Upvotes: 1

Related Questions