A
A
Alexis_D2018-04-27 07:45:59
MySQL
Alexis_D, 2018-04-27 07:45:59

How to load a search query with ajax?

Good day. I have a database search, and, let's say, there are 60,000 thousand rows in it, I made it so that it first displayed 100 records, when loading the first 100, it outputs normally on request, but when ajax is already being called to load track. 100 records, it starts to issue not on demand.
ajax request

$(document).ready(function(){
var inProgress = false;
/* С какой статьи надо делать выборку */
var startFrom = 100;

  $(window).scroll(function() {
/* Если высота окна + высота прокрутки больше или равны высоте всего документа и ajax-запрос в настоящий момент не выполняется, то запускаем ajax-запрос */
    if($(window).scrollTop() + $(window).height() >= $(document).height() - 200 && !inProgress) {
      $.ajax({
          /* адрес файла-обработчика запроса */
          url: 'search_ajax.php',
          method: 'POST',
          /* данные, которые мы передаем в файл-обработчик */
          data: {"startFrom" : startFrom},
          beforeSend: function() {
          inProgress = true;
          }}).done(function(data){
          /* Преобразуем результат, пришедший от обработчика - преобразуем json-строку обратно в массив */
          data = jQuery.parseJSON(data);
          if (data.length > 0) {
          $.each(data, function(index, data){
          /* Отбираем по идентификатору блок со статьями и дозаполняем его новыми данными */
          $(".result_search_table").append("<tr><td>" + data.name + "</td> <td>" + data.nameScore + "</td> <td>" + data.organization + "</td></tr>");
          });
          inProgress = false;
          startFrom += 100;
          }});
      }
  });
});

ajax request handler
$startFrom = $_POST['startFrom'];
   $res = mysqli_query($connect, "SELECT * FROM `test_excel` WHERE `noSpaceName` LIKE '%".$startFrom."%' OR `nameScore` LIKE '%".$startFrom."%' OR `organization` LIKE '%".$startFrom."%' LIMIT {$startFrom}, 100");

   $resSeacrh = array();
   while ($row = mysqli_fetch_assoc($res)) {
       $resSeacrh[] = $row;
   }
   echo json_encode($resSeacrh);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yan-s, 2018-04-27
@Yan-s

Debug the code, determine the cause, isolate and fix it.
This is exactly your job. To begin with, take out the SQL in a separate variable and observe its value to understand what is wrong.

I
iljaGolubev, 2018-04-27
@iljaGolubev

what are you waiting for here?

LIKE '%".$startFrom."%' ...  LIMIT {$startFrom}, 100
LIKE '%101%' ...  LIMIT 101, 100
-- или
LIKE '%learn sql%' ...  LIMIT learn sql, 100</code

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question