3
I’m trying to put together a list of links that point to a template, where one of the variables, with the contents of one of the table fields, should be informed dynamically in the template (being associated with a specific table row).
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 take 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 saves this data in the database, in a field text long
, and so far so good...
On the template page the code is 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 he 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 wanted to generate a list of links, and generate a template to bring the contents of each specific row of the table, with the field associated with the table row that generated the link. (ah, hopefully 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 row to be picked up (via URL?) on the template page, but I don’t know if it’s ideal, I don’t know how to do it, I’m very confused around here... :) :/
I didn’t quite understand, in
template.php
you want to receive/access the variable$dados
?– rray
Yes, but associated with the correct row in the table. I think it got confused because I took the
while
on the template page,$dados['campo']
made no sense... Is that on the template page, I put with thewhile
thuswhile ($dados = $query->fetch_assoc()) {
 echo utf8_encode($dados['campo']);
 }
It returns all the results, one below the other. But I need these links (on another page) point each one to a line... so I took the while, but I think I ended up confusing more than explaining... I’ll edit... thanks.– gustavox
@rray I edited, see if you can understand... is that I record these entries from a form, and I want to put the contents of one of the fields (an HTML code snippet) of the table in a template... for each time user answers the form generates a table row, then these links are assembled with each line, and the template needs to pick up the content of the specific line... Actually it must be something simple, I’m the one who’s crashing to explain :) :/
– gustavox
I edited some more including how I write this variable and include this field in the table... @rray
– gustavox