Johny tota
Johny tota

Reputation: 55

select first row next second, etc from query result

I'm building a quizz game with HTML ajax and PHP.

I's searching for something like pagination and show first the first question, next the second, third, etc

What is the best way to select the first row from query result and save the answered question, next the secund question and so on? Should I use json obect to manipulate the query result?

My ajax look like this:

jQuery(document).ready(function(){

      jQuery('.norma').on('click', function(){
                        var dados = jQuery( this ).serialize();
                        jQuery.ajax({
              type: "POST",
              url: "quizz_gen.php",
                                dataType:"text",
              data: { norma: $(this).attr("name") },
              success: function( response )
              {
                                   $("#resp").html( response );
                                }
                            });
          return false;

}); });

This is my php:

 $sql="SELECT questao_id, titulo_pergunta, abordagem, opcao1, opcao2,
 opcao3, opcao4, nome_tema, nome_assunto FROM quizz ORDER BY RAND( )
 LIMIT 10";

 $query = mysql_query($sql);  while ($row = mysql_fetch_array($query))
 {
     echo utf8_encode($row['questao_id']) . "<br>";    
     echo utf8_encode( $row['nome_tema']). "<br>";    
     echo utf8_encode($row['nome_assunto']). "<br>";    
     echo  utf8_encode($row['titulo_pergunta']). "<br>";    
     echo  utf8_encode($row['opcao1']). "<br>";    
     echo  utf8_encode($row['opcao2']). "<br>";    
     echo  utf8_encode($row['opcao3']). "<br>";    
     echo  utf8_encode($row['opcao4']). "<br><br><br><br>";    
}

Upvotes: 0

Views: 241

Answers (2)

Ajitha Ms
Ajitha Ms

Reputation: 555

You can try this

SELECT * FROM table_name LIMIT $page*$number_of_rows,$number_of_rows

In this above case, you should page current page number and number of rows to be shown as inputs. How it is works, LIMIT 0,10 means select first 10 rows. And if you want to see next page, you should pass $page=1 and $number_of-rows=10. So it apply like LIMIT 1*10,10.. etc.. Thats it..

Upvotes: 1

Ritesh Chandora
Ritesh Chandora

Reputation: 8640

form MySql Documentation LINK

The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants (except when using prepared statements).

With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):

SELECT * FROM tbl LIMIT 1,3;  # Retrieve rows 1-3

Now just add your order by Clause and retrieve rows which ever you want.

During Ajax call mention your current Question Id. and in PHP get the next Question

Upvotes: 1

Related Questions