Split array and display as seperate pages

Discussion in 'PHP' started by webdesigner, Oct 13, 2006.

  1. #1
    Hello,

    I currently have a php page set up that grabs filenames from a directory and stores them into an array so that i can sort them alphabetically and then display. This works fine.

    However, i need to now be able to split the array into chunks of 12 and display them on seperate pages automatically adding next and previous pages.

    Surely it cant be that hard but i just cant get it to work. Please help!

    Thanks in advance.
     
    webdesigner, Oct 13, 2006 IP
  2. Big 'G'

    Big 'G' Member

    Messages:
    89
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    48
    #2
    well i was gonna sugest using array_slice to split the arrays but this late at night my head just doesnt seem to want to work function keeps failing
     
    Big 'G', Oct 13, 2006 IP
  3. Big 'G'

    Big 'G' Member

    Messages:
    89
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    48
    #3
    OKay it got the better of me hopefully you can work with this
    
    //Quick Debug				
    function pre($x){
    	echo "<pre>";
    	print_r($x);
    	echo "</pre>";
    }
    $my_array=array("","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","");
    $x=0; //Inital Offset for count
    $i=1; //Inital Page Number
    //display
    	if(isset($_GET['offset'])){
    	echo "Page Display";
                    //Your display function here
    		pre(array_slice($my_array,$_GET['offset'],12,true));
    	echo "End display";
    	}
    //Pagation
    	echo "<br />pages";
    while($x <count($my_array)){
    	//pre(array_slice($my_array,$x,12,true));
    	echo "<a href='?offset=$x'>[page$i]</a>";
    	$x=$x+12; //Increase by array block req
    	$i++; //Increase page # 
    }
    
    
    PHP:
     
    Big 'G', Oct 13, 2006 IP
  4. streety

    streety Peon

    Messages:
    321
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #4
    What version of php are you using? The preserve keys option in the array slice function has only been available since PHP 5.0.2

    Getting rid of the true value it seems to work fine for me and if I switch to PHP 5 it works fine with the TRUE value.
     
    streety, Oct 13, 2006 IP
  5. harlequeen

    harlequeen Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I followed the tutorial below and was able to make my website paginate and have the previous and next links, and I am an absolute beginner. It was a very helpful tutorial

    http://www.phpfreaks.com/tutorials/73/0.php
     
    harlequeen, Oct 19, 2006 IP
  6. webdesigner

    webdesigner Well-Known Member

    Messages:
    489
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    110
    #6
    Thanks for the replies and help but i still cant figure it out. If anyone would be willing to help me, PM me and i can send you code or chat on msn.

    $5 up for grabs.
     
    webdesigner, Oct 31, 2006 IP
  7. webdesigner

    webdesigner Well-Known Member

    Messages:
    489
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    110
    #7
    All done and dusted now... Thanks!
     
    webdesigner, Oct 31, 2006 IP