0

I'm trying to create a pagination system in PHP. However, my products are loaded from exact so I can't use SQL. What i was thinking is that i would have to put all the items in an array, count that and then create the pagination system. The pagination system isn't the problem, but i can't seem to find a way to count my items properly using the above stated method.

Do note that exact only gives 60 results, so i had to create something that it retrieves all the results which i implemented in the code below.

My current code:

        echo "<table order='0' width='90%'>";
    $offset = 0;
    $run=true;
    $search="";
    $row_count = 0;

            while($run== true){
            $glas = getGLAccounts($search,$offset);
            $count2 = 0;
            $offset=$offset+30;

            foreach($glas as $gla){
              $row_count++;
                if ($row_count==1)
          echo "<tr>";
          echo( "<td><a href='pitem.php?item=".$gla['Code']."'><br/>".
          "<img src='../style/images/plant.png' width='50' height='50' /><br/>".
          "".$gla['Code']."</a></br>
          ".$gla['Description']."<br/>
          &euro;".$gla['CostPriceStandard']."<br />
          <a class='toevoeg' href='catalogus.php?art=".$gla['Code']."'>Toevoegen</a></td>"
          );
                $count2=$count2+1;
              if ($row_count==3) {
          echo "</tr>";
          $row_count=0;
              }
            }
            if($count2<30) {
          $run=false;
        }
        }
        if ($row_count>0) {
      echo "</tr>";
    }
    echo "</table>";

$glas gets all the items but when i count glas. It counts 60 items, but keeps putting 60 on my screen which is probably because i count in a while loop. In the foreach loop it puts a bunch of 1s.

Now my question is, what is the proper way to put my items in an array and count them? And where to put it?

vaxzz
  • 88
  • 8
  • My other question was put on hold- and now you mark this as duplicate. How come? My question hasn't been answerd yet and you do this to me. – vaxzz Dec 06 '16 at 10:51
  • Why do you reinvent the wheel? Look on https://packagist.org/ for pagination libraries, one of the first to pop up is [pagerfanta](https://packagist.org/packages/pagerfanta/pagerfanta) which provides an ArrayAdapter that you could just be using. – k0pernikus Dec 06 '16 at 10:51
  • What is in the `getGLAccounts` function? Seems like `$glas` contains already limited array, so you can't access its original length. – Patrik Krehák Dec 06 '16 at 10:52
  • getGLAaccounts is a function that gives me only the specifc items from the exact database. It's from an API I'm using. – vaxzz Dec 06 '16 at 10:53

0 Answers0