How to match a value before it is available?

Asked

Viewed 75 times

0

I have a site where I do an SQL that returns the records and the amount of records I need to show on the site. For some reason that does not come to question, I no longer have the possibility to capture this variable before header.

I have to put the data in parentheses that are at the top of the site.

inserir a descrição da imagem aqui

This result returns to me through a $_POST['enviar'] the result of SQL and the amount of records that are already inserted below the header. How to put the amount of records in this "Results()"?

The code below shows well what I’m trying to do: get the $qteRegistros and pull it up.

<?php

     echo $qteRegistros; <--------------------
     $qteRegistros = "380";

?>
  • 2

    I didn’t understand the doubt, you want to print the variable value before assigning?

  • 2

    The code is executed in the sequence in which it is written. Change the last two place lines.

  • I can’t change places. Since PHP is ultra fantastic, I want to know if it does. Something to do with global????

  • 3

    Why Cause, Reason, Reason or Circumstance Can You Not Change The Place Lines? Oo

  • I do an SQL and return the amount of records and need to show the amount of records there at the top of the site !!!

  • 3

    http://answall.com/help/mcve

  • 3

    @Marcosvinicius Then run SQL before drawing the top of the site.

  • 4

    You’re trying to kill before you shoot... forget it! the only way I see it would be to move the value to the right location using css or javascript...

  • 3

    I’ll help you understand how Sopt works. It’s simple, it’s like at school. It’s not the teacher who gives the grade, it’s the student who gets the grade. The student does what he has to do, if he if help, the teacher recognizes this. Here the user makes the question stay in a state that you can answer, everyone who can will be happy to help. But you simply cannot answer what is not understandable. After all the help already received here, you cannot say that there is bad will. It’s simply still impossible to help anymore. Do yourself a favor, help us help you.

  • 1

    It is reopened, but for a more precise answer you need to include the code snippet that draws what is in the image.

  • 1

    In short, you need to save all the html in a variable, to the point where you want to use the $qteRegistros. Only from there you can start issuing the output. PHP has a buffer that can help with this.

  • 3

    You want to print a variable that doesn’t exist - that’s a logic problem. I think you should give more information, preferably the code you have.

  • I just wanted to capture a variable after it was ready and @Jader gave me exactly the hint I needed to get my problem solved. I did this by capturing the variable after ready with jQuery and playing it where I needed it. Jader, feel free to come up with an answer for me to evaluate.

  • 1

    I removed my -1. Finally the real doubt appeared... Funny that the first version of the question had Surgiu essa dúvida que deve ser super simples., and as it turned out, simple had nothing :P

Show 9 more comments

1 answer

4


Your problem apparently is that you are setting the variables in the wrong place as the example below - you want to use a variable that has not yet been defined.

<html>
    <head>
        <title><?php echo $total_registros; ?></title>
    </head>
    <body>
        <?php
        $PDO = DATABASE...
        $total_registros = $resultado_pdo;
        ?>
    </body>
</html>

The correct thing would be to separate the logic from the view, but I won’t get into that. Just create the variables and use them later outside the HTML elements. So you can define all variables and use them in HTML.

php page.

//Parte lógica do PHP
<?php
$PDO = DATABASE...
$total_registros = $resultado_pdo;
?>

//composição do HTML
<html>
    <head>
        <title><?php echo $total_registros; ?></title>
    </head>
    <body>
        Encontrados <?php echo $total_egistros; ?> registros.
    </body>
</html>

If I misunderstood your problem, give me more information.

  • 3

    This is definitely the best solution. There are others, manipulating the php buffer or by Javascript, but they are both gambiarras.

  • JS only with JS-ON and buffer will be a GAMB that I do not believe will work because it will always be post-echo.

  • @bfavaretto, I feel even discomfort putting HTML and the logic of PHP in the same file :(

  • 2

    One battle at a time :)

  • @Marcos Vinicius, since you have marked the answer, I will indicate that search on MVC as an architecture model for development. You will see that it falls like a glove.

Browser other questions tagged

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