Count mysql records in Y format to display in wizard-style form [duplicate]

0
Hello, I have a problem with counting the records of a table in the database and show it as in this example: 1 (X) of 10 (Y) when clicking "next" of the form would be 2 (X) of 10 (Y) and so on, as you progress the form increases +1 by X.

To list all the questions registered in the database I 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? Thanks.

    
asked by anonymous 15.08.2018 / 08:26

1 answer

2

Try this:

...
$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 mysqli_num_rows function to get the amount of results you've obtained and save to a variable to display. You can see more about this feature in the documentation .

And use another variable as counter to display the current issue number within the loop.

    
15.08.2018 / 12:37