Here's a full example:
Caller:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>title</title>
<link rel="stylesheet" href="style.css">
<script src="jquery.js"></script>
</head>
<script>
$(document).ready(function()
{
iOffset = 0;
function callEndpoint( call_url, payload ){
return $.ajax({
url: call_url,
type: 'GET',
data: payload
});
}
function loadRows() {
iLimit = 10;
oRequest = callEndpoint( '/play/endpoint2.php', { 'offset': iOffset, 'limit': iLimit } );
oRequest.done(function( sJson ) {
aRaw = JSON.parse( sJson );
console.log( aRaw );
aData = aRaw.data;
if( jQuery.isEmptyObject( aData ) )
{
console.log( 'empty!! ' );
clearInterval( t );
}
else
{
sNewRows = '';
for ( var i = 0; i < aData.length; ++i ) {
sNewRows += '<tr>';
for ( var prop in aData[ i ] ) {
if( aData.hasOwnProperty( prop ) ){
sNewRows += '<td>' + aData[ i ][ prop ] + '</td>';
}
}
sNewRows += '</tr>';
}
$( '#loading-data tr:last' ).after( sNewRows );
iOffset += 10;
}
});
}
loadRows();
var t = setInterval(function(){
loadRows();
},2000); // Load more rows every 2 seconds.
});
</script>
<body>
<table id="loading-data">
<tbody>
<thead>
<tr>
<th>PK</th>
<th>co</th>
<th>type</th>
<th>num</th>
</tr>
</thead>
</tbody>
</table>
</body>
</html>
Endpoint:
class MySql
{
private $sDbName = 'play';
private $sUsername = 'root';
private $sPassword = '';
private $sHost = 'localhost';
private $oConnection = null;
public function __construct()
{
$this->oConnection = new PDO(
'mysql:host='
. $this->sHost
. ';dbname='
. $this->sDbName,
$this->sUsername,
$this->sPassword
);
}
public function getDb()
{
return $this->oConnection;
}
}
$oMySql = new MySql;
$oDb = $oMySql->getDb();
$aGet = $_GET;
if( !empty( $aGet ) )
{
$iOffset = $aGet[ 'offset' ];
$iLimit = $iOffset + $aGet[ 'limit' ];
}
$sSql = "
SELECT pk, companyId, type, page_nav
FROM
`1`
WHERE
pk >= :custom_offset
AND
pk < :custom_limit
";
$oStmp = $oDb->prepare( $sSql );
$oStmp->bindValue( ':custom_offset', $iOffset );
$oStmp->bindValue( ':custom_limit', $iLimit );
$oStmp->execute();
$aResults = $oStmp->fetchAll();
// var_dump( $aResults );
$oErrors = $oStmp->errorInfo();
// var_dump( $oErrors );
$aReturn[ 'data' ] = $aResults;
$sJson = json_encode( $aReturn, 1 );
header( 'Content-type', 'application/json' );
echo $sJson;