Page 1 sur 1

probleme moteur de recherche interne

Posté : 12 mai 2012, 20:36
par graffx
Salut a tous!

Voila, je n' arrive pas a afficher mes données, j' ai une page blanche concernant le moteur de recherche de mon site.

Je me suis basé sur un exemple du forum du SDZ. Si vous pouviez me dire ce qui cloque, ca serait sympa!

Merci d' avance!
<?php
   $mots = $_POST['search'];
   $motrecherche=explode(" ",$mots);
   $WHERE = "WHERE";
   $count = 0;
   reset ($motrecherche);
   while(list($key, $data) = each($motrecherche))
      {
      $count = $count + 1;
      if ($count == 1)   $WHERE .= "     titre LIKE '%" . $data . "%' "; 
      else               $WHERE .= " AND texte LIKE '%" . $data . "%' "; 
      }

$resultat = mysql_query ("SELECT DISTINCT id, titre, vendeur, texte FROM bernay_ann $WHERE ORDER BY date") or die(mysql_error());  
while ($ann= mysql_fetch_array($resultat))
{
echo $ann['titre'];
}
?>

Re: probleme moteur de recherche interne

Posté : 13 mai 2012, 15:50
par graffx
Bon, ayant pas eu de reponse, j' ai decidé de faire au plus simple d'abord puis de compliquer ensuite la chose.

Au plus simple, j' ai une erreur indécelable pour moi.
	if(isset($_POST['search'])&&($_POST['search'] !== '')&&($_POST['search'] !== 'TROUVER UN PRODUIT...'))
	{
    $mots = 'titre="'.$_POST['search'].'"';
	}
	else
	{
	$mots = '';
	}
   
   
   if(isset($_POST['cp'])&&($_POST['cp'] !== '')&&($_POST['cp'] !== 'Code postal'))
	{
   $rcp = 'cp="'.$_POST['cp'].'"';
	}
	elseif(isset($_POST['cp'])&&($_POST['cp'] !== '')&&($_POST['cp'] !== 'Code postal')&&isset($_POST['search'])&&($_POST['search'] !== '')&&($_POST['search'] !== 'TROUVER UN PRODUIT...'))
	{
   $rcp = 'AND cp = "'.$_POST['cp'].'"';
	}
	else
	{
	$rcp == '';
	}
	
 
$resultat = mysql_query ("SELECT DISTINCT * FROM bernay_ann WHERE $mots $cp")or die(mysql_error()); 
Probleme:

Si je tape juste le code postal 7500 , ca m' affiche bien les annonces ayant pour code postal 75000.

Si je tape juste un mot clé, j' ai droit a ceci:


You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Code postal' at line 1

Sachant que j' ai mis une petite fonction javascript qui affiche si le champs est vide ce qu' il faut remplir, exemple : "Code postal" qui disparait si on clique:
<input type="text" name="search" id="search" style="width:180px; border-color:#900; height:25px; font-size:14px;" onblur="value=('<?php
        if(isset($_POST['search']))
		{
		echo $_POST['search'];
		}
		else
		{
		echo 'TROUVER UN PRODUIT...';
		}
		?>')"  value="<?php
        if(isset($_POST['search']))
		{
		echo $_POST['search'];
		}
		else
		{
		echo 'TROUVER UN PRODUIT...';
		}
		?>" onfocus="value=('<?php
        if(isset($_POST['search']))
		{
		echo $_POST['search'];
		}
		else
		{
		echo '';
		}
		?>')"  placeholder="<?php
        if(isset($_POST['search']))
		{
		echo $_POST['search'];
		}
		else
		{
		echo 'TROUVER UN PRODUIT...';
		}
		?>" />
            <br />
            <br />
            <select name="t" id="select"  style="width:180px; border-color:#900; height:25px; font-size:11px;" >
              <option selected="selected" disabled="disabled">Type</option>
              <option value="Professionnel">Professionnel</option>
              <option value="Particulier">Particulier</option>
            </select>
<br />
<br />
            <input name="cp" type="text" id="cp" size="12" maxlength="5" style="width:180px; border-color:#900; height:25px; font-size:11px;" onblur="value=('<?php
        if(isset($_POST['cp']))
		{
		echo $_POST['cp'];
		}
		else
		{
		echo 'Code postal';
		}
		?>')"  value="<?php
        if(isset($_POST['cp']))
		{
		echo $_POST['cp'];
		}
		else
		{
		echo 'Code postal';
		}
		?>" onfocus="value=('<?php
        if(isset($_POST['cp']))
		{
		echo $_POST['cp'];
		}
		else
		{
		echo '';
		}
		?>')"  placeholder="<?php
        if(isset($_POST['cp']))
		{
		echo $_POST['cp'];
		}
		else
		{
		echo 'Code postal';
		}
		?>" />
Personnellement je ne comprend pas l' erreur!

Merci de votre aide ca m' arrangerait pour avancer! :s

Re: probleme moteur de recherche interne

Posté : 13 mai 2012, 18:10
par graffx
Je tente une deriere reponse puis j' abandonne, c'est au dessus de mes capacités, le probleme viendrai de AND.

si je met le AND directement dans la requete ca marche, mais si je le met dans une variable, marche plus.

Ca marche:
SELECT DISTINCT id, titre, cp FROM bernay_ann WHERE $mots AND $rcp ORDER BY id DESC
Ca marche plus:
$rcp= $_POST['cp'];
   if(isset($_POST['cp'])&&$_POST['cp'] !== '')
	{
   $rcp = 'cp='.$rcp.'';
	}
	elseif(isset($_POST['cp'])&&$_POST['cp'] !== ''&&isset($mots)&&$mots != '')
	{
   $rcp = 'AND cp= '.$rcp.'';
	}
	else
	{
	$rcp == '';
	}

Pourtant je ne vois pas ce qui le choque....

Re: probleme moteur de recherche interne

Posté : 13 mai 2012, 18:35
par AB
A la fin, fais afficher ta requête pour voir le résultat.
$requete = "SELECT.....";

//...

//debug vérification
echo $requete;

Re: probleme moteur de recherche interne

Posté : 13 mai 2012, 20:57
par graffx
Je ne peux pas a cause de l' erreur qui est toujours la.

En fait la requete est bonne comme dit plus haut si je met le AND directement dans la requete entre les deux variable, si je met le AND dans la variable qui elle sera dans la requete, probleme.

Y' a t-il une sorte de concatenation pour les fonction AND OR etc quand elle sont dans une variable? C'est surtout ca la clé au probleme en fait.

Re: probleme moteur de recherche interne

Posté : 13 mai 2012, 21:03
par graffx
Autant pour moi, j' ai reussi a afficher "Resource id #7 ", bizarre non?

Re: probleme moteur de recherche interne

Posté : 13 mai 2012, 22:17
par AB
Autant pour moi, j' ai reussi a afficher "Resource id #7 ", bizarre non?
ça c'est la ressource de la requête une fois exécutée (après le mysql_query).
Mets la chaine de la requête dans une variable avant de la faire exécuter cela permet entre autre de pouvoir la faire afficher pour débug.
$requete = "SELECT.....";

//...

//vérification
echo $requete;

//exécution de la requête
$resultat = mysql_query ($requete) or die(mysql_error()); 

Re: probleme moteur de recherche interne

Posté : 18 mai 2012, 01:16
par graffx
Hello, desolé de l' absence, je suis en plein déménagement!

Bon alors j' ai réussi a un résultat.

Tout fonctionne, un ou plusieurs mots clés seuls ou avec un code postal, mais un probleme persiste, si je n' ai pas de resultat, il devrait me l' afficher, hors, j' ai juste droit a une page blanche avec mon header en haut.


Un twix a celui qui comprend!
<?php
session_cache_limiter('private_no_expire, must-revalidate');
?><table width="950" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="1286" height="209"><table width="950" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
          <td width="710" align="center" valign="top"><br />



<?php




$req = "SELECT DISTINCT * FROM bernay_ann";
if(isset($_POST['cp']) && !empty($_POST['cp'])) {
	$rcp = $_POST['cp'];
	$req .= " WHERE cp='".$rcp."'";
	if(isset($_POST['search']) && !empty($_POST['search'])) {
		$array_mots = explode(' ',$_POST['search']); //on sépare les mots
		$req .= " AND titre LIKE '%".implode($array_mots,"%' OR  titre LIKE '% ")."%'";
	}
} elseif(isset($_POST['search']) && !empty($_POST['search'])) {
	$array_mots = explode(' ',$_POST['search']); //on sépare les mots
	$req .= " WHERE titre LIKE '%".implode($array_mots,"%' OR  titre LIKE '% ")."%'";
}
$req .= " ORDER BY id DESC";
$query = mysql_query($req)or die(mysql_error());
$nbre = mysql_num_rows($query)or die(mysql_error());

if($nbre != '')
{
?>
<?php
while ($ann= mysql_fetch_array($query))
{




?>

<table width="690" border="0" align="center" cellpadding="0" cellspacing="0">
              <tr>
                <td style="background:url(images/divers/bg_ann.gif);">
<table width="690" height="90" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="96" align="center" valign="middle"><img src="images/shops/<?php echo $ann['photo1']; ?>" alt="" width="80" height="70" /></td>
    <td width="594" align="left" valign="top"><table width="585" border="0" align="right" cellpadding="0" cellspacing="0">
      <tr>
        <td height="10" colspan="2"></td>
      </tr>
      <tr>
        <td width="402" height="19" style="font-weight:bolder; font-size:14px; color:#300;"><a href="index.php?a=ann&id=<?php echo $ann['id']; ?>=&t=<?php echo $_GET['t']; ?>&cp=<?php echo $_GET['cp']; ?>&v=<?php echo $_GET['v']; ?>&pmn=<?php echo $_GET['pmn']; ?>&pmx=<?php echo $_GET['pmx']; ?>">
          <?php
		echo $ann['titre'];	
		?>
        </a></td>
        <td width="183" align="right"  style="font-weight:bolder; font-size:14px; color:#300;"><table width="170" border="0" align="right" cellpadding="0" cellspacing="0">
          <tr>
            <td align="right" valign="top"><a href="index.php?a=cpte&pr=<?php echo $ann['vendeur']; ?>" title="Ceci est la note globale du vendeur donn&eacute;e par les acheteurs. Vous pouvez consulter son profil pour plus d' informations">
              <?php
							  if($ann['pro'] == '1')
							  {
								  ?>
              <?php
								  $vendeuru = $ann['vendeur'];
								  
								  $prou2 = mysql_query('SELECT pseudo FROM bernay_users WHERE pseudo = "'.$vendeuru.'"');
								  $prou= mysql_fetch_array($prou2)
								  ?>
              <?php
								  
							  echo '<a href="index.php?a=cpte&id='.$prou['pseudo'].'"><img src="images/divers/pro.png" title="'.$prou['pseudo'].'" alt="" border="0" align="absbottom" /></a><img src="images/header/blank.gif" alt="" width="20" />';
							  }					  
							  ?>
              <?php
						
						$annvendeur = $ann['vendeur'];
						
						$retour_app2=mysql_query("SELECT COUNT(*) AS id FROM bernay_app WHERE cible = '".$annvendeur."' "); //Nous r&eacute;cup&eacute;rons le contenu de la requ&ecirc;te dans $retour_total
						$donnees_app2=mysql_fetch_assoc($retour_app2); //On range retour sous la forme d'un tableau.
						$app2=$donnees_app2['id']; //On r&eacute;cup&egrave;re le total pour le placer dans la variable $total.
						?>
              <?php
                		$req_app2="SELECT SUM(note) as total FROM bernay_app WHERE cible = '".$annvendeur."'";
 					    $result1_app2=mysql_query($req_app2);
						$val1_app2=mysql_fetch_array($result1_app2);
						$total_app2 = $val1_app2['total'];
						
						if($app2 > '0')
						{
						$total_app3 = ($total_app2 / $app2);
						}
						?>
              <?php
						if ($app2 == '0')
						{
						echo '0 ';
						}
						else
						{
						echo $total_app3;
						echo ' ';
						}
						?>
              / 10</a></td>
            <td width="20">&nbsp;</td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td height="50" align="left" valign="bottom" style="font-weight:bolder; font-size:14px; color:#300;"><?php
		echo $ann['cp'];
		echo ' ';
		echo $ann['ville'];
		?></td>
        <td align="left" valign="bottom"><table width="100" border="0" align="right" cellpadding="0" cellspacing="0">
          <tr>
            <td align="right" valign="top" style="font-weight:bolder; font-size:14px; color:#300;"><?php
                            echo $ann['prix'];
							?>
              &#x20AC;</td>
            <td width="20" align="right" valign="top" style="font-weight:bolder; font-size:14px; color:#300;">&nbsp;</td>
          </tr>
        </table></td>
      </tr>
    </table></td>
  </tr>
</table>
</td>
</tr>
</table>
<?php
}
}
else
{
echo '<table width="550" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td colspan="2" align="center" valign="top" style="font-size:14px; font-weight:bolder; text-align:center;">Aucun r&eacute;sultat pour cette recherche, recommencez !<br /><br />
    <img src="images/divers/bureaucrate.png" width="500" height="402" /><br /><br /></td>
  </tr>
  <tr>
    <td width="50%" align="center" valign="top">
    <script type="text/javascript"><!--
google_ad_client = "ca-pub-2988340172457918";
/* 336x280, date de cr&eacute;ation 05/12/09 */
google_ad_slot = "9129849470";
google_ad_width = 336;
google_ad_height = 280;
//-->
      </script>
            <script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
      </script></td>
    <td height="20" align="center" valign="top">
    <script type="text/javascript"><!--
google_ad_client = "ca-pub-2988340172457918";
/* grand carre 2 */
google_ad_slot = "0826716095";
google_ad_width = 336;
google_ad_height = 280;
//-->
      </script>
            <script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
      </script>   
      </td>
  </tr>
</table>
';
}
?>

</td>
          <td width="240" height="547" align="left" valign="top"><span style="z-index:100;">
            
          </span><br />
            <table width="230" height="203" border="0" align="right" cellpadding="0" cellspacing="0">
              <tr>
                <td height="230" align="left" valign="top">
				<?php 
				include ('crit.php');
				?>
				</td>
              </tr>
          </table></td>
        </tr>
      </table></td>
  </tr>
</table>