A Function to Build Query Strings : Query String « Development « PHP






A Function to Build Query Strings

<html>
<head>
<title>A function to build query strings</title>
</head>
<body>
<?php
function qlink( $q ){
    GLOBAL $QUERY_STRING;
    if ( ! $q ) 
       return $QUERY_STRING;
    $ret = "";
    foreach( $q as $key => $val ) {
        if ( strlen( $ret ) ) 
           $ret .= "&";
        $ret .= urlencode( $key ) . "=" . urlencode( $val );
    }
    return $ret;
}
$q = array ( name => "Joe",
             interest => "coding",
             homepage => "http://www.google.com"
          );
print qlink( $q );

?>
<p>
<a href="anotherpage.php?<? print qlink($q) ?>">Go!</a>
</p>
</body>
</html>

           
       








Related examples in the same category

1.Send data without a form
2.Get query string