使用Ajax从PHP连续加载数据

我是一个搜索引擎,可以从多个网站获取所有DOM元素并将其加载到主页上。问题在于,每次搜索内容时,您都需要等待XX秒才能显示结果。

我想在获得结果后加载结果,并显示每个结果直到完成,以免凝视空白屏幕几秒钟,这怎么可能?

在搜索页面上,我有这个

    function returnResults() {
        searchbooks("books","10")
        //keyword and how many for each website;
      }

    function searchbooks(keyword,howmany) {
    $.ajax({
        type: 'POST',url: 'display.php',data: {whatyouseach: keyword},dataType:"json",success: function(data)
        {   
              showonebyone(data);
        },error: function(data)
        {
          console.error(data);
          console.error("error");
        },});
  }

    function showonebyone(alldata)
  {
    $.each( alldata,function( key,value ) {
      $.each( value,function(nr,data_pt_maga) {
        afiseaza_in_trebuiala(key,data_pt_maga[3],data_pt_maga[1],data_pt_maga[0],data_pt_maga[2],data_pt_maga[4]);
      });
    });
  }

    function afiseaza_in_trebuiala(vz,im,lk,ti,pr,st) {

                  var showthis = "";
                  showthis += '<li>';
                  showthis += '<a href="'+lk+'"><img src="'+im+'"/></a>';
                  showthis += '<h3>'+ti+'</h3><p>'+vz+'</p><p>'+st+'</p>';
                  showthis += '<p>'+pr+' USD</p></li>';
                  $("#results").append(showthis);
  }

ajax函数中的页面

require_once('crawler.php');
if($_POST["cecautamtext"] != "") {
  $results_from_php = start_search($_POST["whatyouseach"]);
  header('Content-type: application/json');
  echo json_encode($results_from_php );
}

还有爬虫页面

$reteta_magazine = array(
    "shop1" => array(
          "getdomfrom" => "https://www.example.org","maindiv" => "div[class='search results']","secdiv" => "li[class='item product product-item']","name" => "a[class='product-item-link']","img" => "img[class='product-image-photo default_image']","price" => "span[class='price']","soc" => "button[class='action tocart primary']","link_prod" => "a[class='product-item-link']",),"shop2" => array(
          "getdomfrom" => "https://www.example2.org",);


function start_search($keyword) {
  global $shops;
  $date_brut_cautare = array();
  foreach ($shops as $single_shop => $elemente) {
    $rez_each_site = seach_each_site($single_shop,$keyword,$elemente["existanta_containter_search"],$elemente["path_element"],$elemente["link"],$elemente["nume_prod"],$elemente["imagine_prod"],$elemente["pret_prod"],$elemente["stoc_prod"],$elemente["link_prod"]);
    $date_brut_cautare[$single_shop] = $rez_each_site;
  }
  if(is_array($date_brut_cautare) || is_object($date_brut_cautare))
  {
    return $date_brut_cautare;
  }
}


function seach_each_site($magazin_singur,$exista_conainter,$pathul_defaul,$link_decautare,$numprod,$imagine,$pret,$stoc,$link_c_produs) {
  $rezultate_onbyone = array();
  $urlac = urlencode($keyword);
  $tempval = array();
    $html = file_get_html($link_decautare.$urlac);

    if($html->find($exista_conainter)){
    $i = 0;

      foreach($html->find($pathul_defaul) as $div) {
    $gasit_pret = "";
    $gasit_link = "";
    $gasit_nume = "";
    $gasit_stoc = "";
    $gasit_img = "";
    $img_temp = "";

      $gasit_pret = $div->find($pret,0)->plaintext;
      $gasit_link = $div->find($link_c_produs,0)->href;
      $gasit_nume = $div->find($numprod,0)->plaintext;
      $gasit_stoc = $div->expect($stoc,0);
      $gasit_img = $div->find($imagine,0)->src;

     $tempval[$i] = array ($gasit_nume,$gasit_link,$gasit_pret,$gasit_img,$gasit_stoc);

     $i++;
      }
  }

    $html->clear();
    unset($html);

    return $tempval;
}
vighi 回答:使用Ajax从PHP连续加载数据

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3162866.html

大家都在问