Pergunta

I'm using urlencode & urldecode to pass variables trough a html-form.

$info = 'tempId='.$rows['tempID'].'&tempType='.$rows['tempType'].'&dbId='.$rows['ID'];
echo '<input type="hidden" name="rank[]" value="'.urlencode($info).'" >';

Here is what is in $rows

array (size=4)
  'ID' => string '110' (length=3)
  'tempID' => string '1' (length=1)
  'tempType' => string 'temp_first' (length=10)
  'pageOrder' => string '0' (length=1)

So $info is

tempId=1&tempType=temp_first&dbId=110

But if i then decode it, it losses 1 parameter. How is this possible?

foreach (explode('&', urldecode($list[$i])) as $chunk) {
    $param = explode("=", $chunk);

    $tempId = urldecode($param[0]); // template id
    $tempType = urldecode($param[1]); // Template type
    $dbId = urldecode($param[2]); // database ID

    var_dump($param);

}

Output:

array (size=2)
  0 => string 'dbId' (length=4)
  1 => string '110' (length=3)

Sometime there are even things in the array wich should not be in there, for example instead of temp_first it says tempType. Just the variable name.

I hope you guys can help me

Foi útil?

Solução

There's no need to explode and process the string manually, you can use parse_str():

parse_str(urldecode($list[$i]), $output);
var_dump($output);

Would output:

array
  'tempId' => string '1' (length=1)
  'tempType' => string 'temp_first' (length=10)
  'dbId' => string '110' (length=3)

Outras dicas

try this

  $result=array();
  foreach (explode('&', urldecode($list[$i])) as $chunk) {
     $param = explode("=", $chunk);
     $result[$param[0]]=$param[1];
  } 
  var_dump($result);

Could you try this and check the result (I'm groping in the dark though):

//change this <input type="hidden" name="rank[]" value="'.urlencode($info).'" > to
//<input type="hidden" name="urlargs" value="'.urlencode($info).'" >
$values = explode('&',urldecode($_POST['urlargs']));
$arguments = array();
foreach($values as $argument_set){
    $data = explode('=',$argument_set);
    $arguments[$data[0]] = $data[1];
}
var_dump($arguments);

I believe the problem is in the way you're processing the value

$data=array();
foreach (explode('&', urldecode($list[$i])) as $chunk) {

    $param = explode("=", $chunk); //
    $data[$param[0]]=$param[1]

}

Instead of grouping all code together, start by putting it in seperate vars and echo the content for debugging. Because you are saying you loose one variable, but the output you show is just one of the variables. What is the var_dump of the other two?

Because your var_dump($param); would output the part before the '=' and after the '=', so indeed i would expect the output to be something like: So which one of these are you missing?

array (size=2)
  0 => string 'tempId' (length=6)
  1 => string '1' (length=1)

array (size=2)
  0 => string 'tempType' (length=8)
  1 => string 'temp_first' (length=10)

array (size=2)
  0 => string 'dbId' (length=4)
  1 => string '110' (length=3)

DEBUG code:

foreach ($list as $row) {
  echo 'Full row:: '. $row.'<br>';

  //if the data is comming via GET or POST, its already decoded and no need to do it again
  $split = explode('&', urldecode($row));

  foreach($split as $chunk) {
    echo 'Chunk:: '.$chunk.'<br>';

    $param = explode('=', $chunk);

    echo 'param name:: '.$param[0].'<br>';
    echo 'param value:: '.$param[1].'<br>';
  }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top