简体   繁体   中英

Ajax pagination on page load?

Ok first I'll tell how this SHOULD work: I have a page with image links down the side, click on an image and the info for that link appears in another div. I use jquery/ajax to post the links id to a php file and return that data to the chosen div. The links should paginate so that 4 display at a time.

This is what IS happening: The post part is ok, when i click a link the correct data is being displayed in the chosen div. I dont know how to make the links div paginate though. i need them paginated when the page loads, right now when the page loads all the links are shown, then when i click on a link the correct amount (4) are shown!

This is my html with the 2 divs:

<div class="dogsrehomeandrehomed">
<?php
include 'inc/connect.php';
$q = mysqli_query($link, "SELECT filename, id, name, age, sex  FROM gallery WHERE 
gallery = 1 ORDER BY id DESC") or die (mysql_error());
while($row = mysqli_fetch_array($q)){
$data = $row['filename'];
$file = substr($data, strpos($data, "/") + 1);
echo"<div class='homedogs'>",
"<a href={$row['id']} class='dogchoice'>",
"<img class='nailthumb-container3' src='$file' alt='{$row['name']}. Image' />",
"</a>",
"<br />",
'NAME: ',$row['name'],"<br />",'AGE: ',$row['age'],"<br />",'SEX: ',$row['sex'],
"</div>";
}
?>
</div>

<div class="dog">
<?php
include 'inc/connect.php';
$q = mysqli_query($link, "SELECT *  FROM gallery WHERE gallery = 1 ORDER BY id DESC 
LIMIT 1") or die (mysql_error());
while($row = mysqli_fetch_array($q)){
$data = $row['filename'];
$file = substr($data, strpos($data, "/") + 1);
echo"<div class='rehomediv'>",
"<img class='nailthumb-container2' src='$file' alt='{$row['name']}. Image' />","<br   
/>",
"<div class='nameagesex'>",
'NAME: ',$row['name'],"<br />",'AGE: ',$row['age'],"<br />",'SEX: ',$row['sex'],
"</div>",
"<div class='description'>",
nl2br($row['description']),
"</div>",
"</div>";
}
?>          
</div>
<script src="js/dog.js"></script>

This is my dog.js file:

$('a.dogchoice').click(function(e) {
e.preventDefault();
var linkClass = $(this).attr("class");
var linkText = new String(this);
var categoryValue = linkText.substring(linkText.lastIndexOf('/') + 1);
var params = {};
params[linkClass] = categoryValue;  
$.post('inc/dogchoice.php', params, function(data) {
    var totalRecords = $(data).length;
    var pageSize = 4;
    var numOfPages = Math.ceil(totalRecords / pageSize);
    var i,
      pageLinks = '<div class="pageLinks">';
    for (i = 0; i < numOfPages; i++) {
      pageLinks += '<a href="#" onclick="showDogLinks(' + i + ');return false;">' + (i 
      + 1) + '<\/a> ';
    }
    pageLinks += '<\/div>';
    $('.dog').html(pageLinks + data);
    showDogLinks(0);
});

});

//function to slice up records into pages
function showDogLinks( pageNo ) { 
var perPage = 4; 
var start = pageNo * perPage; 
var end = start + perPage; 
$('.homedogs').hide().filter(function(index) { 
    return ( (index > (start-1)) && ( index < end ) ); 
} ).show(); 
}

Can anyone help? Thanks for looking

Ive sorted it. I just paginated the link container with php.

Thanks for looking..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM