September 17, 2009

Simple query strings

Often times a developer will want to pass a piece of information between pages on his website. While the most desirable method to do this is via a form, sometime this just isn’t possible. During these times, it’s time to turn to query strings.

Query strings are a bit of a confusing concept, mostly because of their name. If you ignore the terminology, it is just a way for you to pass information between pages on a website using the url. This is the key to Query strings. They make your url go from this

index.php

to this:

index.php?q1=something&q2=somethingelse

And that’s all a query string is. When a user sees a link like that in the location bar in their browser what they are really seeing is index.php but with a little added information passed to it.

How do you do it?

Creating query strings is ridiculously simple. Say you want to pass an id to news.php. You would like it like this:

<a href="index.php?id=4">Link to index.php with id 4</a>

If you wanted to pass an id and another value, you would do it like this:

<a href="index.php?id=4&another=value">Link to index.php with id 4 and another value</a>

And that’s it. If a user clicks on that link they will be directed to index.php with your query string attached!

How do I get that information?

Writing the query string, of course, is only half the battle. The other half is actually reading that query string. This is where the $_GET array comes into play. $_GET is built into PHP and provides you access to the values after the ? in query strings. Since $_GET is an array, the key that you will use to access your values, are their names!

A little confusing, but let me give you a quick breakdown using one of our query strings:

key = id
value = 4

So that means, to get that value 4, we would need to use $_GET['id'].

If we follow that logic, then to get the value of “another” we would use $_GET['another'].

Summary

Query strings are a little confusing until you realize that they’re just a way of passing variables through the url.

Still confused? Post your questions in the comments or send me an email!

Comments (11)

  1. June 2, 2011

    Wonderful blog! I found it while surfing around on Yahoo News. Do you have any tips on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Cheers

  2. June 2, 2011

    Terrific post however I was wondering if you could write a litte more on this subject? I’d be very grateful if you could elaborate a little bit further. Cheers!

  3. June 2, 2011

    Hi there! This is my first visit to your blog! We are a team of volunteers and starting a new project in a community in the same niche. Your blog provided us useful information to work on. You have done a marvellous job!

  4. June 2, 2011

    My partner and I absolutely love your blog and find nearly all of your post’s to be what precisely I’m looking for. Do you offer guest writers to write content in your case? I wouldn’t mind creating a post or elaborating on a number of the subjects you write concerning here. Again, awesome web log!

  5. June 2, 2011
    daniel mamann said...

    I really like what you guys are usually up too. This kind of clever work and coverage! Keep up the fantastic works guys I’ve added you guys to blogroll.

  6. July 15, 2011

    I heard of this guy who actually got pretty savvy at this stuff, he hides up in a cave and only comes out once in a while, a little bird told me he is about to wake up from his winter hibernation and boy O boy is he hungry and he just might go on a feeding frenzy, the menu read’s roasted on the grill Tommy Middaugh, broiled Jeanette Bunn la cart in the convention oven, nicely seared Scott Bridge, burnt so bad you have to through it away Travel To Go and just in for desert Jason Zenna sorbet…
    He might just wake in about 4-5 days as the temperature is heating up and as far as I know blogroll is so 2008 might want to try nu-gen ninja I heard it is blogroll to the 10th power but what do I know?? LOL ok ok no more messages I heard a growl coming from the cave… oh oh what is that I hear wej was a jew then thought he was a cookie monster too!! lol lol

  7. July 15, 2011

    hi jason zenna be nice or the cookie monster might get hungry said…

    I heard of this guy who actually got pretty savvy at this stuff, he hides up in a cave and only comes out once in a while, a little bird told me he is about to wake up from his winter hibernation and boy O boy is he hungry and he just might go on a feeding frenzy, the menu read’s roasted on the grill Tommy Middaugh, broiled Jeanette Bunn la cart in the convention oven, nicely seared Scott Bridge, burnt so bad you have to through it away Travel To Go and just in for desert Jason Zenna sorbet…
    He might just wake in about 4-5 days as the temperature is heating up and as far as I know blogroll is so 2008 might want to try nu-gen ninja I heard it is blogroll to the 10th power but what do I know?? LOL ok ok no more messages I heard a growl coming from the cave… oh oh what is that I hear wej was a jew then thought he was a cookie monster too!! lol lol

  8. July 15, 2011

    Click it t clock, click it t clock, Travel To Go Has left the dock, the cock got lost! oh where could it be?? oh yeah that’s right Jennette Bunn has it LOL

  9. July 15, 2011

    Ok Ok I got another,
    The incy bincy twicker climbed into the twekers nest, out came the rain and washed Tommy Middaugh out…out came the sun and dried up Scotty Bridge and the incy bincy twekers didnt make it to see another day!

  10. July 15, 2011

    Alright last one, last one, then no more, the Russians and Chinese take over after this one…
    Oh Dradel dradel dradel…..weight let me start over
    Oh travel to go h travel to go oh travel to go what a scam you all are yes losers and zeros and twekers not withstanding I’m on one now and am coming full speed ahead, and there’s a particular rep management site that’s gonna have to be found I think on page 2043 of Google and guess whats on pages 1 to 2042….

  11. July 15, 2011

    With love from the bad dream you had when you were a kid and couldn’t find your mother to help you because she was shit scared too!!!

Leave a Reply