Query Strings, how do you do it? |
Query Strings, how do you do it? |
![]()
Post
#1
|
|
![]() Mel Blanc was allergic to carrots. ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Official Designer Posts: 6,371 Joined: Aug 2008 Member No: 676,291 ![]() |
I was wondering how you could make those url's that have a question mark and a few equal signs and such at the end of a url. I belive they are called query strings, right? How can I do it to like make a page go to a next page such as this where it has "?page=2". I know it has to something with PHP and I tried googling tutorials but I just don't understand. Can someone please explain how to do this like if I wanted to make the page go the second page using a simple HTML "a" tag? Thanks.
![]() |
|
|
![]() |
![]()
Post
#2
|
|
![]() Senior Member ![]() ![]() ![]() ![]() ![]() ![]() Group: Administrator Posts: 2,648 Joined: Apr 2008 Member No: 639,265 ![]() |
No, the script that gets run would be whatever's in the path, e.g., page.php in the above example. So page.php?page=1 and page.php?page=99 are all handled by page.php, which would contain the logic for working with the query string values.
My personal website uses a page parameter in the query string to handle paging, as seen here. It's written in Python, so it may not help you understand, but here's the snippet of code I use to handle page numbers: CODE def get_page(request):
try: # Grabs the page from the query string page = int(request.GET['page']) except ValueError: raise PageError except KeyError: # If there's no ?page attribute, we want to # show the first page of results page = 1 if page < 1: raise PageError return page def index(request): try: page = get_page(request) return render_to_response("blog/paged.html", get_posts(page)) except PageError: return HttpResponseRedirect(reverse('blog')) |
|
|
![]() ![]() |