jholdersatx Posted December 2, 2008 Share Posted December 2, 2008 Is there an existing contribution, or perhaps a simple modification that would make it so that if a customer were to use the search field, and the result of that search was only one item, instead of staying on the search page, the script would bypass the result page, and go straight to the product page? I've been toying with it, but keep getting nothing but errors. Link to comment Share on other sites More sharing options...
jholdersatx Posted December 3, 2008 Author Share Posted December 3, 2008 Currently I'm mainly having problems isolating where the variable is that stores the listing for the table, I'm trying to make it so that if there's only one result, it'll constitute a 301 redirect to the "/store/-####-p-.html" where #### is the pid number returned. Link to comment Share on other sites More sharing options...
jholdersatx Posted December 3, 2008 Author Share Posted December 3, 2008 it seems to be here at about line 255 in advanced_search_result.php: if (isset($search_keywords) && (sizeof($search_keywords) > 0)) { $where_str .= " and ("; for ($i=0, $n=sizeof($search_keywords); $i<$n; $i++ ) { switch ($search_keywords[$i]) { case '(': case ')': case 'and': case 'or': $where_str .= " " . $search_keywords[$i] . " "; break; default: $keyword = tep_db_prepare_input($search_keywords[$i]); $where_str .= "(pd.products_name like '%" . tep_db_input($keyword) . "%' or p.products_model like '%" . tep_db_input($keyword) . "%' or m.manufacturers_name like '%" . tep_db_input($keyword) . "%'"; if (isset($HTTP_GET_VARS['search_in_description']) && ($HTTP_GET_VARS['search_in_description'] == '1')) $where_str .= " or pd.products_description like '%" . tep_db_input($keyword) . "%'"; $where_str .= ')'; break; } } $where_str .= " )"; Link to comment Share on other sites More sharing options...
jholdersatx Posted December 3, 2008 Author Share Posted December 3, 2008 The code should be practically identical to the one where it activates if 0 items were returned, that's all I'm trying to find now is where the code is activated that brings the error page "no items found that match the search criteria" ? Then I just need to create an if/else statement to say if 1 item returned, go to that item. Link to comment Share on other sites More sharing options...
Kanie Posted December 3, 2008 Share Posted December 3, 2008 This is possible, but you would either have to change how advanced_search_results.php works or include a few additional SQL queries and evaluations into the file. The search results are handled by the 'product_listing.php' module and only in this file does the SQL query execute and get evaluated to return the appropriate response. Unfortunately, by the time this SQL result is evaluated HTTP headers have already been sent by the page meaning you can no longer use tep_redirect() to redirect the user. The solution would be to carry out an SQL query prior to any HTML being sent and therefore would require you to move a bulk of the PHP code from inside to the HTML to above it. Then you would have to write the additional query, evaluate that query and if it returns 1 query the database again to find the product information. After all of that you could then execute the tep_redirect() to redirect the user. To be honest, it is a great idea and whilst the extra queries aren't ideal it does offer some usability benefits. I may return to this post a little later tonight with a working example. Link to comment Share on other sites More sharing options...
jholdersatx Posted December 3, 2008 Author Share Posted December 3, 2008 it doesn't necessarily have to use tep_redirect() just to beat the header, though, does it? I've actually noticed by accident that if you declare multiple headers, for some reason certain browsers pick up on them, and execute the code. (at least for metatags) My train of thought is to add an arbitrary variable to advanced_search.php ($alpha=1) and inside of product_listing.php add a check for it, and double the code over (if $alpha=1 , elseif alpha<= 0 }}) so that the same could would be committed dependent on who's calling it, from there simply modify $list_box_contents[0] = array('params' => 'class="productListing-odd"'); $list_box_contents[0][] = array('params' => 'class="productListing-data"', 'text' => TEXT_NO_PRODUCTS); to simply calling the pid that's in the sql string, and creating a tep_redirect() to a hard coded html value with the pid variable in there. Its just parsing that sql string successfully that's throwing me off. Link to comment Share on other sites More sharing options...
Kanie Posted December 4, 2008 Share Posted December 4, 2008 "it doesn't necessarily have to use tep_redirect() just to beat the header, though, does it?I've actually noticed by accident that if you declare multiple headers, for some reason certain browsers pick up on them, and execute the code. (at least for metatags)" I really don't like the thought of declaring multiple headers. As you said yourself "certain browsers pick up on them", but certainly now all. The best method for doing this is the redirect the user before any HTML is output.. Which is the solution that I am going to show you now.. 1) In advanced_search_result.php copy everything between and including these following lines: // create column list $define_list = array('PRODUCT_LIST_MODEL' => PRODUCT_LIST_MODEL, $listing_sql = $select_str . $from_str . $where_str . $order_str; Once copied, delete these lines. 2) In advanced_search_result.php locate this line: if ($error == true) { tep_redirect(tep_href_link(FILENAME_ADVANCED_SEARCH, tep_get_all_get_params(), 'NONSSL', true, false)); } And paste everything you just copied after it. 3) Directly after everything you have just pasted, paste this code: $listing_split = new splitPageResults($listing_sql, MAX_PRODUCTS_PER_PAGE, 'p.products_id'); if($listing_split->number_of_rows == 1) { $product_query = tep_db_query($listing_split->sql_query); $product = tep_db_fetch_array($product_query); tep_redirect(tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $product['products_id'])); } 4) In includes/modules/product_listing.php locate this code: $listing_split = new splitPageResults($listing_sql, MAX_PRODUCTS_PER_PAGE, 'p.products_id'); And replace it with: if(basename($_SERVER['PHP_SELF']) != FILENAME_ADVANCED_SEARCH_RESULT) $listing_split = new splitPageResults($listing_sql, MAX_PRODUCTS_PER_PAGE, 'p.products_id'); I have tested and implemented this successfully, but if you've made any deep modifications to advanced_search_result.php before then the code I have supplied may break what you've previously done. This method doesn't use any unnecessary queries and point #4 ensures that. Modifying the product_listing.php module in this way is saying only execute the listing_split SQL query if we're not in advanced_search_result.php - this is because our modified advanced_search_result.php already executes this code before. Link to comment Share on other sites More sharing options...
Kanie Posted December 4, 2008 Share Posted December 4, 2008 I just realised that anyone who used the code above will get a "division by zero" error - this is because I am making reference to a variable I created for my own purposes. Any references to "MAX_PRODUCTS_PER_PAGE" should actually be "MAX_DISPLAY_SEARCH_RESULTS". The code to use for point #3 should be: $listing_split = new splitPageResults($listing_sql, MAX_DISPLAY_SEARCH_RESULTS, 'p.products_id'); if($listing_split->number_of_rows == 1) { $product_query = tep_db_query($listing_split->sql_query); $product = tep_db_fetch_array($product_query); tep_redirect(tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $product['products_id'])); } And point #4 should be.. In includes/modules/product_listing.php locate: $listing_split = new splitPageResults($listing_sql, MAX_DISPLAY_SEARCH_RESULTS, 'p.products_id'); And replace it with: if(basename($_SERVER['PHP_SELF']) != FILENAME_ADVANCED_SEARCH_RESULT) $listing_split = new splitPageResults($listing_sql, MAX_DISPLAY_SEARCH_RESULTS, 'p.products_id'); Link to comment Share on other sites More sharing options...
Kanie Posted December 4, 2008 Share Posted December 4, 2008 I decided to release this as a contribution. Not sure how many people would be interested in it, but it does make for some useful functionality. http://www.oscommerce.com/forums/index.php?showtopic=322785 Link to comment Share on other sites More sharing options...
jholdersatx Posted December 4, 2008 Author Share Posted December 4, 2008 Installed code into the RC2a site that we run, and it executes flawlessly. Link to comment Share on other sites More sharing options...
Kanie Posted December 4, 2008 Share Posted December 4, 2008 Excellent.. nice to hear you've got the solution you were looking for :) Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.