CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   Paginating An Array (http://www.codingforums.com/showthread.php?t=266338)

JordanMRichards 06-26-2012 03:27 PM

Paginating An Array
 
Hello everyone.
I am new to this forum.

I'm not sure how I'd paginate an array. I've worked out the $start and $limit. But I'm just not sure how I'd put that into my foreach loop.

May I stress that I've already handled displaying the links to the page etc.

All I need to do is implement pagination into an array of data.

Here's an example of my situation.

PHP Code:


    
if($got_item_run mysql_query($get_items_q)){
            if(
$item_data mysql_fetch_assoc($got_item_run)) {
            
$item_list $item_data['catalog_items'];
            
$items2 explode(',',$item_list);
            
$hasItem false;
    
                  foreach(
$items as $curr_item) {

                
$curr_items explode('&'$curr_item);
                
$array_length count($curr_items);
                    if(
$array_length>=2) { 

I've made a pagination with mySQL because the "LIMIT" handled a lot of it. But you can't simply put "LIMIT $start, $limit" in a foreach loop.

I hope you understand my question.

Examples of your method of paginating an array in this situation would be appreciated.

Thank you.

DrDOS 06-26-2012 03:47 PM

This is where you need a for loop and not a for each loop. Then you just use arithmetic modulus to initiate the next page. I use for loops almost exclusively because they're simple, controlable and dependable, both in php and javascript.

JordanMRichards 06-26-2012 03:50 PM

Quote:

Originally Posted by DrDOS (Post 1245243)
This is where you need a for loop and not a for each loop. Then you just use arithmetic modulus to initiate the next page. I use for loops almost exclusively because they're simple, controlable and dependable, both in php and javascript.

I appreciate your feedback there DrDOS. However, the foreach loop is there to deal with the Array DATA. I can't use a for loop to do that can I?

If you could give me an example of such a for loop in my script I would really appreciate it. (I am 14 years of age and sometimes It's good to have an example just to make it "stick").

Thank you.

DrDOS 06-26-2012 05:26 PM

If you know the count of the array then you can use a for loop. I happen to have the code for a for loop right in my Clipboard.
PHP Code:

for ( $i $i count($curr_items) ; $i++ )
{
if(
$i%24==0Start a new page.
Whatever your are doing goes here.


Starts a new page every 24th item.

JordanMRichards 06-26-2012 05:31 PM

Thanks, but how can I use my $limit and $start variables? Because it gets the $_GET['p'] from the url etc

PHP Code:

        if($got_item_run mysql_query($get_items_q)){
            if(
$item_data mysql_fetch_assoc($got_item_run)) {
            
$item_list $item_data['catalog_items'];
            
$items2 explode(',',$item_list);
            
$hasItem false;

                  foreach(
$items2 as $curr_item) {
    for ( 
$i $i count($curr_item) ; $i++ )
{
if(
$i%1==0) {

                
$curr_items explode('&'$curr_item);
                
$array_length count($curr_items);
                    if(
$array_length>=2) {
                    
$item_id $curr_items[$start];
                    
$item_serial $curr_items[$start+1];
                        if (
itemExists($item_id)) {
                        
$item_name getItemById($item_id'name');
                        
$item_type getItemById($item_id'type');
                        
$item_gender getItemById($item_id'gender');
                        
$item_img getItemById($item_id'display_image');
                        echo 
$item_name;
                        echo 
$item_serial


JordanMRichards 06-26-2012 05:40 PM

Here's more of my code.

It's getting really stressful now. I've been trying to do this for ages.

PHP Code:

//  ====================================================== //
//  ===================Pagination========================= //
//  ====================================================== //
//  ====================================================== //
    
$adjacents 1;
    
$total_pages 5;
    
$total_pages $total_pages[num];
    
$limit 1;     
    
$limit $limit 2;                        
    
    if(
$page
        
$start = ($page 1) * $limit;             
    else
        
$start 0;                                
    
    
/* Get data. */
    
$get_items_q $get_items_q." LIMIT $start, $limit";
    
    
    
/* Setup page vars for display. */
    
if ($page == 0$page 1;                    
    
$prev $page 1;                            
    
$next $page 1;                            
    
$lastpage ceil($total_pages/$limit);        
    
$lpm1 $lastpage 1;                        
    
    
$pagination "";
    if(
$lastpage 1)
    {    
        
$pagination .= "<div class=\"pagination\">";
        
//previous button
        
if ($page 1
            
$pagination.= "<a href=\"$prev_page&p=$prev\">◄ previous</a>";
        else
            
$pagination.= "<span class=\"disabled\">◄ previous</span>";    
        
        
//pages    
        
if ($lastpage + ($adjacents 2))    //not enough pages to bother breaking it up
        
{    
            for (
$counter 1$counter <= $lastpage$counter++)
            {
                if (
$counter == $page)
                    
$pagination.= "<span class=\"current\">$counter</span>";
                else
                    
$pagination.= "<a href=\"$prev_page&p=$counter\">$counter</a>";                    
            }
        }
        elseif(
$lastpage + ($adjacents 2))    //enough pages to hide some
        
{
            
//close to beginning; only hide later pages
            
if($page + ($adjacents 2))        
            {
                for (
$counter 1$counter + ($adjacents 2); $counter++)
                {
                    if (
$counter == $page)
                        
$pagination.= "<span class=\"current\">$counter</span>";
                    else
                        
$pagination.= "<a href=\"$prev_page&p=$counter\">$counter</a>";                    
                }
                
$pagination.= "...";
                
$pagination.= "<a href=\"$prev_page&p=$lpm1\">$lpm1</a>";
                
$pagination.= "<a href=\"$prev_page&p=$lastpage\">$lastpage</a>";        
            }
            
//in middle; hide some front and some back
            
elseif($lastpage - ($adjacents 2) > $page && $page > ($adjacents 2))
            {
                
$pagination.= "<a href=\"$prev_page&?p=\">1</a>";
                
$pagination.= "<a href=\"$prev_page&p=2\">2</a>";
                
$pagination.= "...";
                for (
$counter $page $adjacents$counter <= $page $adjacents$counter++)
                {
                    if (
$counter == $page)
                        
$pagination.= "<span class=\"current\">$counter</span>";
                    else
                        
$pagination.= "<a href=\"$prev_page&p=$counter\">$counter</a>";                    
                }
                
$pagination.= "...";
                
$pagination.= "<a href=\"$prev_page&p=$lpm1\">$lpm1</a>";
                
$pagination.= "<a href=\"$prev_page&p=$lastpage\">$lastpage</a>";        
            }
            
//close to end; only hide early pages
            
else
            {
                
$pagination.= "<a href=\"$prev_page&?p=\">1</a>";
                
$pagination.= "<a href=\"$prev_page&p=2\">2</a>";
                
$pagination.= "...";
                for (
$counter $lastpage - (+ ($adjacents 2)); $counter <= $lastpage$counter++)
                {
                    if (
$counter == $page)
                        
$pagination.= "<span class=\"current\">$counter</span>";
                    else
                        
$pagination.= "<a href=\"$prev_page&p=$counter\">$counter</a>";                    
                }
            }
        }
        
        
//next button
        
if ($page $counter 1
            
$pagination.= "<a href=\"$prev_page&p=$next\">next ►</a>";
        else
            
$pagination.= "<span class=\"disabled\">next ►</span>";
        
$pagination.= "</div>\n";        
    }



//  ====================================================== //
//  ====================================================== //
//  ====================================================== //
//  ====================================================== //


        
if($got_item_run mysql_query($get_items_q)){
            if(
$item_data mysql_fetch_assoc($got_item_run)) {
            
$item_list $item_data['catalog_items'];
            
$items2 explode(',',$item_list);
            
$hasItem false;


    for ( 
$i $i count($items2) ; $i++ )
{
if(
$i%2==0) {

                
$curr_items explode('&'$curr_item);
                
$array_length count($curr_items);
                    if(
$array_length>=2) {
                    
$item_id $curr_items[$start];
                    
$item_serial $curr_items[$start+1];
                        if (
itemExists($item_id)) {
                        
$item_name getItemById($item_id'name');
                        
$item_type getItemById($item_id'type');
                        
$item_gender getItemById($item_id'gender');
                        
$item_img getItemById($item_id'display_image');
                        echo 
$item_name;
                        echo 
$item_serial;

        if (
$gender=='boy') {
        
$gendericon 'Images/boy_icon.png';
        }else if (
$gender=='girl') {
        
$gendericon 'Images/girl_icon.png';
        }else{
        
$gendericon 'Images/no_gender_icon.png';
        }
        if(
$limited==1) {
            
$total_available $total_sold."/".$total_av;
            
$item_special 'Images/limited icon_over.png'// Img source to "LIMITED" icon
        
}else{
        
$item_special 'Images/item_transparent.png'//Transparent image
        
}
        
        if(
$item_type=='shoes') {
            
$item_type 'Shoes';
        }
        if(
$item_type=='trousers') {
            
$item_type 'Lower Wear';
        }
        if(
$item_type=='t_shirt') {
            
$item_type 'T-shirt|Vest';
        }
        if(
$item_type=='top') {
            
$item_type 'Top';
        }
        if(
$item_type=='face') {
            
$item_type 'Face';
        }
        if(
$item_type=='hair') {
            
$item_type 'Hair';
        }
        if(
$item_type=='acc') {
            
$item_type 'Accessory';
        }
        if(
$item_type=='hat') {
            
$item_type 'Hat';
        }
        
}  
                            }
                    }
                }
}




All times are GMT +1. The time now is 04:07 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.