Jump to content

Recherche avec et sans espace dans prestashop


Recommended Posts

Bonsoirs,

dans le fichier /override/classes/ j'ai ajouter search.php avec ce bou de code

<?php
Class Search extends SearchCore
{
public static function find($id_lang, $expr, $pageNumber = 1, $pageSize = 1, $orderBy = 'position', $orderWay = 'desc', $ajax = false)
{
 global $cookie;
 $db = Db::getInstance();
 // TODO : smart page management
 if ($pageNumber < 1) $pageNumber = 1;
 if ($pageSize < 1) $pageSize = 1;

 if (!Validate::isOrderBy($orderBy) OR !Validate::isOrderWay($orderWay))
  die(Tools::displayError());

 $whereArray = array();
 $inArray = array();
 $scoreArray = array();
 $words = explode(' ', Search::sanitize($expr, $id_lang));
 foreach ($words as $key => $word)
  if (!empty($word) AND strlen($word) != 1)
  {
   $word = str_replace('%', '\\%', $word);
   $word = str_replace('_', '\\_', $word);
   $inListResult = $db->ExecuteS(' SELECT DISTINCT `id_product`
    FROM `'._DB_PREFIX_.'search_word` sw
    LEFT JOIN `'._DB_PREFIX_.'search_index` si ON sw.`id_word` = si.`id_word`
    WHERE sw.`id_lang` = '.intval($id_lang).'
    AND sw.`word` LIKE '.($word[0] == '-' ? ' \''.pSQL(Tools::substr($word, 1, PS_SEARCH_MAX_WORD_LENGTH)).'%\'' : '\''.pSQL(Tools::substr($word, 0, PS_SEARCH_MAX_WORD_LENGTH)).'%\''));
   $inList=array();
	   foreach($inListResult as $result)
	   {
		    $inList[]=$result['id_product'];
	   }
   if(count($inList)) $whereArray[] = ' p.id_product '.($word[0] == '-' ? 'NOT' : '').' IN ('.implode(',', $inList).')';
   if ($word[0] != '-')
 $scoreArray[] = 'sw.word LIKE \''.pSQL(Tools::substr($word, 0, PS_SEARCH_MAX_WORD_LENGTH)).'%\'';
  }
  else
   unset($words[$key]);
 if (!sizeof($words))
  return ($ajax ? array() : array('total' => 0, 'result' => array()));

 

je veux quand cherchant la référence 245589 que je taper 245 589 ou 24 55 89 je trouve le même resultas

 

dans ma recherche sur mon site si je tapée 245%589 je tombe sur 245589 en gros je veux replacer dans ce code les espace par des % merci pour votre aide.

Link to comment
Share on other sites

Bonjour,

 

En remplacant

$words = explode(' ', Search::sanitize($expr, $id_lang));

Par

$words = explode(' ', Search::sanitize($expr, $id_lang));
$words[] = implode('%', $words);

 

Cela devrait fonctionner.

 

La ligne supplémentaire permet de rajouter à la liste des mots à chercher la concaténation de tous les mots avec un % au milieu.

 

Il faut également enlever la protection pour ce dernier mot ajouté :

$word = str_replace('%', '\\%', $word);

 

Deviend

if ($key != sizeof($words)-1)
   $word = str_replace('%', '\\%', $word);

 

Je n'ai pas testé mais ca devrait marcher.

Link to comment
Share on other sites

Merci pour votre reponce je vient de testez le code que vous m'avez donner.

 

c'est deja mieux mais en tappant exemple 107 106 j'ai tous les debut qui commence par 107 qui sort et ce qui finnisse aussi par 106 ou contenant le mieux serais d'avoir que 107106 qui sort uniquement merci pour votre aide

 

voici le code modifier que vous m'avez donner

 

<?php
Class Search extends SearchCore
{
public static function find($id_lang, $expr, $pageNumber = 1, $pageSize = 1, $orderBy = 'position', $orderWay = 'desc', $ajax = false)
{
 global $cookie;
 $db = Db::getInstance();
 // TODO : smart page management
 if ($pageNumber < 1) $pageNumber = 1;
 if ($pageSize < 1) $pageSize = 1;

 if (!Validate::isOrderBy($orderBy) OR !Validate::isOrderWay($orderWay))
  die(Tools::displayError());

 $whereArray = array();
 $inArray = array();
 $scoreArray = array();
 $words = explode(' ', Search::sanitize($expr, $id_lang));
$words[] = implode('%', $words);
 foreach ($words as $key => $word)
  if (!empty($word) AND strlen($word) != 1)
  {
   if ($key != sizeof($words)-1)
   $word = str_replace('%', '\\%', $word);
   $word = str_replace('_', '\\_', $word);
   $inListResult = $db->ExecuteS(' SELECT DISTINCT `id_product`
    FROM `'._DB_PREFIX_.'search_word` sw
    LEFT JOIN `'._DB_PREFIX_.'search_index` si ON sw.`id_word` = si.`id_word`
    WHERE sw.`id_lang` = '.intval($id_lang).'
    AND sw.`word` LIKE '.($word[0] == '-' ? ' \'%'.pSQL(Tools::substr($word, 1, PS_SEARCH_MAX_WORD_LENGTH)).'%\'' : '\'%'.pSQL(Tools::substr($word, 0, PS_SEARCH_MAX_WORD_LENGTH)).'%\''));
   $inList=array();
	   foreach($inListResult as $result)
	   {
		    $inList[]=$result['id_product'];
	   }
   if(count($inList)) $whereArray[] = ' p.id_product '.($word[0] == '-' ? 'NOT' : '').' IN ('.implode(',', $inList).')';
   if ($word[0] != '-')
 $scoreArray[] = 'sw.word LIKE \'%'.pSQL(Tools::substr($word, 0, PS_SEARCH_MAX_WORD_LENGTH)).'%\'';

Link to comment
Share on other sites

  • 2 years later...

Bonjour je trouve votre post genial. j'essaie de l'integrer sur mon site neanmoins je pense que le code n'est pas terminé.

 

Mon site me donne une erreur de systaxe a la derniere ligne nr43.

 

voila comment se termine le code:

 

   else
    unset($words[$key]);
  if (!sizeof($words))
   return ($ajax ? array() : array('total' => 0, 'result' => array()));
 
 
Auriez-vous la fin du code?
 
merci d'avance.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...