par
Pok » 23 mai 2012, 13:58
Bonjour,
Je débute en php, et j'essaye de monter un
moteur de recherche avec
extraction de BDD (mysql / phpmyadmin). J'ai pondu un mix à partir des tuto/exemples... trouvés sur le net en essayant de comprendre comment ça fonctionne. Helas
mon système de pagination ne fonctionne pas depuis que j'ai tout passé en mode
FULLTEXT (Ajout d'index FULLTEXT dans la BDD, et script php normalement adapté (de ce que j'ai pu en comprendre)).
Observez le résultat :
http://www.levac.fr/test/moteur.php Faites une recherche sur le mot-clé "cable", les résultats s'affichent en 1ere page OK..... la pagination s'affiche en bas OK... il existe bel et bien 3 pages pour les résultats du mot clé "cable" OK ........ MAIS dès qu'on clique sur la plage 2 ou 3 ... plus rien ! Il semblerait que lorsqu'on change de page, on perd le mot-clé...... mais pourquoiiii snif ?! Avez vous une idée, que vous pourriez m'expliquer le plus clairement possible s'il vous plait ?
Merci pour l'aide que vous pourrez m'apporter.
Ci-dessous l'image montrant les index fulltext crées :
Puis voici mon code :
Code : Tout sélectionner
<!DOCTYPE html>
<html>
<head>
<title>Recherche</title>
<style>
.pagination {
line-height:2em;
}
.pagination a,
.pagination span {
padding:0.2em 0.5em;
}
.pagination a {
border:1px solid #9AAFE5;
color:#105CB6;
text-decoration:none;
}
.pagination a:hover {
border:1px solid #296BB5;
color:#000031;
}
.pagination .prevnext {
font-weight:bold;
}
.pagination span.disabled {
border:1px solid #DDDDDD;
color:#999999;
}
.pagination span.current {
border:1px solid #2E6AB1;
background-color:#2E6AB1;
color:#ffffff;
font-weight:bold;
}
</style>
</head>
<body>
<?
mysql_connect('host',bdd','pass'); //ce sont pas vrai log hein... inutile de montrer ça à tout le monde
mysql_select_db('bdd'); // idem, pas le vrai !
$requete = htmlspecialchars(str_replace(' ',' +',' '.$_POST['requete']));
print $requete;
function pagination($current_page, $nb_pages, $link='?page=%d', $around=3, $firstlast=1)
{
$pagination = '';
$link = preg_replace('`%([^d])`', '%%$1', $link);
if ( !preg_match('`(?<!%)%d`', $link) ) { $link .= '%d'; }
if ( $nb_pages > 1 ) {
// Lien précédent
if ( $current_page > 1 ) {
$pagination .= '<a class="prevnext" href="'.sprintf($link, $current_page-1).'" title="Page précédente">« Précédent</a>';
}
else {
$pagination .= '<span class="prevnext disabled">« Précédent</span>';
}
// Lien(s) début
for ( $i=1 ; $i<=$firstlast ; $i++ ) {
$pagination .= ' ';
$pagination .= ($current_page==$i) ? '<span class="current">'.$i.'</span>' : '<a href="'.sprintf($link, $i).'">'.$i.'</a>';
}
// ... après pages début ?
if ( ($current_page-$around) > $firstlast+1 ) {
$pagination .= ' …';
}
// On boucle autour de la page courante
$start = ($current_page-$around)>$firstlast ? $current_page-$around : $firstlast+1;
$end = ($current_page+$around)<=($nb_pages-$firstlast) ? $current_page+$around : $nb_pages-$firstlast;
for ( $i=$start ; $i<=$end ; $i++ ) {
$pagination .= ' ';
if ( $i==$current_page ) {
$pagination .= '<span class="current">'.$i.'</span>';
}
else {
$pagination .= '<a href="'.sprintf($link, $i).'">'.$i.'</a>';
}
}
// ... avant page nb_pages ?
if ( ($current_page+$around) < $nb_pages-$firstlast ) {
$pagination .= ' …';
}
// Lien(s) fin
$start = $nb_pages-$firstlast+1;
if( $start <= $firstlast ) { $start = $firstlast+1; }
for ( $i=$start ; $i<=$nb_pages ; $i++ ) {
$pagination .= ' ';
$pagination .= ($current_page==$i) ? '<span class="current">'.$i.'</span>' : '<a href="'.sprintf($link, $i).'">'.$i.'</a>';
}
// Lien suivant
if ( $current_page < $nb_pages ) {
$pagination .= ' <a class="prevnext" href="'.sprintf($link, ($current_page+1)).'" title="Page suivante">Suivant »</a>';
}
else {
$pagination .= ' <span class="prevnext disabled">Suivant »</span>';
}
}
return $pagination;
}
// Numero de page (1 par défaut)
if( isset($_GET['page'])){
$page = $_GET['page'];
}
else {
$page = 1;
}
// Nombre d'info par page
$pagination = 10;
// Numéro du 1er enregistrement à lire
$limit_start = ($page-1)*$pagination;
$sql = "SELECT * FROM produits WHERE MATCH (DESIGNATION, DESCRIPTION) AGAINST ('%$requete%' IN BOOLEAN MODE) ORDER BY id DESC LIMIT $limit_start, $pagination";
$resultat = mysql_query($sql);
while( $donnee = mysql_fetch_assoc($resultat) ){
?> <a href="<? echo $donnee['LIEN']; ?>"> <? echo "<h4>{$donnee['DESIGNATION']}</h4>"; ?></a><?
echo $donnee['DESCRIPTION'];
echo "<hr/>";
}
// Nb d'enregistrement total
$sql = "SELECT COUNT(ID) as nbArt FROM produits WHERE MATCH (DESIGNATION, DESCRIPTION) AGAINST ('%$requete%' IN BOOLEAN MODE)";
$req = mysql_query($sql) or die(mysql_error());
$data = mysql_fetch_assoc($req);
$nbArt = $data['nbArt'];
// Pagination
$nb_pages = ceil($nbArt/$pagination);
// Affichage
echo '<p class="pagination">'.pagination($page,$nb_pages).'</p>';
?>
</body>
</html>
Bonjour,
Je débute en php, et j'essaye de monter un [b]moteur de recherche[/b] avec [b]extraction de BDD[/b] (mysql / phpmyadmin). J'ai pondu un mix à partir des tuto/exemples... trouvés sur le net en essayant de comprendre comment ça fonctionne. Helas [color=#800000]mon système de pagination ne fonctionne pas[/color] depuis que j'ai tout passé en mode [color=#800000]FULLTEXT[/color] (Ajout d'index FULLTEXT dans la BDD, et script php normalement adapté (de ce que j'ai pu en comprendre)).
Observez le résultat : http://www.levac.fr/test/moteur.php Faites une recherche sur le mot-clé "cable", les résultats s'affichent en 1ere page OK..... la pagination s'affiche en bas OK... il existe bel et bien 3 pages pour les résultats du mot clé "cable" OK ........ MAIS dès qu'on clique sur la plage 2 ou 3 ... plus rien ! Il semblerait que lorsqu'on change de page, on perd le mot-clé...... mais pourquoiiii snif ?! Avez vous une idée, que vous pourriez m'expliquer le plus clairement possible s'il vous plait ?
Merci pour l'aide que vous pourrez m'apporter.
Ci-dessous l'image montrant les index fulltext crées :
[url=http://www.hostingpics.net/viewer.php?id=299477screena.jpg][img]http://img15.hostingpics.net/thumbs/mini_299477screena.jpg[/img][/url]
Puis voici mon code :
[code]
<!DOCTYPE html>
<html>
<head>
<title>Recherche</title>
<style>
.pagination {
line-height:2em;
}
.pagination a,
.pagination span {
padding:0.2em 0.5em;
}
.pagination a {
border:1px solid #9AAFE5;
color:#105CB6;
text-decoration:none;
}
.pagination a:hover {
border:1px solid #296BB5;
color:#000031;
}
.pagination .prevnext {
font-weight:bold;
}
.pagination span.disabled {
border:1px solid #DDDDDD;
color:#999999;
}
.pagination span.current {
border:1px solid #2E6AB1;
background-color:#2E6AB1;
color:#ffffff;
font-weight:bold;
}
</style>
</head>
<body>
<?
mysql_connect('host',bdd','pass'); //ce sont pas vrai log hein... inutile de montrer ça à tout le monde
mysql_select_db('bdd'); // idem, pas le vrai !
$requete = htmlspecialchars(str_replace(' ',' +',' '.$_POST['requete']));
print $requete;
function pagination($current_page, $nb_pages, $link='?page=%d', $around=3, $firstlast=1)
{
$pagination = '';
$link = preg_replace('`%([^d])`', '%%$1', $link);
if ( !preg_match('`(?<!%)%d`', $link) ) { $link .= '%d'; }
if ( $nb_pages > 1 ) {
// Lien précédent
if ( $current_page > 1 ) {
$pagination .= '<a class="prevnext" href="'.sprintf($link, $current_page-1).'" title="Page précédente">« Précédent</a>';
}
else {
$pagination .= '<span class="prevnext disabled">« Précédent</span>';
}
// Lien(s) début
for ( $i=1 ; $i<=$firstlast ; $i++ ) {
$pagination .= ' ';
$pagination .= ($current_page==$i) ? '<span class="current">'.$i.'</span>' : '<a href="'.sprintf($link, $i).'">'.$i.'</a>';
}
// ... après pages début ?
if ( ($current_page-$around) > $firstlast+1 ) {
$pagination .= ' …';
}
// On boucle autour de la page courante
$start = ($current_page-$around)>$firstlast ? $current_page-$around : $firstlast+1;
$end = ($current_page+$around)<=($nb_pages-$firstlast) ? $current_page+$around : $nb_pages-$firstlast;
for ( $i=$start ; $i<=$end ; $i++ ) {
$pagination .= ' ';
if ( $i==$current_page ) {
$pagination .= '<span class="current">'.$i.'</span>';
}
else {
$pagination .= '<a href="'.sprintf($link, $i).'">'.$i.'</a>';
}
}
// ... avant page nb_pages ?
if ( ($current_page+$around) < $nb_pages-$firstlast ) {
$pagination .= ' …';
}
// Lien(s) fin
$start = $nb_pages-$firstlast+1;
if( $start <= $firstlast ) { $start = $firstlast+1; }
for ( $i=$start ; $i<=$nb_pages ; $i++ ) {
$pagination .= ' ';
$pagination .= ($current_page==$i) ? '<span class="current">'.$i.'</span>' : '<a href="'.sprintf($link, $i).'">'.$i.'</a>';
}
// Lien suivant
if ( $current_page < $nb_pages ) {
$pagination .= ' <a class="prevnext" href="'.sprintf($link, ($current_page+1)).'" title="Page suivante">Suivant »</a>';
}
else {
$pagination .= ' <span class="prevnext disabled">Suivant »</span>';
}
}
return $pagination;
}
// Numero de page (1 par défaut)
if( isset($_GET['page'])){
$page = $_GET['page'];
}
else {
$page = 1;
}
// Nombre d'info par page
$pagination = 10;
// Numéro du 1er enregistrement à lire
$limit_start = ($page-1)*$pagination;
$sql = "SELECT * FROM produits WHERE MATCH (DESIGNATION, DESCRIPTION) AGAINST ('%$requete%' IN BOOLEAN MODE) ORDER BY id DESC LIMIT $limit_start, $pagination";
$resultat = mysql_query($sql);
while( $donnee = mysql_fetch_assoc($resultat) ){
?> <a href="<? echo $donnee['LIEN']; ?>"> <? echo "<h4>{$donnee['DESIGNATION']}</h4>"; ?></a><?
echo $donnee['DESCRIPTION'];
echo "<hr/>";
}
// Nb d'enregistrement total
$sql = "SELECT COUNT(ID) as nbArt FROM produits WHERE MATCH (DESIGNATION, DESCRIPTION) AGAINST ('%$requete%' IN BOOLEAN MODE)";
$req = mysql_query($sql) or die(mysql_error());
$data = mysql_fetch_assoc($req);
$nbArt = $data['nbArt'];
// Pagination
$nb_pages = ceil($nbArt/$pagination);
// Affichage
echo '<p class="pagination">'.pagination($page,$nb_pages).'</p>';
?>
</body>
</html>
[/code]