views:

86

answers:

2

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.

+3  A: 

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"&gt;&lt;/script&gt; 
<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
From.ME.to.YOU
You should probably mention that your JavaScript requires jQuery.
Rupert
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!
Nightmare_IntMain
@Nightmare_IntMain i edited the question .. check it again
From.ME.to.YOU
A: 

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.

threendib