Bonjour à tous,
Je suis actuellement en train d'essayer de faire un pagination en AJAX. J'ai suivi le tutoriel de Grafikart, il marche correctement cependant sur mon site, j'utilise l'url rewriting pour index.php?p=index&q=1 (p correspond à la page en cours et q la pagination) qui donne www.example.com/1.
Mon problème est le suivant : quand j'appuie pour naviguer sur la deuxième page (donc q=2), l'ensemble du site se recharge une deuxième fois. J'ai fait des recherches mais rien fonctionne. Voilà mon code :
Ajax :
$(document).ready(function(){
$(".pagination a").click(function(){
page = $(this) .attr("href");
$.ajax({
url: "/"+page,
cache: false,
success:function(html){
afficher(html);
},
error:function(XMLHttpRequest, textStatus, errorThrown){
alert(textStatus);
}
})
return false;
});
});
function afficher(data){
$(".t").fadeOut(500, function(){
$(".t").empty();
$(".t").append(data);
$(".t").fadeIn(1000);
})
}
Pagination :
<div class = "t">
<?php
$req = $bdd->prepare("SELECT COUNT(id) as nbArt FROM web");
$req->execute();
$data = $req->fetch();
$nbArt = $data'nbArt'];
$perPage = 8;
$nbPage = ceil($nbArt/$perPage);
if(isset($_GET'q'])){
intval($_GET'q']);
}
if(isset($_GET'q']) && $_GET'q']>0 && $_GET'q']<=$nbPage){
$cPage = intval($_GET'q']);
}
else{
$cPage =1;
}
$req = $bdd->prepare("SELECT id, titre, lien, site, date_creation FROM web ORDER BY date_creation DESC LIMIT ".(($cPage-1)*$perPage).",$perPage");
$req->execute();
while($data = $req->fetch()){
?>
<a href = "<?php echo $data'lien']; ?>" title = "<?php echo $data'titre']; ?>" onclick="window.open(this.href); return false;"><div class = "web_1">
<h2><?php echo stripcslashes($data'titre']); ?></h2>
<p>Zlatané le <?php echo date('d/m/y - H:i', strtotime($data'date_creation'])); ?> par <?php echo stripslashes($data'site']); ?></p>
</div></a>
<?php
}
$req->closeCursor();
?>
</div>
<div class = "pagination">
<?php
for($i=1;$i<=$nbPage;$i++){
echo " <p class = 'pagination_none'><a href=\"http://www.prozlatan.fr/$i\">$i</a> |</p>";
}
?>
.htaccess :
RewriteRule ^([a-zA-Z\-]*)$ index.php?p=$1 [L]
RewriteRule ^([0-9\-]*)$ index.php?p=index&q=$1 [L]
Merci de votre lecture, à bientôt ! :)