uccdev's avatar

Is it possible to paginate two different tables on the same view?

So I have a laravel project that fetches data from not one, but two API endpoints. API endpoints like Canvas, so I cannot use any DB::table queries or other tricks. It gets the data from a Courses endpoint as well as a Users endpoint. I've been requested to fit the data of both tables on the same view.

Displaying all the data is fine, I'm able to collect the data from both tables in my controller and parse it to the view:

return view('index')
  ->with("courseData", $courseData) //course Data
  ->with("cPages", $cPages) //course pages
  ->with("cRels", $cRels) //course references 
  ->with("cPageNums", $pageNums) //course page numbers
  ->with("userData", $userData) //user data
  ->with("uPages", $uPages) //user pages
  ->with("uRels", $uRels) //user references
  ->with("uPageNums", $uPageNums); //user page numbers

I'm also able to successfully paginate the Courses table.

if (isset($cPageNums)) {
      ?><a href="<?php echo $cPageNums[0]; ?>" rel="<?php echo $cRels[0]; ?>"><?php echo $cRels[0]; ?></a>
      <?php
        if ($cPages[1] != null ) {
          ?> <a href="<?php echo $cPageNums[1]; ?>" rel="<?php echo $cRels[1]; ?>"><?php echo $cRels[1]; ?></a>
        <?php
      } else { echo "Error"; }
        if ($cPages[2] != null) {
        ?>  <a href="<?php echo $cPageNums[2]; ?>" rel="<?php echo $cRels[2]; ?>"><?php echo $cRels[2]; ?></a>
        <?php
        }
        if ($cPages[3] != null) {
          ?> <a href="<?php echo $cPageNums[3]; ?>" rel="<?php echo $cRels[3]; ?>"><?php echo $cRels[3]; ?></a>
  <?php }
    } else {
      echo "No course pageNums set!";
    }

For comparison's sake, my User pagination code, which is only different in that it uses different variables:

if (isset($uPageNums)) {
        ?><a href="<?php echo $uPageNums[0]; ?>" rel="<?php echo $uRels[0]; ?>"><?php echo $uRels[0]; ?></a>
        <?php
      if ($uPages[1] != null ) {
        ?> <a href="<?php echo $uPageNums[1]; ?>" rel="<?php echo $uRels[1]; ?>"><?php echo $uRels[1]; ?></a>
      <?php
    } else { echo "Error"; }
      if ($uPages[2] != null) {
      ?>  <a href="<?php echo $uPageNums[2]; ?>" rel="<?php echo $uRels[2]; ?>"><?php echo $uRels[2]; ?></a>
      <?php
      }
      if ($uPages[3] != null) {
        ?> <a href="<?php echo $uPageNums[3]; ?>" rel="<?php echo $uRels[3]; ?>"><?php echo $uRels[3]; ?></a>
<?php }
  } else {
    echo "No user pageNums set!";
  }
  ?>

So what happens when I click the buttons these create in my view? Well if they're the Course buttons, they work perfectly fine. I move between all the pages I need to.

When I click the user ones? I move through the Course table instead. It's as if the page only recognises the one set, not the other. So I suppose I've two big questions.

  1. Is what I'm looking to do possible? Paginate between two tables on the same webpage? I'm aware it might be easier to simply have a separate view for each, but management would prefer this way.

  2. If it is possible, what can I do to accomplish that? Or what is the mistake I'm presently making?

Any help on this matter would be greatly appreciated.

EDIT: For further clarity, here is how I sort pagination. Canvas naturally paginates its data, so I try to retrieve its links directly from that. Then after extraction, I pass the data containing those onto the view.

    $explData = explode("\n",$resp); //Need the page links in order to move from data to data, since Canvas keeps all its data paginatable.

    $courseLinks = self::getLinks($explData); //gets links from data
    if (is_null($links)) {
      echo "exec'd data did not contain any links";
      return null;
    }
    $cPageLinks = explode(",", $links); //the actual strings in full. Array form.
    $cPages = array_fill(0, 4, null); //the page URL links
    $cRels = array_fill(0, 4, null); //references (i.e "current", "next", "prev", "last")
    $cPageNums = array_fill(0, 4, null); //amount of pages
    $i = 0;
    //loop to populate the above
    foreach($cPageLinks as $p) {  //get the important page details and store them in these arrays
      $cPages[$i] = AppHelper::instance()->get_string_between($cPageLinks[$i], "<", ">"); //helper method, returns raw string.
      $cRels[$i] = AppHelper::instance()->get_string_between($cPageLinks[$i], 'rel="', '"');
      $cPageNums[$i] = AppHelper::instance()->get_string_between($cPageLinks[$i], "?page=", "&");
      $i++;
}

This process is mirrored for getting the User endpoint's pages.

0 likes
8 replies
uccdev's avatar

Thanks for your reply jlr. I am not using JS Ajax. I use PHP Laravel with custom functions to get the pagination links from my API endpoint. Then in the View, I use basic HTML in two separate divs to display the data.

EDIT: Thank you for the link. It seems to refer to Eloquent pagination in general, which is quite different to how I do it. As someone who's new to using Eloquent pagination, could you suggest how I might use it?

uccdev's avatar

Also I've updated the original post. It now shows how I get the thread data. Just in case it's useful for understanding or giving feedback

jlrdw's avatar

Double check your variables in controller, ones not shown above, because it should work.

Perhaps a typo some where.

AND

Bravo to you for actually knowing how to write a custom paginator. It's scary how many folks new to laravel has never written pagination.

1 like
uccdev's avatar

Thank you jl. I need to finish work now but I will give them another look in the morning.

Also, thanks, that makes me feel much better :D

Snapey's avatar

you are just returning a page number in your links. You need to give these query parameters a name, eg, courses=3 rather than just 3

1 like
uccdev's avatar

I see, Snapey, thank you. What name exactly should I give them? Should the name match the div they are in? Or the name of the table they're referring to? Or the dataset, like courseData?

EDIT: I ask because I've modified my View with the following, where I put userData= value, and it unfortunately gave me a curl error:

  <?php //TODO Page Information. Configured for "v1/accounts/1/courses" endpoint, reconfigure to "v1/courses" endpoint
  //dd($cata);
  if (isset($uPageNums)) {
    ?><a href="<?php echo $uPageNums[0]; ?>" rel="<?php echo $uRels[0]; ?>"><?php echo $uRels[0]; ?></a>
    <?php
//      echo "<a href='" . $pageNum . "' rel='current'>Current</a>";
      if ($uPages[1] != null ) {
        ?> <a href="userData=<?php echo $uPageNums[1]; ?>" rel="<?php echo $uRels[1]; ?>"><?php echo $uRels[1]; ?></a>
      <?php
    } else { echo "Error"; }
      if ($uPages[2] != null) {
      ?>  <a href="userData=<?php echo $uPageNums[2]; ?>" rel="<?php echo $uRels[2]; ?>"><?php echo $uRels[2]; ?></a>
      <?php
      }
      if ($uPages[3] != null) {
        ?> <a href="userData=<?php echo $uPageNums[3]; ?>" rel="<?php echo $uRels[3]; ?>"><?php echo $uRels[3]; ?></a>
<?php }
  } else {
    echo "No user pageNums set!";
  }

Please or to participate in this conversation.