[ Team LiB ] Previous Section Next Section

Creating a Query String

To create a query string, you need to be able to URL encode the keys and values you want to include. Assume that we want to pass a URL to another page as part of a query string. The forward slashes and the colon in a full URL would create ambiguity for a parser. We must therefore convert the URL into hexadecimal characters. We can do this using PHP's urlencode() function, which accepts a string and returns an encoded copy:

print urlencode("http://p24.corrosive.co.uk");
// prints http%3A%2F%2Fp24.corrosive.co.uk

Now that you can URL encode text, you can build your own query string. The following fragment builds a query string from two variables:

$interest = "arts";
$homepage = "http://p24.corrosive.co.uk";
$query = "homepage=".urlencode( $homepage );
$query .= "&interest=".urlencode( $interest );
<a href="newpage.php?<?php print $query ?>">Go</a>

The URL in the link reaches the browser including an encoded query string:


The homepage and interest parameters become available within newpage.php as global variables.

This approach is clumsy, however. Because we have hard-coded variable names into the query string, we cannot reuse the code easily. To pass information effectively from page to page, we need to make it easy to embed names and values into a link and generate a query string automatically. This is especially important if we are to maintain the benefit of PHP that it is easy for a nonprogrammer to work around.

As of PHP 5, a new function was introduced to automate this process: http_build_query() accepts an associative array or object and returns a URL-encoded string suitable for adding to a URL in your script.

In Listing 19.5, we use the http_build_query() method to build a query string dynamically.

Listing 19.5 A Function to Build Query Strings
 2:   "-//W3C//DTD XHTML 1.0 Strict//EN"
 3:   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 4: <html>
 5: <head>
 6: <title>Listing 19.5 Using http_build_query() to Build Query Strings</title>
 7: </head>
 8: <body>
 9: <?php
10: $q = array (
11:     'name' => "Arthur Harold Smith",
12:     'interest' => "Cinema (mainly art house)",
13:     'homepage' => "http://p24.corrosive.co.uk/harold/"
14:     );
15: $query = http_build_query( $q );
16: print $query;
18: // prints name=Arthur+Harold+Smith&interest=Cinema+%28mainly+art+house
19: //    %29&homepage=http%3A%2F%2Fp24.corrosive.co.uk%2Fharold%2F
21: ?>
22: <p>
23: <a href="anotherpage.php?<?php print $query ?>">Go!</a>
24: </p>
25: </body>
26: </html>

We construct an array on line 10 with elements 'name', 'interest', and 'homepage'. We then pass the array to http_build_query() on line 15, storing the returned string in a variable called $query. As a test, we print $query to the browser before using the query string in a link on line 23.

Using this function, we can pass information between pages with the minimum of PHP code within HTML elements.

    [ Team LiB ] Previous Section Next Section