Click here to Skip to main content
14,391,608 members
Rate this:
Please Sign up or sign in to vote.
See more:
AJAX/PHP related question, is there a way to list 5000 records at a time and than APPEND next 5000 records in HTML Table and so on until end of records using ajax - php



Load Script on click of a HTML Button: OR document.ready() and also show progress bar.

What I have tried:

// counter that allows you to get a new set of rows
var step = 0;
// set variable if you want to restrict the number of rows will be loaded
var maxStep = 0;//
// how many rows should be returned
var count = 5000;
// if the cancel button is pressed
var cancel = false;

$(function() {

    $('#load').click(function(){

        getData();
    })

    $('#cancel').click(function(){
        cancel = true;
    })
});

function getData()
{
    step++;

    //If cancel variable is set to true stop new calls
    if(cancel == true) return;
    // checks if the variable is set and limits how many rows to be fetched
    if(maxStep >0 and step >= maxStep) return;


    $.post('ajax.php'
        ,{
            'step':step,
            'count':count,
        }
        ,function(data, textStatus, jqXHR){   

		if(textStatus == "success")
                 foreach ($data as $row) {
          echo $row['username']." -- ID :" .$row['user_id']. " -- FirstName :" .$row['first_name']. "<br>\n";
		}
		if(textStatus == "error")
      alert("Error: " + jqXHR.status + ": " + jqXHR.statusText);
		       
	
             // when it finishes processing the data, call back function
             getData();

        }
        ,'json'
    )       
}

==== ajax.php  =====


step = 0;
if(isset($_POST['step'])) $step = (int)$_POST['step'];

$count = 0;
if(isset($_POST['count'])) $count = (int)$_POST['count'];



if($step>0 and $count>0)
{
    $offset = ($step-1) * $count;        
    $limit = $offset.','.$count;

    // --------------        
    // your code here
    // --------------
  
  $data = $DB->query("SELECT * FROM user_details LIMIT .$limit")->fetchAll();
    $result = mysql_query($sql);
    $arr_result = array();
  foreach ($data as $row) {
           $arr_result[] = $row;
        }
   
    $arr_result_enc = json_encode($arr_result);
    echo $arr_result_enc;

    // echo rows
    //echo json_encode($rows);        
}
Posted
Updated 18-Dec-18 5:27am
v3
Comments
MadMyche 18-Dec-18 9:31am
   
Please use the Improve Question widget and wrap your blocks of code into code blocks, makes it a lot easier to read. Also move your "comments" into the block as well
Member 14093672 18-Dec-18 15:47pm
   
Done
Richard Deeming 18-Dec-18 11:38am
   
Seriously? Who is actually going to scroll through 5000 records at a time trying to find the one they want? Let alone an infinite scroller with a page size of 5000.

If a user can check one record per second, it would take them nearly 90 minutes to scan through a single page of results. And that's assuming they don't need to leave for a "comfort break", or even blink.

Give your users the tools to find the information they want without having to scroll through such a massive list. Either let them search or filter the data, or show them a summary.
Member 14093672 18-Dec-18 15:48pm
   
Seriously, thats a kind of requirement, I may consider loading 1000 records at a time, @Richard do you know what i am doing wrong in my script. could suggest changes to my script please?

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

What you are looking for is called infinite scroll, and is easily searchable. Try comparing PHP samples to your code and see how they line up.
   
Comments
Member 14093672 18-Dec-18 10:29am
   
I was having issues to update, now I guess it looks acceptable. I am new to this site, hope my formatting will improve gradually as mt coding knowledge. Any help is highly appreciated.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100