user718229
user718229

Reputation: 573

paginate array from model in view php mvc

I am making a search form that pulls user data in multi-dimensional arrays from the model, I cant seem to figure out how to paginate it, in an MVC style set up. The model sends the array of unknown length, since there are several search fields which are all optional, to the view. It is $_POST data so I would lose the data when I refresh the page.

Here is a simplified version of array...

Array
(
[0] => Array
    (
        [id] => 1

        [username] => Rebequa

        [city] => arlington

        [state] => va

    )

[1] => Array
    (
        [id] => 2

        [username] => steve

        [city] => 

        [state] => 

    )

[2] => Array
    (
        [id] => 6

        [username] => Rebequa2

        [city] => arlington

        [state] => va

    )
)

Upvotes: 0

Views: 1194

Answers (2)

tereško
tereško

Reputation: 58444

First of all, you should not read data directly from $_POST. Instead you should either store the conditions in $_SESSION or cache the results of your search, and the redirect user to "results".

If you play around with your form and the F5 key in Firefox or Internet Explorer, you will understand, what sort of issues it causes.

I would recommend to store the conditions in $_SESSION and retrieve total number of results (which also get stored in $_SESSION) before you perform redirect.

Then for each page you just amend the conditions, and generate the list of results.


BTW, if you are really trying to implement MVC design pattern, you should be aware that model layer must not have concept of Page. That would be part of presentation logic. The view, when retrieving information from model layer, should request give me 1..10 entry, give me 31..40 entry and so on.

View would then take this information from model layer and render the response using as many templates as required.

Another thing you must understand is that both $_SESSION and forms of cache are mediums of storage. Thy should not directly accessible only from withing model layer.

I would recommend to avoid caching of search results because it's only useful in cases when you have small amount of data, which takes long time to retrieve. Caching an array with thousands of elements would be counter-productive.

Upvotes: 1

Sammaye
Sammaye

Reputation: 43884

It is hard to truely understand you but array_chunk might work to split the array into "chunks" or "pages":http://php.net/manual/en/function.array-chunk.php

For example to get an array of making a pagesize of 1 for your array:

array_chunk($my_model_array, 1);

Which will output something similar to:

Array
(
[0] => Array(
    [0] => Array
    (
        [id] => 1

        [username] => Rebequa

        [city] => arlington

        [state] => va

    )
), 
[1] => Array(
    [0] => Array
    (
        [id] => 2

        [username] => steve

        [city] => 

        [state] => 

    )
)

So each element within the root array will now equal a page and then you can scroll over those "pages". You can then place each of the root elements into a div and use JS to toggle between pages.

Edit

Of course the best method here is to use AJAX to get your results and do and AJAX list of users from the search form or use $_GET since this data is not setting DB models etc it is merely being used to grab users from the models like a search query.

Upvotes: 1

Related Questions