|
|
|
|
Bonjour,
J'amerais bien mettre en place sur mon site un moteur de recherche. Etant une tanche en prog, je me suis inspiré d'un script que j'ai trouvé sur le net. Dans ce script les resultats de la recherche sont affichés comme ceci :
nom trouvé
lien trouvé
Ce que je voudrais, sa serait un affichage dans ces eaux la mais je n'y arrive pas :
echo'<a href='lien trouve'>'nom trouve'</a>';
while ($nbre_mots < sizeof($motclef)) {
if (strlen($motclef[$nbre_mots]) > 2) {
$query_nom = mysql_query ("SELECT nom, lien FROM $table WHERE nom LIKE '%$motclef[$nbre_mots]%' ");
$query_lien = mysql_query ("SELECT nom, lien FROM $table WHERE lien LIKE '%$motclef[$nbre_mots]%' ");
while ($row_nom = mysql_fetch_array($query_nom)) {
echo 'Animes trouvé';
echo '<br>';
echo 'nom:';
echo '<br>';
printf ("%s", $row_nom[0]);
echo '<br>';
echo 'lien:';
echo '<br>';
printf ("%s", $row_nom[1]);
echo '<br>';
}
}
$nbre_mots++;
}
Dans la requête Sql, nom est invoqué en premier, puis lien en deuxième,
while ($nbre_mots < sizeof($motclef)) {
if (strlen($motclef[$nbre_mots]) > 2) {
$query_nom = mysql_query ("SELECT nom, lien FROM $table WHERE nom LIKE '%$motclef[$nbre_mots]%' ");
$query_lien = mysql_query ("SELECT nom, lien FROM $table WHERE lien LIKE '%$motclef[$nbre_mots]%' ");
while ($row_nom = mysql_fetch_array($query_nom)) {
echo '<a href="'.$row[1].'">'.$row[0].'</a>';
}
}
$nbre_mots++;
}
Je te conseille de glaner quelques renseignements sur l'affichage de données en php, sur le rôle des guillemets (simples ou double) et sur la concatenation. |
Il y a une dernière chose que j'aimerais savoir.
$table = "table1_tbl+table2_tbl+etc..."; J'ai effectuer plusieurs recherches mais je n'ai rien trouvé et j'aimerais finir ce petit script assez rapidement. J'apprendrais le php plus sérieusement après, je le jure ^^. Merci d'avance. |
Alors on va dire que le nom de tes tables est stocké comme ceci:
$table1="table1"; $table2="table2"; Bon je pars du principe que dans ces tables tu as un champs "nom" et un champs "lien". Sinon il faudra adapter. Ca donne donc ceci: while ($nbre_mots < sizeof($motclef)) {
if (strlen($motclef[$nbre_mots]) > 2) {
$like="%$motclef[$nbre_mots]%";
$query_nom = mysql_query ("SELECT lien.$table1, nom.$table1,lien.$table2,nom.$table2, FROM $table1,$table2 WHERE (nom.$table1 LIKE '$like') AND nom.table2 LIKE '$like' ");
while ($row_nom = mysql_fetch_array($query_nom)) {
echo '<a href="'.$row[0].'">'.$row[1].'</a>';
echo '<a href="'.$row[2].'">'.$row[3].'</a>';
}
}
$nbre_mots++;
}
J'ai simplifié quelques trucs. Et j'ai supprimé $query_lien qui ne servait à rien. |
D'accord donc par exemple si j'ai 3 tables ça donne ça :
while ($nbre_mots < sizeof($motclef)) {
if (strlen($motclef[$nbre_mots]) > 2) {
$like="%$motclef[$nbre_mots]%";
$query_nom = mysql_query ("SELECT lien.$table1, nom.$table1,lien.$table2,nom.$table2,lien.$table3,nom.$table3, FROM $table1,$table2,$table3 WHERE (nom.$table1 LIKE '$like') AND nom.table2 LIKE '$like' AND nom.table3 LIKE '$like' ");
while ($row_nom = mysql_fetch_array($query_nom)) {
echo '<a href="'.$row[0].'">'.$row[1].'</a>';
echo '<a href="'.$row[2].'">'.$row[3].'</a>';
echo '<a href="'.$row[4].'">'.$row[5].'</a>';
}
}
$nbre_mots++;
}
Ne manque-t-il pas $ ? WHERE (nom.$table1 LIKE '$like') AND nom.$table2 LIKE '$like' AND nom.$table3 LIKE '$like' "); |
Si. J'ai fait une coquille.
WHERE (nom.$table1 LIKE '$like') OR (nom.$table2 LIKE '$like') OR (nom.$table3 LIKE '$like') "); Faudrait tester pour voir. Je ne sais pas ce que ça va donner. Peut être qu'il vaut mieux faire une requête séparée pour chaque table. |
Ecris voir ceci en dessous du mysql_query:
echo "SELECT lien.$table1, nom.$table1,lien.$table2,nom.$table2,lien.$table3,nom.$table3 FROM $table1,$table2,$table3 WHERE (nom.$table1 LIKE '$like') OR (nom.$table2 LIKE '$like') OR (nom.$table3 LIKE '$like') "; Ca devrait afficher la requête à l'écran..... Tu peux pourrait l'écraire ici? Si ça n'affiche rien, transforme mysql_fetch_array par @mysql_fetch_array.... |
Voila se qui s'affiche :
SELECT lien.dossiersA_tbl, nom.dossiersA_tbl,lien.dossiersB_tbl,nom.dossiersB_tbl,lien.dossiersC_tbl,nom.dossiersC_tbl FROM dossiersA_tbl,dossiersB_tbl,dossiersC_tbl WHERE (nom.dossiersA_tbl LIKE '%blabla%') OR (nom.dossiersB_tbl LIKE '%blabla%') OR (nom.dossiersC_tbl LIKE '%blabla%') |
Argh oui excuse, j'ai fait tout de travers.
mysql_query("SELECT $table1.lien, $table1.nom,$table2.lien,$table2.nom,$table3.lien,$table3.nom FROM $table1,$table2,$table3 WHERE ($table1.nom LIKE '$like') OR ($table2.nom LIKE '$like') OR ($table3.nom LIKE '$like') "); |
Bon, maintenant, tu auras peut être des erreurs car certains pourront être remplis et pas d'autres.
|
Ok, voilà ce qu'il faudrait que tu fasses:
while ($nbre_mots < sizeof($motclef)) {
if (strlen($motclef[$nbre_mots]) > 2) {
$query_nom1 = mysql_query ("SELECT nom, lien FROM $table1 WHERE nom LIKE '%$motclef[$nbre_mots]%' ");
$query_nom2=mysql_query("SELECT nom, lien FROM $table2 WHERE nom LIKE '%$motclef[$nbre_mots]%' ");
$query_nom3=mysql_query("SELECT nom, lien FROM $table3 WHERE nom LIKE '%$motclef[$nbre_mots]%' ");
echo "<br><br>$table1 : <br><br>";
while ($row1 = mysql_fetch_array($query_nom)) {
echo '<a href="'.$row1[1].'">'.$row1[0].'</a>';
}
echo "<br><br>$table2 : <br><br>";
while ($row2 = mysql_fetch_array($query_nom)) {
echo '<a href="'.$row2[1].'">'.$row3[0].'</a>';
}
echo "<br><br>$table3 : <br><br>";
while ($row_nom3 = mysql_fetch_array($query_nom)) {
echo '<a href="'.$row[1].'">'.$row[0].'</a>';
}
}
$nbre_mots++;
}/
Voilà, en espérant qu'il n'ya pas de fautes.... |