You are here:  » Async Search


Async Search

Submitted by Alex on Tue, 2019-04-09 13:59 in

Hi,

As you know i have a large database and because of that the search function sometimes takes some time.
Is there an option to make the search results async? So that the user directly sees the page and some kind of "loading" text till the results are there? (Now they have to wait till the script is ready before they see a page)

Submitted by support on Wed, 2019-04-10 11:08

Hi,

Here's a simple way to asynchronously load search results for user entered queries (so index queries e.g. merchant / category / brand will still load as normal as this is important for search engines)...

To give it a go, edit search.php and look for the following code at line 6:

  $q = (isset($_GET["q"])?tapestry_normalise($_GET["q"],":\.\/"):"");

...and REPLACE with:

  $q = (isset($_GET["q"])?tapestry_normalise($_GET["q"],":\.\/"):"");
  $async = isset($_GET["a"]);

And then the following code at line 77:

  if ($q)

...and REPLACE with:

  $parts = explode(":",$q);
  if (($q && $async) || (isset($parts[1])))

And finally, quite a substantial change to the display part of the script so rather than describe in individual mods, look for the following code beginning at (now) line 539:

  require("html/header.php");
  require("html/menu.php");
  require("html/searchform.php");
  require("html/banner.php");
  if (isset($searchresults))
  {
    if ($config_enableSearchFilters)
    {
      require("html/searchfilters.php");
    }
    require("html/searchresults.php");
  }
  else
  {
    require("html/noresults.php");
  }
  if (isset($navigation))
  {
    if ($minPrice || $maxPrice)
    {
      $sort .= "&minPrice=".$minPrice."&maxPrice=".$maxPrice;
    }
    if ($merchantFilter)
    {
      $sort .= "&merchantFilter=".urlencode($merchantFilter);
    }
    if ($categoryFilter)
    {
      $sort .= "&categoryFilter=".urlencode($categoryFilter);
    }
    if ($brandFilter)
    {
      $sort .= "&brandFilter=".urlencode($brandFilter);
    }
    require("html/navigation.php");
  }
  require("html/footer.php");

...and REPLACE with:

  if (!$async)
  {
    require("html/header.php");
    require("html/menu.php");
    require("html/searchform.php");
  }
  if (($q && $async) || (isset($parts[1])))
  {
    require("html/banner.php");
    if (isset($searchresults))
    {
      if ($config_enableSearchFilters)
      {
        require("html/searchfilters.php");
      }
      require("html/searchresults.php");
    }
    else
    {
      require("html/noresults.php");
    }
    if (isset($navigation))
    {
      if ($minPrice || $maxPrice)
      {
        $sort .= "&minPrice=".$minPrice."&maxPrice=".$maxPrice;
      }
      if ($merchantFilter)
      {
        $sort .= "&merchantFilter=".urlencode($merchantFilter);
      }
      if ($categoryFilter)
      {
        $sort .= "&categoryFilter=".urlencode($categoryFilter);
      }
      if ($brandFilter)
      {
        $sort .= "&brandFilter=".urlencode($brandFilter);
      }
      require("html/navigation.php");
    }
  }
  elseif($q && !$async)
  {
    print "<div id='sr'>";
    $banner["h2"] = "<strong>Searching...</strong>";
    require("html/banner.php");
    print "</div>";
    $href = $config_baseHREF."search.php?a=1&q=".urlencode($q)."&page=".$page."&sort=".$sort;
    if ($minPrice || $maxPrice) $href .= "&minPrice=".$minPrice."&maxPrice=".$maxPrice;
    if ($merchantFilter) $href .= "&merchantFilter=".urlencode($merchantFilter);
    if ($categoryFilter) $href .= "&categoryFilter=".urlencode($categoryFilter);
    if ($brandFilter) $href .= "&brandFilter=".urlencode($brandFilter);
    print "<script>window.onload = function() { $('#sr').load('".$href."'); }</script>";
  }
  if (!$async)
  {
    require("html/footer.php");
  }

Cheers,
David.
--
PriceTapestry.com