Links in php tables

Asked

Viewed 104 times

0

I’m making a site for a futsal club and created a table with the names of the players in php that will fetch information from a database and wanted to, if possible, make links through the names to pages with more detailed information about the players. This is code for creating 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();
    ?>

Esta é a tabela onde quero fazer as hiperligações

  • Players have an id on the BD table?

  • In the BD table players only have name, date of birth and nationality

  • 1

    What makes a player unique in the table? At least you should include a field id at auto-increment on the table, so that each player has a unique id. Because if two players have the same name/date of birth/nationality, which one do you want?

  • After including the id field, how should php code look like?

  • I’m responding to that. It’s a bit big the answer. Do that and then come that you should be. Leave in auto-increment the id column

  • And by the way, you can include the code of your connection (no passwords) just to see how you’re structuring it

  • Amigo @Miguelsousa put the codes only in your question, leave code in the comments may confuse someone , Miguel be responding so I suggest you wait for the suggestions and then ask questions, it may be that the answer really takes a little , but wait a little.

  • I answered below, I hope it fits

  • (I’m sorry if I’m being ignorant) I already put the above codes but then I get the following error: Notice: Undefined index: id in C: wamp www Pap juvenis.php on line 75. Am I doing something wrong? @Miguel

Show 4 more comments

1 answer

0


Taking advantage of the code that you have, which I believe works, we will 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 player page.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 do GET to the id parameter in the URL, which belongs to the player you clicked on to see the details. For example: player.php? id=1, in this case we will fetch the details from the BD (on the player page.php) the player whose id is 1. Also note that where there is die() can put whatever you want, it is only to show you the errors that can occur since these are inputs of the user never trust 100%. But in principle this works perfectly.

  • Please do not use comments for long discussions. I moved everyone to chat here. Next time, please create a chat room if you need to prolong on any subject. Hug!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.