0

I want to show a certain amount of results (say, 5) and make a:

<a href="" onclick="<?php ShowNextResult(); ?>">

And use onlick to show the next 5 results.

1

4 Answers 4

3

EDIT :: HTML

<div id="results">
   <div class="result"></div>
   <div class="result"></div>
   <div class="result"></div>
</div>
<a href="#" id="showMore" />Show more</a>

JAVASCRIPT Use Jquery as below

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
<script type="text/javascript">
$(function(){
      $('#showMore').click(function(event) {
         event.preventDefault();
         $number = $('.result').size();

        $.ajax({
           type: "POST",
           url: "getNext.php",
           data: "count=$number",
           success: function(results){
             $('#results').append(results);
           }
         });

      });

});
</script>

PHP

you should make a new php page (getNext.php ) that will get query results

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM Persons LIMIT {$_POST['count']},5");

while($row = mysql_fetch_array($result))
  {
  echo "<div class='result'>".$row['FirstName'] . " " . $row['LastName']."</div>";
  }

mysql_close($con);
?>

HELP

you can use SQL something like

SELECT x,xx,xxx FROM XxXxXs Limit $_POST['count'],5
Sign up to request clarification or add additional context in comments.

3 Comments

You should probably mention that your JavaScript requires jQuery.
If you could add the way to get the results to the Function it would be way better but +1 for just a great anwser and thank you!
You really shouldn't be showing sample SQL queries with $_POST variables, as this is a textbook injection risk.
0

Since you specifically mention JavaScript I assume you don't want to reload the page or anything like that. Your onClick will have to trigger an AJAX call to a php page on your server that will handle the request and give you back the next five records (or the last 5, or random ones, etc...).

JQuery is really popular for doing this and have built in functionality to make this process easier.

http://api.jquery.com/jQuery.ajax/

Here are some tutorials: http://docs.jquery.com/Tutorials

Your best bet is to write this functionality w/o using JavaScript. Make the page accept arguments to show specific records. Once you have that code done, then put the AJAX on top of it, but that way you'll have the older stuff to fall back on if you need to for compatibility or things don't work the way you need them to.

These are pretty general answers, do you need specific help making the query to only show the next 5 records? Or the specific PHP code to tie it together? Or just the JS to do the AJAX stuff? Could you be more descriptive if you need more info.

Comments

0

change

data: "count=$number",

to

data: "count=" + $number,

because then it isn't work!

Comments

0

Here's my solution that showing quiz questions partially with next button means on each click at Next Button 5 more question will display.

    <?php
    $strSQL="SELECT * FROM `quizes` WHERE Q1 IS NOT NULL ORDER BY RAND()";
    $result=mysql_query($strSQL);
      while($row=mysql_fetch_array($result)){
    $c=0;   
    $q[]= $row['Q1']; // This is database record that has all question stored as array
    ?>

    <?php   
    for($inc=0; $inc < $ret; $inc++){ ?>
    <table>
        <tr id="<?php echo "i".$inc ?>">
            <td id="qs"> <?php  print_r($q[$inc]); ?></td> 
    </tr></table> 
    <!-- here in i am display all question with loop in this variable $q[$inc] -->

    <?php } ?>

    // Now we are going to display partial 
    instead of all so data will display partially with next button


    <a href="#" id="more">Next/Show More</a> 
    //this is anchor/button on which more questions will load and display when clicked


    //CSS question are placing in tr (table row) so first hide all question

    <style>

    tr{
        display:none
    }

    </style>

    //jquery now we will show partial question 5-questions at each click
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

    <script>
    $(document).ready(function(){
  $("[id=i0],[id=i1],[id=i2],[id=i3],[id=i4],[id=i5]").show();
//Display first 5-question on page load other question will 
//show when use will click on next button

       var i=0;

        $("#more").click(function(){ // Next button click function
    //questions tr id we set in above code is looping like this i1,i2,i3,i4,i5,i6...

    i=i+5; //at each click increment of 5 question

       var e=i+5; 
//start after the last displayed question like if previous result was 1-5 question then next result should be 5-10 questions...
        for(var c=i; c < e; c++  ){

            $("[id=i"+c+"]").show();
          }

        });
        });
    </script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.