Pagination algorithm with javascript

I have a javascript function which makes an ajax request to php controller method which returns a JSON encoded array.

function initGallery(offset) { if(offset === undefined) { var request_url = url+'avatar/gallery'; } else { var request_url = url+'avatar/gallery/'+offset; } $('#avatar_gallery').html('') $.get(request_url,function(data) { var dat = jQuery.parseJSON(data); //Build gallery $('#avatar_gallery').html('<div class="gallery_box"></div>'); $('.gallery_box').append('<div class="gallery_header">Your Avatar Gallery</div>'); $('.gallery_box').append('<div class="gallery_container"></div>'); $.each(dat.avatars,function(index,item) { $('.gallery_container').append( '<div class="gallery_item">'+ '<img src="'+item.avatar_src+'" id="'+item.avatar_id+'" onclick="avatar.view_avatar("/>'+ '</div>' ); }); $('.gallery_box').append('<div class="gallery_footer"></div>'); $('.gallery_footer').html('<div class="gallery_pagination"><div>'); }); }

And this is my controller method

function gallery($offset= 0) { $limit = 12; $user_id = $this->session->userdata('user_id'); $data = $this->avatar_model->user_avatars($user_id,$limit,$offset); $avatars = array(); $count = $this->avatar_model->count_user_avatars($user_id); $pages = ceil($count/$limit); foreach($data as $key => $avatar) { $dat['avatar_id'] = $avatar->avatar_id; $dat['avatar_src'] = $avatar->avatar_small; $dat['create_date'] = time("d-m-Y",$avatar->create_date); $avatars[] = $dat; } $server_response['avatar_count'] = $count; $server_response['avatars'] = $avatars; echo json_encode($server_response); }

I dont really have an idea on how to paginate the data returned from the reques. Please point me in the right direction

-------------Problems Reply------------

It's easy. Add a class to your pagination link (you can use full_tag_open and full_tag_close config variable: <p class="pagination> and </p>). After that you can redefine the .pagination a click event (I'm using JQuery):

function () {
$(".pagination a").click(function(event){

I hope this helps you.

Category:php Views:3 Time:2011-10-20

Related post

  • Smart pagination algorithm 2008-10-02

    I'm looking for an example algorithm of smart pagination. By smart, what I mean is that I only want to show, for example, 2 adjacent pages to the current page, so instead of ending up with a ridiculously long page list, I truncate it. Here's a quick

  • Pagination algorithm working incorrectly 2010-02-19

    I've got a fairly simple pagination algorithm here but it's not working the way I'd like it to. Currently it's displaying like this 1 2 3 ... 33 34 35 [36] 37 38 ... 47 48 49 50 When it should be displaying like this 1 2 3 ... 33 34 35 [36] 37 38 39

  • How can I improve this PHP pagination algorithm? 2010-09-19

    I'm working on a pagination algorithm in PHP. I can guess that it needs room for improvement, so I'd like some thoughts on how to improve it, be it cleaning up the code itself, from a UI/UX standpoint, or anything else you can think of. The algorithm

  • Composition algorithm in Javascript to return all possible compositions of a number 2011-12-04

    Edit: I need to implement composition algorithm in Javascript, where the result would be the same as the figure on the right in Wikipedia.With a given number (n), the function would be able to return all possible separations, e.g. 2: [1,1], [2] (2 se

  • uudecode algorithm in javascript 2012-02-07

    i'm looking for a implementation of uudecode algorithm in javascript, anybody knows if any framework or native javascript implements this funcionality? I'm trying to find the solution in the web and i cant find anything, if any can helps i would grea

  • Which Regular Expression Algorithm does Javascript use for Regex? 2009-04-07

    I was reading this article today on two different regular expression algorithms. According to the article old Unix tools like ed, sed, grep, egrep, awk, and lex, all use what's called the Thompson NFA algorithm in their regular expresssions... Howeve

  • Algorithm of JavaScript "sort()" Function 2010-08-06

    Recently when I was working with JavaScript "sort()" function, I found in one of the tutorials that this function does not sort the numbers properly. Instead to sort numbers, a function must be added that compares numbers, like the following code:-

  • Bresenham algorithm in Javascript 2011-01-12

    I need a fast algorithm for calculating coordinates for a line between two points. I tried to find good JavaScript Bresenham implementation, but there are too many and quite confusing publications. In wikipedia - here the fastest and most simple form

  • What is the best auto-suggest search algorithm for javascript 2011-02-24

    Say I have an object: var names = ["john", "jane", "al", "mary", "zane" ... 1000+ Names] I want to create an auto-suggest to search these names. What's the most efficient way of doing this? I've read creating a trie or ternary data structure is best,

  • fixing pagination limit on javascript 2012-02-15

    first of all, to be clear i am a newbie with javascript and jquery. now, i am using a javascript file to paginate inside a div. the script works fine, but if there is a lot of data then i need to limit the paginating links with a "next" and a "previo

  • Pagination with Jquery/Javascript/Ajax and PHP/Mysql 2011-02-08

    Does anyone know of a link to a site that has a tutorial/code on good way to paginate information coming from a database? (without page refresh) I have spent the better part of the day looking for a site that has what I need. Most are dealing with st

  • Pagination algorithm from mutil-array? 2011-02-15

    Have an $A is the data for pagination: $A = array( 0=>array( 0=>1, 1=>2 ), 1=>array( 0=>3, 1=>5, 2=>2 ), 2=>array( 0=>3, 1=>1, 2=>6, 3=>6 ) ); I have a function // page to show (1-indexed) // number of items to sho

  • "Bucket Fill" algorithm in Javascript or coffeescript 2012-02-16

    i'm writing a little coffeescript/js app that allows user to design icons ( 16x16 pixels or 32X32 pixels ). The icon is actually a 2 dimensional array with color cells. A cell can have a color or be empty. I want the user to be able to fill blank cel

  • Limit pagination links number javascript 2012-03-02

    I have this pagination script which works perfectly except for this little issue. so now I want to limit the navigation numbers. but I can't pass the (this.currentPage) window.thisPager= new Pager('comments', 10); thisPager.init(); thisPager.showPage

  • To color the image using flood fill algorithm in javascript 2012-04-10

    Am trying to color the specific portion of the image but the color applied for full image.. can anyone help me? --------------Solutions------------- You should reconsider the structure of your code I got lost when I sear

  • Does this JavaScript code follow the Midpoint Displacement algorithm? 2011-03-07

    I'm trying to use the Midpoint Displacement Algorithm using JavaScript and canvas as recommended on The code below generates points where the array index is the x position and its value is the y position. var createTerrain

  • Porters Stemming Algorithm Javascript, How to 2011-08-20

    Below is Porters Stemming Algorithm for JavaScript which I have taken from here: I would like to be able to use the algorithm by simply calling: var stemmed_word = porter_stemming_alg( "some_word_to_st

  • How to implement php-mysql data pagination using data providers 2011-06-16

    Ok. i had created another question about this but i guess i had complicated it a bit.Ill try to simplify my problem here: Suppose you have this code: <?php if($_POST['page']){ $page = $_POST['page']; $cur_page = $page; $page -= 1; $per_page = 15;

  • Algorithm / pseudo-code to create paging links? 2008-09-04

    Can someome provide code or pseudo-code for how the paging links on StackOverflow are generated? I keep racking my brain but can't think of a decent way to build the dynamic links that always show the 2 pages around the current, plus the first and la

Copyright (C), All Rights Reserved.

processed in 0.084 (s). 11 q(s)