There is such an ajax handler, each time the $num variable is decremented by 12:

  • Get: 32/12 rows
  • Get: 20/12 rows
  • Get: 8/12 rows
  • Get: 0/12 rows

As can be seen from the last row of the record that will be output this Get: 8/12 rows if subtract 8-12=-4 then the records will no longer be displayed. But we still have 8 entries left. How can I get them out? How to do the processing? It should be like Get: 0/8 rows

 $mesgs = $db->QueryFetchArrayAll("SELECT a.id, a.aut, a.mesg, a.timestamp, b.name, b.email FROM txt_mesgs a LEFT JOIN users b ON b.id = a.aut WHERE a.tid = '".$id."' ORDER BY a.timestamp ASC LIMIT 0, ".$num.""); 

And also if in the table for example 15 records, then you need to output the rest (3) Get: 0/3 rows

 <?php if (isset($_POST['num']) && isset($_POST['tid']) && is_numeric($_POST['num']) && is_numeric($_POST['tid'])) { $num = $db->EscapeString($_POST['num']); $id = $db->EscapeString($_POST['tid']); if ($num > 0) { $mesgs = $db->QueryFetchArrayAll("SELECT a.id, a.aut, a.mesg, a.timestamp, b.name, b.email FROM txt_mesgs a LEFT JOIN users b ON b.id = a.aut WHERE a.tid = '".$id."' ORDER BY a.timestamp ASC LIMIT ".$num.", 12"); foreach($mesgs as $mesg){ ?> <div id="<?=$mesg['id']?>" class="ticket_mmesg"> <img class="ticket_mavatar" src="http://www.gravatar.com/avatar/<?=md5(strtolower(trim($mesg['email'])))?>?s=45" alt="<?=ucfirst($mesg['name'])?>" /> <div class="ticket_mtime-name"><div class="ticket_musername"><?=ucfirst($mesg['name'])?></div><div class="ticket_mtime"><?=date('H:i', $mesg['timestamp'])?></div></div> <div class="ticket_mdelete"></div> <div class="ticket_mtext"><?=$mesg['mesg']?></div> </div> <?php } } else { $db->Close(); echo 0; } }else{ echo 'Error'; } ?> 

-

 $(document).ready(function () { var check_messages = setInterval(function () { var scrollPosition = $('#ticket_mscroll').scrollTop(); if (scrollPosition == 0) { load_tickets(); } }, 1000); var num = 32; //Всего у нас 56 сообщений, 56-24 =32 (На экране 12 сообщений уже выведено) function load_tickets() { var tid = 1; $.ajax({ url : "get.php", type : "POST", data : { "num" : num, "tid" : tid, }, cache : false, success : function (las_messages) { if (las_messages == 0) { // смотрим ответ от сервера и выполняем соответствующее действие //$(".ticket_mhead").append("<div> Error </div>"); clearInterval(check_messages); } else { $("#doload").prepend(las_messages+' '+num+'/12'); num = num - 12; } } }); } }); 
  • To be honest, I didn’t understand at all how the first part of your question is related to what is written after the first code with the SQL query, because there is nothing about either ajax or reducing num , but only sampling and rendering 12 records from tables starting at $num . So it is better for you to include your ajax part in the question. - teran
  • Greetings, added Ajax. - Andrei Andrei

1 answer 1

Expand order by in your sql query:

 $mesgs = $db->QueryFetchArrayAll("SELECT .. ORDER BY a.timestamp DESC ..."); 

Those. DESC instead of ASC

So you will need to take entries in a more standard version. In your example of 15 entries, the last one will be:

 $mesgs = $db->QueryFetchArrayAll("SELECT .. ORDER BY a.timestamp DESC LIMIT $num 12"); //$num=12 
  • LIMIT 0 12 -> 0 - 12
  • LIMIT 12 12 -> 12 - 15

The database itself will display the last 3 records. No further action is required.