+ Reply to Thread
Results 1 to 3 of 3

Thread: Grouping results from array by page number.

  1. #1
    learning_brain is offline x10 Sophmore learning_brain is an unknown quantity at this point
    Join Date
    Apr 2010
    Location
    UK, Midlands
    Posts
    170

    Grouping results from array by page number.

    You all know the old problem... 1000's of results and 1 html page...

    This is easily resolved if my results were direct from a MySQL result, using LIMIT, but I can't do that....

    My results are contrived from a full MySQL result, scoring each result relevance to the search string/s and adding to an array with the relevance value. This is then sorted by relevance and echo'd from the array.

    I want to split the results into page sets, using array_slice($array,$startRow,$endRow), and then use a seperate navigation link with the desired page number which will pass a page number to the url....which is almost there but not quite...

    I also have an additional problem. I don't think I can use foreach because I need to specify each row number in incremental order to achieve this.

    So.....this is where I am now..

    PHP Code:
    <?php if(isset($processed_array)){
        
                
    $maxRows 18;//specify total rows per page
                
    $pageNum 0;//initiate page number
                
    if (isset($_GET['pageNum'])) {//if page number exists in url
                      
    $pageNum $_GET['pageNum'];//page number is the number in url
                
    }
                
    $startRow $pageNum $maxRows;// start record on specified page is page number times max rows per page
                
    $endRow $startRow $maxRows;
                
    $totalPages ceil($count_processed_array $maxRows);
                
                
    //split into page sets
                
    $part_processed_array array_slice($processed_array$startRow$endRow);
                
                
    $count_part_processed_array count($part_processed_array);
            
                
               
                for (
    $row $startRow;$row $endRow$row++) {
                
    ?>
                
                    <echo results from $part_processed_array)......>

                <?php
                
    }
                
            } else {
            
                echo 
    "<hr/>No Results";
            
            }
    //end if(isset($processed_array)){
    The pageNum and startRow work a treat but the endRow is always startRow + whatever the rows per page is.....which is wrong. I can't get my head round how to manage this if,say, there are 45 result rows on a page of e.g. 50 loops.

    I'm also not sure my totalPages is right... :S
    Last edited by learning_brain; 06-23-2010 at 03:41 PM.

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Grouping results from array by page number.

    Quote Originally Posted by learning_brain View Post
    I also have an additional problem. I don't think I can use foreach because I need to specify each row number in incremental order to achieve this.
    Though foreach only handles entire arrays, you can combine it with array_slice as you do and you can process a single section of an array just fine.

    PHP Code:
    foreach (array_slice($items$startRow$rowsPerPage) as $i => $item) {
      
    $rowNum $i+$startRow;
      ...

    Note: the name $maxRows is misleading; $rowsPerPage is more accurate. Also, array_slice takes the offset & length of the slice, not start & end. Third, array_slice doesn't preserve indices, though this may not matter for your code.

    Of course, there's nothing wrong with using a for loop rather than a foreach loop. Personally, I prefer for in cases like this, as it's readable and the array_slice will use more memory.

    Quote Originally Posted by learning_brain View Post
    The pageNum and startRow work a treat but the endRow is always startRow + whatever the rows per page is.....which is wrong. I can't get my head round how to manage this if,say, there are 45 result rows on a page of e.g. 50 loops.
    I'm not sure of the problem you're facing. With array_slice, it shouldn't matter if there aren't enough items in the array to produce the requested length; it will simply stop at the end. array_slice(range(0,10), 5, 100); will give you range(5,10). Outside of array_slice, you can use min() to set the upper bound:
    PHP Code:
    $fromRow $startPage $rowsPerPage;
    $upto min($fromRow $rowsPerPagecount($rows));
    for (
    $i=$fromRow$i $upto; ++$i) {
        ...

    Quote Originally Posted by learning_brain View Post
    I'm also not sure my totalPages is right... :S
    Then test it. In general, figuring out all the important edge cases to test can take some work, but here testing 0, 10, 20 and 30 results at 20 results per page should be sufficient.


    Considering your search implementation, you should cache search results so you don't need to re-run the search for every page of results.
    Last edited by misson; 06-23-2010 at 05:28 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  3. #3
    learning_brain is offline x10 Sophmore learning_brain is an unknown quantity at this point
    Join Date
    Apr 2010
    Location
    UK, Midlands
    Posts
    170

    Re: Grouping results from array by page number.

    Perfect - solved it now.

    I had two problems. First, the array_slice can preserve indices.. e.g. array_slice($array, $startRow, $pageRows, true); problem solved.

    My second was the stupid misunderstanding that the slice uses start/stop, as opposed to your suggestion of start, length.

    The for loop was giving me a problem becasue it was always looping through the $pageRows amount, which was always fixed. I have changed this now to counting the amount of rows in each sliced portion.

    I've also managed to create a page navigation with the same system..... sweet!

    This has made my day!
    Last edited by learning_brain; 06-24-2010 at 04:20 PM.

+ Reply to Thread

Similar Threads

  1. Interaction design: When to check a grouping checkbox
    By Tarzan in forum Graphics & Webdesign
    Replies: 1
    Last Post: 05-28-2010, 09:55 PM
  2. Replies: 4
    Last Post: 04-23-2010, 02:38 AM
  3. Recursive PHP array to Javascript array/object
    By Veridis in forum Programming Help
    Replies: 0
    Last Post: 04-16-2008, 02:55 AM
  4. Custom Entropy Search Results Page
    By 007bond in forum The Marketplace
    Replies: 3
    Last Post: 12-09-2007, 05:58 AM
  5. Custom Entropy Search Results Page
    By 007bond in forum Scripts & 3rd Party Apps
    Replies: 4
    Last Post: 12-03-2007, 05:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers