1

I'm sending multiple parameters to another page, and I am using http_build_query() to do this. The following code:

$array = array();

if(!empty($_POST['modelcheck'])){
    foreach($_POST['modelcheck'] as $selected){
        $array[] = $selected;
    }
}

$args = array
(
    'pricefrom' => $fromval,
    'priceto' => $toval,
    'model' => $array
);
    $params = http_build_query($args);
    $cleanedParams = preg_replace('/%5B(\d+?)%5D/', '', $params);


header("Location: ../page2.php?" . $cleanedParams);

gives me a url:

page2.php?pricefrom=10000&priceto=60000&model=1&model=2

As you can see model is repeated multiple times, I would like the parameters following the first model to be model2, model3.......etc.

I've tried putting it in a for loop:

for ($i=0; $i <count($array) ; $i++) { 
    $args = array
    (
        'pricefrom' => $fromval,
        'priceto' => $toval,
        'model'.$i => $array
    );
}

but this just gives me :

page2.php?pricefrom=10000&priceto=60000&model1=1&model1=2
miken32
  • 42,008
  • 16
  • 111
  • 154
  • Any particular reason why you want it to be `&model1=1&model1=2` and not `&model[]=1&model[]=2`? – Dharman Jun 07 '19 at 15:29
  • @Dharman What I want is `&model1=1&model2=2`, – JordanPattinson Jun 07 '19 at 15:33
  • 1
    You should use `&model[]=1&model[]=2` which will be an array on the other end and also you won't need to try and clean up with preg_replace. – AbraCadaver Jun 07 '19 at 15:34
  • if models are always integers, why not send all in one as `&model=1,2,3` and later use `$models = explode(",",$_GET['model']);` this will turn "1,2,3" into an array where $models[0] => "1", $models[1] => "2" and so on. – 2x2p Jun 07 '19 at 17:15

3 Answers3

1

You can use the second parameter in http_build_query to prefix the numerical keys with a string:

$args = array
(
    'pricefrom' => $fromval,
    'priceto' => $toval
);
$args += $array; // merge the two arrays together
$params = http_build_query($args, 'model', '&amp;'); // use a second arg for prefix.

However, I do not recommend to create separate names for variables like this. Better to use &model[]=1&model[]=2. See Passing arrays as url parameter

Dharman
  • 30,962
  • 25
  • 85
  • 135
1

There was nothing wrong with your original code except that you broke it with the call to preg_replace.

if(!empty($_POST['modelcheck'])){
    foreach($_POST['modelcheck'] as $selected){
        $models[] = $selected;
    }
}

$args = [
    'pricefrom' => $fromval,
    'priceto' => $toval,
    'model' => $models,
];

$params = http_build_query($args);
header("Location: ../page2.php?" . $params);

Now, in page2.php you just use $_GET['model'] as an array.

<?php
foreach ($_GET['model'] as $model) {
    printf('%s<br/>', $model);
}
miken32
  • 42,008
  • 16
  • 111
  • 154
-2

Your $args variable should look like:

$args = array (
    'pricefrom' => $fromval,
    'priceto' => $toval,
    'model' => $array
);

UPD

Use preg_replace for replace html special chars if you want to use http_build_query with multiple params.

$query = preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '[]=', http_build_query($args));

You will receive an array by accessing to $_GET['model']

Vitalii
  • 1,137
  • 14
  • 25