title: Pagination with unlimited nesting
body: <p>I have a mySQL table which stores categories and subcategories nested to unlimited level. The table structure is :</p>
  
  <pre><code>cat_id, parent_id, cat_name
     1       0       Ingredients
     2       1       Veg
     3       1       Non-veg
     4       3       Egg
     5       2       Potatoes
  </code></pre>
  
  <p>I want to show the complete list of these items in a PHP page in a tree view. So first the items that have parent id "0" will be shown and if they have child items the child elements will be shown with recursion to unlimited level. The output will be something like the following but in table format :</p>
  
  <pre><code>id         title
   1         Ingredients
     2         Veg
       5         Potatoes
     3         Non-veg
       4         Egg
  </code></pre>
  
  <p>I am able to achieve this with the help of multiple queries. First I get all the records with parent id = 0 and then I loop through all the items and inside the loop I check if there are child records present show them (recursively).</p>
  
  <p>But now I need to add pagination and show only 10 records per page.</p>
  
  <p>Is there a way to achieve pagination and dynamically show 10 records per page including the child and child-child records ?
  How will I calculate the total number of pages and get the records ? I will also have to add filters later in these type of tables.</p>