How to Get Multiple Parameters With Same Name from a Url in PHP

How to get multiple parameters with same name from a URL in PHP

Something like:

$query  = explode('&', $_SERVER['QUERY_STRING']);
$params = array();

foreach( $query as $param )
{
// prevent notice on explode() if $param has no '='
if (strpos($param, '=') === false) $param += '=';

list($name, $value) = explode('=', $param, 2);
$params[urldecode($name)][] = urldecode($value);
}

gives you:

array(
'ctx_ver' => array('Z39.88-2004'),
'rft_id' => array('info:oclcnum/1903126', 'http://www.biodiversitylibrary.org/bibliography/4323'),
'rft_val_fmt' => array('info:ofi/fmt:kev:mtx:book'),
'rft.genre' => array('book'),
'rft.btitle' => array('At last: a Christmas in the West Indies.'),
'rft.place' => array('London'),
'rft.pub' => array('Macmillan and co.'),
'rft.aufirst' => array('Charles'),
'rft.aulast' => array('Kingsley'),
'rft.au' => array('Kingsley, Charles'),
'rft.pages' => array('1-352'),
'rft.tpages' => array('352'),
'rft.date' => array('1871')
)

Since it's always possible that one URL parameter is repeated, it's better to always have arrays, instead of only for those parameters where you anticipate them.

PHP Get multiple values from GET with same name

I dont think without using array you cant retrieve data of same name. Example

<input type="text" name="color[]" value="blue">
<input type="text" name="color[]" value="green">
<input type="text" name="color[]" value="black">

now datas will pass like this

?color[]=Blue&color[]=Green&color[]=Black  
$color=$_GET['color'];
print_r($color);

Array
(
[color] => Array
(
[0] => Blue
[1] => Green
[2] => Black
)
)

Correct way to pass multiple values for same parameter name in GET request

Indeed, there is no defined standard. To support that information, have a look at wikipedia, in the Query String chapter. There is the following comment:

While there is no definitive standard, most web frameworks allow
multiple values to be associated with a single field.[3][4]

Furthermore, when you take a look at the RFC 3986, in section 3.4 Query, there is no definition for parameters with multiple values.

Most applications use the first option you have shown: http://server/action?id=a&id=b. To support that information, take a look at this Stackoverflow link, and this MSDN link regarding ASP.NET applications, which use the same standard for parameters with multiple values.

However, since you are developing the APIs, I suggest you to do what is the easiest for you, since the caller of the API will not have much trouble creating the query string.

How to get multiple parameters with same name from a URL in PHP and insert all records into table

You try to assign a value with same name.so your last value replace with the existing value.

for example :your URL look like,

http://www.example.com/index.php?finished_product_name=abc&material_name=xxx&finished_product_name=pqr&material_name=yyy

so your $_GET['finished_product_name'] has value is pqr not abc.


If you can change the field name with include [], then PHP will create an array containing all of the matching values:

http://www.example.com/index.php?id[]=123&version[]=3&id[]=234&version[]=4

your URL example like,

http://www.example.com/index.php?finished_product_name[]=abc&material_name[]=xxx&finished_product_name[]=pqr&material_name[]=yyy

your for loop is :

for ($i=0; $i < count($_POST['finished_product_name']); $i++ )
{
$product =$_POST['finished_product_name'][$i];
$material = $_POST['material_name'][$i];
$quantity = $_POST'product_quantity'][$i];
}

How to correctly display multiple parameters in URL with the same GET name for PHP/MySQL

It sounds like you want to look in the tags column in your table and find any matches for the values passed in the URL. If you want to find ANY of the tag values change the use of AND to OR instead.

$tags  = $_GET['tags'];
$tags2 = $_GET['tags2'];
$tags3 = $_GET['tags3'];

$fetch = mysqli_query($conn, "SELECT * FROM users
JOIN user_images ON users.username = user_images.username
WHERE tags LIKE '%" . $tags . "%' OR
tags LIKE '%" . $tags2 . "%' OR
tags LIKE '%" . $tags3 . "%'
ORDER BY users.id DESC")


Related Topics



Leave a reply



Submit