1
votes

Have the code bellow that result in more than 27k ids, so i would like to paginate the result to 100 IDS result per page.

function subtree($id, $parents)
{
    echo ' <tr><td> <b>Client No.:</b> </td><td> ';
    echo $id;
    echo ' </td></tr> ';
    if (isset($parents[$id])) {
        foreach ($parents[$id] as $child) {
            subtree($child, $parents);
        }
    }
} 

$parents is an array in the main sql query:

$id is the parent ID

when i call the function the result is all in the same page, what i would like to do now is paginate those results to 100 per page.

2
@PeeHaa, result is so huge in one page , so need to paginate the result to 100 per page.user3117183

2 Answers

0
votes

try this

$count = 0;
function subtree($id, $parents)
{
    echo ' <tr><td> <b>Client No.:</b> </td><td> ';
    echo $id;
    echo ' </td></tr> ';
    if (isset($parents[$id]))
        foreach ($parents[$id] as $child) {
            if ($count === 100) {
                break;
            }
            subtree($child, $parents);
            $count++;
        }
}

This will help you in displaying 100 results per page. Now you can create links to show the next 100 records in next page

0
votes

Improve your database routines. You will probably want to use a temporary table to help flatten and normalize your multi-dimentional data before attempting to paginate it. You will need to keep track of what page you're on and how big each page is,

If you're using mysql, use an auto generated id column to specify where you start AND id > 300 then add LIMIT 100 to say where you end. On oracle it would be AND RowNum between 300 and 400.

Your other option is using your recursive function to build a flat list of id's and store it in the session, and then run your pagination routine over that.

Your other-other option is to rewrite the recusion into a stack-and-loop structure so you know how far you've gone as you're going through it, specifying a start point for this page, running through it until you hit the start counter, emit data until you hit the end marker, and return.

The worst option, of course, would be to simply use globals to indicate how many things you have tried to print out and put an if ($count >= $start && $count <= $end) around the echo statements. Don't do that. but it would be very simple to do and probably work without thinking about it very hard and be horribly inefficient.