How to report a variable, associated with a row in a table field, for a template

3

I am trying to build a list of links that point to a template, where one of the variables, with the contents of one of the fields in the table, must be dynamically informed in the template (being associated with a specific row in the table) p>

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;
?>

I have SQL that records this data in the database, in a text long field, and so far everything is fine ...

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 table field 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 specific row of the table, with the field associated with the row of the table that generated the link. (ah, I hope it's not getting any more confusing: P) ...

I thought it would be the case to create a for or foreach to generate this variable dynamically, and inform the table line to be taken (via URL?) in the template page, but I do not know if it is the ideal , I do not know how to do this, I'm really confused here ... :): /

    
asked by anonymous 17.09.2015 / 20:43

1 answer

2

I do not 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 DB with auto increment;

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

2 - I changed 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 autoincrement field number through the URL, like this:

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

6 - Then I get the field:

$idAutoInc = $_GET['idautoinc'];

7 - And now I can query the specific field, like this:

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

8 - And so each link takes the id of the correct field:

    $query = $mysqli->query($sql);
    while ($dados = $query->fetch_assoc()) {
        echo  utf8_encode($dados['campo']);
    }
    
18.09.2015 / 02:59