Actions

Difference between revisions of "JURI/buildQuery"

From Joomla! Documentation

(New page: Builds the query part of the URI represented by the JURI object from an array of query items. Returns the query part in URL-encoded string form. ===Syntax=== string buildQuery( $params, $...)
 
m (See also: re-categorisation)
 
Line 75: Line 75:
 
* [[JURI/getQuery|JURI->getQuery]]
 
* [[JURI/getQuery|JURI->getQuery]]
 
* [[JURI/setQuery|JURI->setQuery]]
 
* [[JURI/setQuery|JURI->setQuery]]
<noinclude>[[Category:Development]][[Category:Framework]][[Category:JURI]]</noinclude>
+
<noinclude>[[Category:JURI]]</noinclude>

Latest revision as of 11:32, 9 August 2012

Builds the query part of the URI represented by the JURI object from an array of query items. Returns the query part in URL-encoded string form.

Contents

Syntax

string buildQuery( $params, $key )

where:

Argument Data type Description Default
$params array Associative array of name-value pairs to be assembled into the query. Array elements may themselves be arrays, but only to a single level of nesting.
$key string Name of the array for multi-valued arguments. Usually only used internally to handle nested arrays. null

Example 1

In this example, a query is built from an array of three elements, one of which is itself an array of three elements.

$params = array( 'var1' => 1,
                 'var2' => array ( 'var3' => 'three', 'var4' => 'four', 'var5' => 5 ),
                 'var6' => 'six'
                );
echo 'Query is ' . $u->buildQuery( $params ) . "\n";

would output

Query is var1=1&var2[var3]=three&var2[var4]=four&var2[var5]=5&var6=six

Example 2

In this example, an existing URI query is merged with an array of new query items to form a new URI.

$uri = 'http://fredbloggs:itsasecret@www.example.com:8080/path/to/Joomla/index.php?task=view&id=32#anchorthis';
$u =& JURI::getInstance( $uri );
echo 'Before: ' . $u->toString() . "\n";
$params = array( 'task' => 'save',
                 'var1' => 1,
                 'var2' => 'two'
                );
$params = array_merge( $u->getQuery( true ), $params );
$query = $u->buildQuery( $params );
$u->setQuery( $query );
echo 'After : ' . $u->toString();

would output

Before: http://fredbloggs:itsasecret@www.example.com:8080/path/to/Joomla/index.php?task=view&id=32#anchorthis
After : http://fredbloggs:itsasecret@www.example.com:8080/path/to/Joomla/index.php?task=save&id=32&var1=1&var2=two#anchorthis

Example 3

In this example, an array of query items is obtained from a URI containing a query part which contains a multi-valued item.

$uri = 'http://www.example.com/path/to/Joomla/index.php?var1=1&var2[var3]=three&var2[var4]=four&var2[var5]=5&var6=six';
$u =& JURI::getInstance( $uri );
print_r( $u->getQuery( true ) );

would output

Array
(
    [var1] => 1
    [var2] => Array
        (
            [var3] => three
            [var4] => four
            [var5] => 5
        )

    [var6] => six
)

See also