user7440045
user7440045

Reputation:

count number of rows in database

I have some questions in my database. I display only one question per page with a "NEXT" button. When the user clicks on next button I display the next question from the database randomly.

So the problem is when user reached has the last question I want to change the "NEXT" button name to "FINISH".

please help me how to do this..

this is my count query:
$nRows = $dbh->query('select count(*) from questions')->fetchColumn(); 
echo $nRows;

This is my next button in my application.

<button  id='Next'  class="next-button" name="submit_<?php echo $key ?>" value="<?php echo $key ?>" onclick="change_next(this.value)">Next</button>

this is my java script code to display one question per page:

<script type="text/javascript">

         function change_next(value) 
    {
    if(value == 0) 
    {
         var next = value;
         next++;

        $('#question_'+value).removeClass('active').addClass('hidden');
        $('#question_'+next).removeClass('hidden').addClass('active');
    } 
    else 
    {
        var next = value;
        next++;

        $('#question_'+value).removeClass('active').addClass('hidden');
        $('#question_'+next).removeClass('hidden').addClass('active');
    }
}

Upvotes: 0

Views: 859

Answers (3)

Harshal
Harshal

Reputation: 946

var count = 0;
function showNextQuestion() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      count+=1;
      if(count>=3) {
         document.getElementById("Next").innerHTML="Finish";
      }
    }
  };
  xhttp.open("GET", "QuestionLoader", true);
  xhttp.send();
}

Upvotes: 0

krisph
krisph

Reputation: 697

You just need to chagne the value of the button

    var count = 0;
    function change_next() {
      var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
          document.getElementById("Question").innerHTML = this.responseText;
          if (count == 5) {
             //document.getElementById("Next").value="FINISH";  <--sorry this is if you are using <input type="button" not button =/
             document.getElementById("Next").innerHTML="FINISH";
          }
          count++;
        }
      };
      xhttp.open("GET", "Load_Question.php", true);
      xhttp.send();
    }

Upvotes: 2

Ehsan Ilahi
Ehsan Ilahi

Reputation: 298

<button  id='Next'  class="next-button" name="submit_<?php echo $key ?>" value="<?php echo $key ?>" onclick="change_next()">Next</button>

function change_next()
{
    document.getElementById("Next").value="FINISH"; 
}

Upvotes: 0

Related Questions