Count mysql records in X Y format to display in Wizard style form

Asked

Viewed 31 times

0

Hello, I have a problem to count the records of a table in the database and show it as in this example: 1(X) of 10(Y) when clicking on "next" of the form would be 2(X) of 10(Y) and so on, as the form progresses it increases +1 in the value of X.

To list all questions registered in the database use the following code:

<?php
        include_once "../../conexao.php";
        $sql_questionario = "SELECT * FROM `questionario_perguntas` ORDER BY RAND()";
        $query_questionario = mysqli_query($conn, $sql_questionario);

        while ($dados_questionario = mysqli_fetch_assoc($query_questionario)) {
            ?>
                <fieldset>

                    <div class="divider-text gap-top-20 gap-bottom-45">
                        <span>Questão X de Y</span>
                    </div>

                    <!-- start way to communicate -->
                    <div class="unit check" id="">
                        <div class="group">
                            <h2><?php echo "$dados_questionario[pergunta]";?></h2>

                        </div>
                    </div>


                </fieldset>

Someone to help me? Hug.

  • Your case is the same as a pagination, with the peculiarity of being 1 item per page.

  • Please put the link here of the answer that serves for my case. Poi was very vague this justification. Thank you

  • The link is on the yellow board above the question

1 answer

2


Try this way:

...
$query_questionario = mysqli_query($conn, $sql_questionario);
$total = $query_questionario->num_rows;
$question = 0;

        while ($dados_questionario = mysqli_fetch_assoc($query_questionario)) {
            ?>
                <fieldset>

                    <div class="divider-text gap-top-20 gap-bottom-45">
                        <span>Questão <?= ++question ?> de <?= $total ?></span>
...

You can use the function mysqli_num_rows to get the amount of results obtained and save to a variable to keep displaying. You can see more about this function in the documentation.

And uses another variable as counter to display the current question number within the loop.

  • 1

    Although the topic was closed, stating that the problem was the same as the link. I make it clear here that this solution informed by @Hiago-magalães was exactly what I needed. Because my form does not need paging because it is STEP it only advances after clicking NEXT and with this it creates its own paging. I thank my friend Thiago for the efficient answer, and for those who seek the same problem I will not need to decipher the code of another person with a problem that is not the same to be able to solve. Thank you very much Thiago, God bless you; Big hug

Browser other questions tagged

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