You are here:  » Return Number Of search results before submitting


Return Number Of search results before submitting

Submitted by Selector84 on Tue, 2018-02-20 19:12 in

Hi David,

I'm trying to display a loading number of products window, looking at {link saved} and if you type a search in the text box it returns the products found then loads the form.

The only issue is the way I read the pt_products database is no where as advanced as it's done in your plugin code so it won't always match what pt actually returns.

The only way around it I could see is from approaching this by using the same method as https://www.pricetapestry.org/node/323 but returning the total results found instead.

I assume I would need a modified searchJSON.php file in the main PT installation to do this, could you assist with a modified version to return the found OF results found?

Currently it goes something like this:

{code saved}

Regards,
Sean

Submitted by support on Wed, 2018-02-21 10:20

Hello Sean,

You can use the plugin's pto_search_buildWhere() function to get exactly the same SQL as will be used to perform the query - it's just a case of making sure that all the necessary variables are global, populating $pto_q with your search terms, making the initial query and finally the FOUND_ROWS() query to get the total number of results - have a go with something like this;

  global $wpdb;
  global $pto_q;
  global $pto_searchWhere;
  global $pto_searchSelect;
  global $pto_config_databaseTablePrefix;
  // set keyword from search form in $pto_q here
  $pto_q = "Keywords";
  pto_search_buildWhere();
  $sql = "SELECT ".$pto_searchSelect." FROM `".$pto_config_databaseTablePrefix."products`
            WHERE ".$pto_searchWhere." GROUP BY search_name";
  if ($wpdb->query($sql))
  {
    $sql = "SELECT FOUND_ROWS() as numResults";
    $wpdb->query($sql);
    $numResults = $wpdb->last_result[0]->numResults;
    // number of search results is here in $numResults
    print $numResults;
  }

The above code should work anywhere in WordPress, and will just print the number of results for "Keywords". Looking at your code I think you would want to use;

  $pto_q = $_POST["pto_text_search_data"]; ),

...and then output $numResults instead of the print line as required...

When constructing SQL, Don't forget to use esc_sql() around all variables used to make the query, for example where you might have:

WHERE field LIKE '%".$variable."%'

...this should be:

WHERE field LIKE '%".esc_sql($variable)."%'

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by Selector84 on Thu, 2018-02-22 06:17

Thanks David,

In the end I have now bounced the results from the main pt installation, I stripped down the search page and pass the query over file get contents as it matches the returned number of total results perfectly. Might be a bit long winded but was quick and works.

Thanks again for your perfect support,
Regards,
Sean