php – How to enter a variable, associated with a row in a table field, to a template

Question:

I'm trying to assemble a list of links that point to a template, where one of the variables, with the content of one of the fields in the table, must be dynamically informed in the template (being associated with a specific row in the table).

The code that generates the list:

require_once("conexao.php"); 

$sql = "SELECT `username`, `userid` FROM `banco` WHERE username='$user_name'"; // aqui ele pega o username do usuário logado pra gerar a lista buscando no BD

$query = $mysqli->query($sql);
while ($dados = $query->fetch_assoc()) { //alguns dados
    echo '<div>';
    echo '<br>Nome: ' . $dados['username'] . ' ';
    echo 'ID: ' . $dados['userid'] . ' ' <br><br>';

// e aqui é pra gerar a lista de links pra essa template
    echo " 

    <a href='template.php'>Ir para a template e informar qual linha deve ser pega na variável  </a>

    ";
    echo '</div>';
}

The field I want to get is generated like this:

// BASE
ob_start();
include "../saida.php";
$output = ob_get_clean();
file_put_contents('filename', $output);
echo $output;
?>

Then I have the SQL that records this data in the database, in a text long field, and so far so good…

On the template page the code looks like this:

require_once("conexao.php"); 
    $sql = "SELECT `campo` FROM `banco` WHERE username='$user_name'";
    $query = $mysqli->query($sql);
    while ($dados = $query->fetch_assoc()) {
                            echo  utf8_encode($dados['campo']);
                        }

So it takes all the results, and returns all the contents of this field from the table associated with the logged in user, and writes one result below the other… now I wanted to generate a list of links, and generate a template to bring the content of each line table-specific, with the field associated with the row of the table that generated the link. (Oh, hopefully it's not getting even more confused :P)…

I thought it would be the case to create a for or foreach to generate this variable dynamically, and inform the table row to be taken (via URL?) in the template page, but I don't know if it's ideal, I don't know how to do this, I'm pretty confused around here… 🙂 :/

Answer:

I don't know if it's the safest way, because I'm passing a parameter through the URL, but I managed to solve it like this:

1 – I created a field in the BD with auto increment;

ALTER TABLE  tabela ADD  `idautoinc` INT NOT NULL AUTO_INCREMENT ,
ADD PRIMARY KEY (  `idautoinc` ) ;

2 – I changed the SELECT to include this field:

$sql = "SELECT `diagravacao`, `idautoinc` FROM `tabela` WHERE username='$user_name' ORDER BY `diagravacao` DESC";

3 – I changed the while to:

$query = $mysqli->query($sql);
while ($dados = $query->fetch_assoc()) {
    echo '<div id="resultsbd">';
    echo '<br><b>Data:</b> ' . $dados['diagravacao'] . ' &nbsp &nbsp';
    echo '<b>Id do campo com autoincremento:</b>' . $dados['idautoinc'] . '<br><br>'; 
}

4 – I created a variable to get this id:

 $idautoinc = $dados['idautoinc'];

5 – And now I pass the auto-increment field number by the URL, like this:

<a href='template.php?idautoinc=$idautoinc'></a>

6 – So I take the field:

$idAutoInc = $_GET['idautoinc'];

7 – And now I can consult the specific field, like this:

$sql = "SELECT `campo` FROM `tabela` WHERE username='$user_name' AND idcalc='$idautoinc'";

8 – And so each link gets the id of the correct field:

    $query = $mysqli->query($sql);
    while ($dados = $query->fetch_assoc()) {
        echo  utf8_encode($dados['campo']);
    }
Scroll to Top