To enter a variable, associated with a row of a table field, for a template

Asked

Viewed 742 times

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

  • Yes, but associated with the correct row in the table. I think it got confused because I took the whileon the template page, $dados['campo']made no sense... Is that on the template page, I put with the while thus while ($dados = $query->fetch_assoc()) {&#xA; echo utf8_encode($dados['campo']);&#xA; } 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.

  • @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 :) :/

  • I edited some more including how I write this variable and include this field in the table... @rray

1 answer

2


I don’t know if it’s the safest way, because I’m passing a parameter through the URL, but I was able 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 for:

$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 field number autoincremented by the URL, like this:

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

6 - Then 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 takes the id of the right field:

    $query = $mysqli->query($sql);
    while ($dados = $query->fetch_assoc()) {
        echo  utf8_encode($dados['campo']);
    }
  • Cool that you managed to solve :). Not if this applies to the case, swap chaamada of the link for a include then you can enjoy it $dados just need to do a check on $idAutoInc.

  • Do the include of the page that makes the list on the result page? Oh, sure, I drew... is it safer? is faster (because he will have to read the whole page right)? is another question? : ) @rray

  • 1

    A little more for not exposing the id, but advantage is sharing $dados even.

  • Cool, thanks @rray... hug!

Browser other questions tagged

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