Jump to content

loop which iterates through an array in steps of 10


yanisdon

Recommended Posts

Hi,

I want to create a loop which iterates through an array in steps of 10

 

Let's assume we have an array.

Array
(
    [0] => Array
        (
            [title] => Title one
            [link] => http://www.mylink.com
            [description] => This is a test description
            [dot] => Array
                (
                    [source] => test source
                    [relevance] => 1.0
                    [metadata_score] => 0.3505949
                    [metadata_collection] => Test collection
                    [metadata_mimetype] => text/html
                    [metadata_size] => 0
                    [metadata_url] => http://www.test.com
                )

            [summary] => This is just a test summary
        )

    [1] => Array
        (
            [title] => Title one
            [link] => http://www.mylink.com
            [description] => This is a test description
            [dot] => Array
                (
                    [source] => test source
                    [relevance] => 1.0
                    [metadata_score] => 0.3505949
                    [metadata_collection] => Test collection
                    [metadata_mimetype] => text/html
                    [metadata_size] => 0
                    [metadata_url] => http://www.test.com
                )

            [summary] => This is just a test summary
        )
etc, etc..

 

So far so good.

 

Now let's assume the array contains aprox. 5000 items and this value would be represented by a variable called $totaValue

 

What I really like to be doing here is to loop through the array in steps of 10 and by doing that to create some sort of paging mechanism in order to avoid any impacts triggered by server timeout's etc.

 

Say I've got these values in $arr_param[]:

    [totalresults] => 200
    [startindex] => 1
    [itemsperpage] => 10 //$arr_param['itemsperpage'] 
    

 

So I'd need to create a loop or a function which loops through the entire array and iterates 10 times ($arr_param['itemsperpage']).

 

After that go back and procede with the next 10 iterations and so on.

 

In order to be able to achieve this I'd need to set the $arr_param[startindex] to 11, next time to 21 and next to 31 and so on.. :

 

How can this be done?

 

Any ideas?

 

Cheers

Link to comment
Share on other sites

Why do you need to create ALL of the pages?  Can't you just create one page at a time?

 

Then you can simply use $_GET variables (in the URL) to indicate which record should be the start and how many records it retrieves.

 

$startIndex = $_GET['start']
$items = $_GET['items']

...and generate a URL like

www.yoursite.com/yourpage.php?start=9&items=10

 

I hope that's the sort of idea you're after... if not... oh well.

Link to comment
Share on other sites

Thanks for the response.

Well, maybe I need to add that I don't want to generate a real paging mechanism.

 

A little bit of background.

 

My little app parses a remote RSS file with Magpie. Basically Magpie parses the content of a given RSS file into a PHP object.

So far so good.

 

The RSS file in question is being generated by a remote Server which is prone to timeout. The RSS file contains dynamic parameter for the total number of items and the pagesize which can be used to manipulate the $_GET parameter in order to bypass the timeout issue.

So again:

 

1) the array contains 5000 items.

 

2) start to loop through the array and get the first 10 items (1 -10)

 

3) do something with those 10 items..

 

4) now go back and get the next 10 items (11-20)

 

5) do something with those 10 items..

 

4) now go back and get the next 10 items (21-30)

 

and so on..

 

 

 

Cheers.

Link to comment
Share on other sites

That works:

<?php


// FUNCTION:

function paginate($array,&$offset,$perpage=10){
        $offset = $offset?$offset:0;
        if($offset >= count($array)) return FALSE;
        $page = array_slice($array,$offset,$perpage,TRUE);
        $offset+=$perpage;
        return $page;
}

// USAGE:

        // build a test array
        for($i=0;$i<=5000;$i++){
                $array[$i] = 'item '.$i;
        }
       
        // set your offset variable
        $offset = 1;

        // paginate it some to test it
        while( $a=paginate($array,$offset) ){
                print_r($a);
        } 
?>
/**
Array
(
    [1] => item 1
    [2] => item 2
    [3] => item 3
    [4] => item 4
    [5] => item 5
    [6] => item 6
    [7] => item 7
    [8] => item 8
    [9] => item 9
    [10] => item 10
)
Array
(
    [11] => item 11
    [12] => item 12
    [13] => item 13
    [14] => item 14
    [15] => item 15
    [16] => item 16
    [17] => item 17
    [18] => item 18
    [19] => item 19
    [20] => item 20
)
Array
(
    [21] => item 21
    [22] => item 22
    [23] => item 23
    [24] => item 24
    [25] => item 25
    [26] => item 26
    [27] => item 27
    [28] => item 28
    [29] => item 29
    [30] => item 30
)
Array
(
    [31] => item 31

etc, etc,.........
*/

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.