cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Community Member

PHP Pagination - buttons?

Hi,
I'm struggling with working pagination in PHP. According to the documentation, I can use those links to go to the next page of the collected results. I can put out a certain amount of results per page, which I want, but I also want to go from page to page - I want to have working "Next", "Previous", "First" and "Last" buttons too.

But, in PHP, I have tried to write the exact equivalents of those, and it doesn't work. See my code:

'<a href="https://my.test.infrastructure.com/courses?page=1&per_page=3" rel="next">Next</a>'

This, unfortunately, don't work. Frankly I don't know what does.

I have seen other threads on this matter, but they have all focused on Python or other languages, so none of them help me. The ones I am working with are PHP foremost, with some Laravel, HTML and Blade.

Any advice? Every bit of help is greatly appreciated

Labels (3)
10 Replies
Highlighted
Learner II

What is the response link header code for rel="next" look like after your PHP code makes the request against the API? It should give you the next page like ?page=2&per_page=3 instead of ?page=1&per_page=3

Highlighted

The link to the main page, by default, takes the arguments "?page=1&per_page=3"

The Next button reads exactly as you said: "https://my.test.instructure.com/courses/?page=2&per_page=3"

But it still takes me to the same page, with the exact same results as page 1.

I admit I don't understand why.

0 Kudos
Highlighted
Community Coach
Community Coach

sam.ofloinn@ucc.ie...

While I don't have anything to share (I'm not a developer and I don't know PHP)...but I saw that your href link had the word "infrastructure" rather than "instructure".  Many Canvas schools have URLs with the word "instructure" in their URLs, so I wanted to point this out.  I have no idea if this will help at all in your case.  Just adding my $0.02.

Highlighted

Thanks, Chris. Fortunately that isn't the issue, but I appreciate the pointer!

Highlighted

At this point without seeing your PHP script in action and observing the output, my only advice is to see if a tool like Postman will return the next link and then take that next link and copy/paste it into your browser to see the next page of results. 

0 Kudos
Highlighted
Navigator

sam.ofloinn@ucc.ie,

This looks like you're confusing API calls with web links within the browser.

All of your descriptions and narrative make it sound like you're talking about making an API call, but you don't have /api/v1/ in the URL. What you do have is part of a regular anchor element's href attribute. That means that if someone clicked on it, it should open it up in a browser. API calls should not open directly in a browser, but they should be consumed by some program that knows how to interpret JSON results.

JEFHQ12951 is talking about the response header and the links returned with an API call and you're talking about links on buttons, which the API doesn't have.

Since you're running in PHP and talking about buttons, are you using an LTI? If so, then any internal links are up to you to handle pagination if needed. If you're trying to call content from within Canvas from your LTI, then the routing is set up incorrectly. Since Canvas doesn't understand it, it just keeps returning the same thing.

Highlighted

Are the link examples you are posting the actual links you are using?

They are missing the /api/v1.

The pagination is for API calls, not the page URLs as in you browser.

Highlighted

To be precise, my server is calling the link via Laravel route directory.

While I'm typing in the above link: https://my.test.infrastructure.com/courses?page=1&per_page=3"

What's actually happening under the hood is this cUrl call:

curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => true,
CURLINFO_HEADER_OUT => true,
CURLOPT_URL => 'https://my.test.instructure.com/api/v1/courses/1/sis_imports?page=1&per_page=5',
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_RETURNTRANSFER, true
]);
$resp = curl_exec($curl);
// curl_close($curl);
$data = json_decode($resp, true);
return view('myView')->with('data', $data);

Does this shed any useful light on the matter?

Highlighted

james@richland.edu

I've fixed the URL with /api/v1 now; my test link didn't have that. However, pagination still isn't working quite well.

LTI, I believe I am. I'm using Laravel and Curl to point to the webpage with the results. The code in full:

public function testImport() {
$token = 'my_token';
$headers = ['Authorization: Bearer ' . $token];
$curl = curl_init();

curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => true,
CURLINFO_HEADER_OUT => true,
CURLOPT_URL => 'https://my.test.instructure.com/api/v1/courses/1/sis_imports?page=1&per_page=5',
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_RETURNTRANSFER, true
]);

$resp = curl_exec($curl);
curl_close($curl);
$data = json_decode($resp, true);
return view('MyView')->with('data', $data);

I can attest that the link I've given is correct. Does that affect my pagination in any way?

0 Kudos