Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Enhancing a Simple PHP Application Paginating a List: Controller Retrieving the GET Variable

some questions about video

Hello, I have just watched this video (Paginating a List: Controler(from stage 7)) and in the video we used $_GET function, but I have question about it.

If I understood it right we used $current_page = $_GET["pg"]; function to get page, but what is "pg" is it String or is it some php code to get page number? and will it work if I would use some other string??

It wasn't explained in the video what it is :?

And another question is about this function - function get_products_serach($s) we used it to get text from search area, but we didn't declare anywhere varyable that $s is value from search area and should it still work if I woild change it to some other letter?

I hope someone will help me. Thanks

2 Answers

Andrew Shook
Andrew Shook
31,709 Points

Pg is a get variable used to store information about which page to show. It doesn't show it in this particular video, but below the shirt randy is going to put pagination buttons. These buttons will pass the value for pg into the query string of the url (?pg=1. Notice the question mark thats how you know its a query). When the server gets the URL it takes everything after the "?" and puts it into the $_GET array by taking making the left side of the equals sign the index and the right side the value. What Randy is going to do is take that value and determine which shirts to display on the page. So if there are suppose to be 8 shirt on a page then pg=1 mean get me shirts 0-7 and display them. If pg=2 then get me 8-15.

Now how do you add the ?pg=1 to the url? Thats easy, in the pagination links at the bottom you simply add the query string to the end of the link. So if the page is www.shirts4mike.com.shirts you would make the link for the second set of shirts www.shirts4mike.com/shirts?pg=2 and the third page www.shirts4mike.com/shirts?pg=3

Now for the search question. If you go back and look at the code for the search page and look at the html for the form you will see that the action is GET. This means the form will generate the query string for you URL for you. When the submit button is clicked your browser will look at each name attribute of each field of that form and add it to the $_GET array. The name attribute of each input element will becomes the index and the value entered into the forms input fields will become the value. The name attribute for the search field is called "s" so to accesses that value on the server you would use $_GET['s'.]

Stone Preston
Stone Preston
42,016 Points

pg is a variable thats defined in the url of the page. the end of the url for the page probably has something like ?pg=1 on the end of it. thats where that variable is coming from

function get_products_serach($s) is the start of a function definition. $s is a parameter for that function. it can be named anything as long as you remain consistent throughout the function definition

check out this video on functions and this video on get variables for more info

If I'm undestanding it right, URL creates by him self that ending ?pg=1 right?

Thanks for your quick response.

Stone Preston
Stone Preston
42,016 Points

yep thats correct. probably a link on the page with an href to www.blahblahblah.com/whatever?pg=1

that ending is also known as a query string just fyi, so you will hear it called that a lot of the time

Thank you. Now I understand it. :)