Links in php tables

0

I am making a website for a futsal club and I created a table with the names of players in php that will fetch information from a database and wanted, if possible, to make links through the names to pages with more detailed information about the players. This is code to create the tables:

<?php 
$servername ="localhost";
$username="root";
$password="";
$dbname="casaldogrilo";
//cria conexão
$conn = new mysqli($servername, $username, $password, $dbname);
// verifica conexão
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 
$sql = "SELECT * FROM juniores order by Nome ASC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table style='width:100%' height='100%'>
<tr>
    <th>Nome</th>
    <th>Data_Nasc</th>
    <th>Idade</th>
    <th>Nacionalidade</th>
    </tr>";
    // output data of each row
    while($row = $result->fetch_assoc()) {
    echo "<tr>
    <td align='center'>".$row["Nome"]."</td>
    <td align='center'>".$row["Data_Nasc"]."</td>
    <td align='center'>".$row["Idade"]."</td>
    <td align='center'>".$row["Nacionalidade"]."</td>
    </tr>";
    }
    echo "</table>";
    } else {
    echo "0 results";
    }
    $conn->close();
    ?>

    
asked by anonymous 17.06.2016 / 13:16

1 answer

0

Taking advantage of the code you have, which I believe works, let's add anchors (href):

if ($result->num_rows > 0) {
echo "<table style='width:100%' height='100%'>
<tr>
<th>Nome</th>
<th>Data_Nasc</th>
<th>Idade</th>
<th>Nacionalidade</th>
</tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
    echo '<tr>
        <td align="center"><a href="jogador.php?id=' .$row["id"]. '">' .$row["Nome"]. '</a></td>
        <td align="center">' .$row["Data_Nasc"]. '</td>
        <td align="center">' .$row["Idade"]. '</td>
        <td align="center">' .$row["Nacionalidade"]. '</td>
    </tr>';
}
echo "</table>";

Then on the page player.php:

<?php
if(!isset($_GET['id'])) {
    die('Não existe um id definido');
}
if(!is_numeric($_GET['id'])) {
    die('ID inválido');
}
else {
    $servername = "localhost"; $username = "root"; $password = ""; $dbname = "casaldogrilo"; // cria conexão
    $conn = new mysqli($servername, $username, $password, $dbname); // verifica conexão
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    $sql = "SELECT * FROM juniores WHERE id=" .$_GET['id'];
    if(!$result = $conn->query($sql)) {
        die ('problema na query');
    }
    if ($result->num_rows > 0) {
        $jogador = mysqli_fetch_assoc($result);
        echo 'Nome: ' .$jogador['Nome']. '<br>';
        echo 'Data Nascimento: ' .$jogador['Data_Nasc']. '<br>';
        echo 'Idade: ' .$jogador['Idade']. '<br>';
        echo 'Nacionalidade: ' .$jogador['Nacionalidade']. '<br>';
    }
    else {
        echo 'Jogador não encontrado';
    }
}

Note that we will GET to the id parameter that is in the URL, which belongs to the player where you clicked to view the details. For example, player.php? Id = 1, in this case we will get the details from the DB (on the page player.php) the player whose id is 1. Note also that where die() exists you can put what you want, it's just to show you the errors that can occur since this is user inputs never trust to 100%. But in principle this works perfectly well.

    
17.06.2016 / 13:46