Log In · Register

 
 
Reply to this topicStart new topic
Query Strings, how do you do it?
Mikeplyts
post Feb 22 2009, 10:27 AM
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. _smile.gif
 
mipadi
post Feb 22 2009, 10:46 AM
Post #2


Senior Member
******

Group: Administrator
Posts: 2,648
Joined: Apr 2008
Member No: 639,265



Like this:

CODE
<a href="/path/to/page.php?page=2">Page 2</a>


Of course, query strings are useless unless you have some PHP code to extract and use the values in the query string. You can do this using PHP's $_GET variable:

CODE
<?php echo $_GET["page"]; ?>
 
Mikeplyts
post Feb 22 2009, 10:50 AM
Post #3


Mel Blanc was allergic to carrots.
*******

Group: Official Designer
Posts: 6,371
Joined: Aug 2008
Member No: 676,291



Oh ok. Also, would I have to make a new page that's called "2" or something?
 
mipadi
post Feb 22 2009, 10:54 AM
Post #4


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'))
 
newkidontheblock
post Feb 22 2009, 12:15 PM
Post #5


Offline.
*****

Group: Official Designer
Posts: 609
Joined: Mar 2007
Member No: 507,591



I know the code above is too advance for someone who is a beginner but here is a simply code. The code is mostly self explanatory.

CODE
<?php
    if ( isset( $_GET['page'] ) ){
        $page = $_GET['page']; # Getting page ID ex. ?page=2
    }
    if ( empty( $page ) || $page == '' || $page == '0' ){
        echo "Home Page Content";
    } else if ( $page == '1' ){
        echo "Page 1 Content Goes Here";
    } else if ( $page == '2' ){
        echo "Page 2 Content Goes Here";
    } else {
        echo "Page Not Found";
    }    
?>


Within the code their are for pages:
- Homepage
- Page 1
- Page 2
- Error Page

To access the Homepage you simply can access it by going to the page.php, or page.php?page=0
To access page one and to you can go to page.php?page=1 or page.php?page=2
To acess the error page you can go to any page that is not listed ex. 3, 4, 5, etc.

To add new page you can add an
CODE
else if( $page == 'PAGEID') {
    echo "Page content goes here";
}

Make sure it is places before the else statement.
PAGEID Doesn't have to be a number it can be letters too.

HERE is a preview
 
Mikeplyts
post Feb 22 2009, 06:09 PM
Post #6


Mel Blanc was allergic to carrots.
*******

Group: Official Designer
Posts: 6,371
Joined: Aug 2008
Member No: 676,291



^Ok, I tried it but it still wasn't working for me. It shows the page blank. I notice you have a script tag that has a src of "./js/jquery.js". Do I have to make a .js page to put the php in? Also, should I add the DOCTYPE?
 
Maccabee
post Feb 22 2009, 06:18 PM
Post #7


Senior Member
*******

Group: Official Designer
Posts: 5,880
Joined: Nov 2007
Member No: 593,382



Or! If it's in the case of a form or something, it would be like this page:
http://wefuze.com/php/form.php
Because i used get on the form it shows the php results in the address bar which allows someone to bookmark it.
Edit: I o see you wanna know about doctypes too. yes its good to add one.
At the top of the page add:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

After much researcher I have learnt that on any page this is prob the best to use.

And replace <html> at the top with:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
Mikeplyts
post Feb 22 2009, 06:44 PM
Post #8


Mel Blanc was allergic to carrots.
*******

Group: Official Designer
Posts: 6,371
Joined: Aug 2008
Member No: 676,291



Ok, I figured out how to make it not be blank but would I be able to put HTML tags into the 'echo' property in Fawaz's code?
 
mipadi
post Feb 22 2009, 06:49 PM
Post #9


Senior Member
******

Group: Administrator
Posts: 2,648
Joined: Apr 2008
Member No: 639,265



QUOTE(Mikeplyts @ Feb 22 2009, 06:44 PM) *
Ok, I figured out how to make it not be blank but would I be able to put HTML tags into the 'echo' property in Fawaz's code?

Yes.
 
Mikeplyts
post Feb 22 2009, 06:57 PM
Post #10


Mel Blanc was allergic to carrots.
*******

Group: Official Designer
Posts: 6,371
Joined: Aug 2008
Member No: 676,291



Could I also put PHP includes in there?
 
Maccabee
post Feb 22 2009, 07:02 PM
Post #11


Senior Member
*******

Group: Official Designer
Posts: 5,880
Joined: Nov 2007
Member No: 593,382



Yes. But dont put <?php include() ?> just put include() cause the <?php ?> tags are already there. Unless of course you put it outside the code then it would be normal.

Edi: also quick thing, even though long php url's look cool, it's not very practical and it would be better to keep em short.

What i do is make a seperate directory with its own index for each new page so instead of wefuze.com/contact.php its wefuze.com/contact/
 
mipadi
post Feb 22 2009, 07:02 PM
Post #12


Senior Member
******

Group: Administrator
Posts: 2,648
Joined: Apr 2008
Member No: 639,265



QUOTE(Mikeplyts @ Feb 22 2009, 06:57 PM) *
Could I also put PHP includes in there?

Yes. You can put in any PHP code you want.

However, you really should ask yourself why you're using the query string. If you're using it for, e.g., pagination in a blog, then good. But don't use the query string to specify wildly different pages (i.e., don't do something like "index.php?page=main", "index.php?page=about", etc. -- in other words, don't make one big "index.php" file that handles your entire site).
 
Mikeplyts
post Feb 22 2009, 08:15 PM
Post #13


Mel Blanc was allergic to carrots.
*******

Group: Official Designer
Posts: 6,371
Joined: Aug 2008
Member No: 676,291



Ok. Thanks. biggrin.gif Another question, how I could make the double quote ( " ) appear inside the echo property without making the code mess up?
 
newkidontheblock
post Feb 22 2009, 08:28 PM
Post #14


Offline.
*****

Group: Official Designer
Posts: 609
Joined: Mar 2007
Member No: 507,591



Use forward slash \ before any Quotes like \"
 
mipadi
post Feb 22 2009, 08:29 PM
Post #15


Senior Member
******

Group: Administrator
Posts: 2,648
Joined: Apr 2008
Member No: 639,265



You have to escape them with a backslash \, like so:
CODE
<?php echo "\"What string?\" he asked."; ?>

Or you can use single quotes around the string:
CODE
<?php echo '"What string?\" he asked."; ?>

The second method has a few gotchas, though:
  • You'll now have to escape apostrophes '.
  • Variable interpolation does not work in single-quoted strings.
 
newkidontheblock
post Feb 22 2009, 08:34 PM
Post #16


Offline.
*****

Group: Official Designer
Posts: 609
Joined: Mar 2007
Member No: 507,591



Here is an easy way to do use HTML inside in those parameters that I provided
CODE
<?php
    if ( isset( $_GET['page'] ) ){
        $page = $_GET['page']; # Getting page ID ex. ?page=2
    }
    if ( empty( $page ) || $page == '' || $page == '0' ){
        echo "Home Page Content";
    } else if ( $page == '1' ){
        echo "Page 1 Content Goes Here";
    } else if ( $page == '2' ){
        echo "Page 2 Content Goes Here";
    } else {
        echo "Page Not Found";
    }    
?>


Just close off the PHP after the opening parentheses like this.
CODE
else if ( $page == '1' ){ ?>
ANY HTML CODE HERE
<?php } ?>
and reopen it before the closing parentheses.
 
Mikeplyts
post Mar 13 2009, 04:19 PM
Post #17


Mel Blanc was allergic to carrots.
*******

Group: Official Designer
Posts: 6,371
Joined: Aug 2008
Member No: 676,291



Thanks. biggrin.gif

One more question, how could you add another "variable"? Like "?NAME=VALUE&ANOTHERNAME=ANOTHERVALUE".
 
mipadi
post Mar 13 2009, 04:54 PM
Post #18


Senior Member
******

Group: Administrator
Posts: 2,648
Joined: Apr 2008
Member No: 639,265



QUOTE(Mikeplyts @ Mar 13 2009, 05:19 PM) *
One more question, how could you add another "variable"? Like "?NAME=VALUE&ANOTHERNAME=ANOTHERVALUE".

Just make a link:

HTML
<a href="?name=value&amp;anothername=anothervalue">Links</a>


Different values are separated by an "&".
 
Mikeplyts
post Mar 13 2009, 05:11 PM
Post #19


Mel Blanc was allergic to carrots.
*******

Group: Official Designer
Posts: 6,371
Joined: Aug 2008
Member No: 676,291



^Yeah, I know that but how do I like 'define' it in the PHP code?
 
mipadi
post Mar 13 2009, 05:57 PM
Post #20


Senior Member
******

Group: Administrator
Posts: 2,648
Joined: Apr 2008
Member No: 639,265



QUOTE(Mikeplyts @ Mar 13 2009, 06:11 PM) *
^Yeah, I know that but how do I like 'define' it in the PHP code?

Access it with the $_GET variable:
CODE
$var = $_GET['name'];
$other_var = $_GET['anothername'];
 
Mikeplyts
post Mar 13 2009, 06:04 PM
Post #21


Mel Blanc was allergic to carrots.
*******

Group: Official Designer
Posts: 6,371
Joined: Aug 2008
Member No: 676,291



^And how would I add that to Fawaz's code?

Thanks for the help btw..:p
 
mipadi
post Mar 13 2009, 06:10 PM
Post #22


Senior Member
******

Group: Administrator
Posts: 2,648
Joined: Apr 2008
Member No: 639,265



QUOTE(Mikeplyts @ Mar 13 2009, 07:04 PM) *
^And how would I add that to Fawaz's code?

Thanks for the help btw..:p

Depends on where you want to use it, and what you want to do with it.

The simple answer is, "Anywhere you want to."
 

Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members: