Nitish
Nitish

Reputation: 2763

Unable to retrieve array values

I have a HTML form where I will be sending all the information to the server side. One of my field is like

<option value="bus_info[{$k->bus_id},{$k->route_from},{$k->route_to}]">{$k->route_from} To {$k->route_to},{$k->bus_time}</option>

And when check in PHP side

$bus_information[] = $_REQUEST['bid'];

var_dump($bus_information);`

it shows the following result :

array
  0 => string 'bus_info[1,Guwahati,Amguri]' (length=27)

Now how can I retrieve the elements from the array ? I tried echo $bus_information[0][0]; but it shows the result b ! Why so

Upvotes: 0

Views: 92

Answers (4)

Rich
Rich

Reputation: 900

This will give you an array of values you're looking for:

Remove the array wrapper in the option value field:

<option value="{$k->bus_id},{$k->route_from},{$k->route_to}">{$k->route_from} To {$k->route_to},{$k->bus_time}</option>

You will now get a string to work with when the form is submitted:

$bus_info_string = $_REQUEST['bid']; 

echo($bus_info_string);

The string looks like:

'1,Guwahati,Amguri'

Explode the string

Now use PHP's explode method. This converts the string values into an array:

$bus_info_array = explode(",", $bus_info_string);

The array looks like this:

Array
(
    [0] => 1
    [1] => 'Guwahati'
    [2] => 'Amguri'
)

Now to access the individual elements, simply use:

echo $bus_info_array[0]; // Bus Id
echo $bus_info_array[1]; // Route From
echo $bus_info_array[2]; // Route To

Upvotes: 1

user254875486
user254875486

Reputation: 11240

Multidimensional arrays don't work that way in HTML and PHP. You should use this in your form:

<option value="bus_info[{$k->bus_id}][{$k->route_from}][{$k->route_to}]">{$k->route_from} To {$k->route_to},{$k->bus_time}</option>

EDIT based on comment:

Sorry, this will not work at all. I'm assuming you want the user to select some sort of route, and based on that you want to pass a particular value. The way a <select> works is this:

In your HTML use this:

<select name="test">
    <option value="1">First option</option>
    <option value="2">Second option</option>
</select>

Now, in PHP you can access the value that was submitted like this:

$_REQUEST['test'] //this value will be 1 if the user 
                  //selected the first option, or 2 if 
                  //he selected the second

You can use Arrays in the names of the input fields, so it would be possible to do something like this in HTML:

<select name="test[1]">
    <option value="1">First option</option>
    <option value="2">Second option</option>
</select>
<select name="test[2]">
    <option value="1">First option</option>
    <option value="2">Second option</option>
</select>

Note that I used the array notation on the name attribute, not the value. Now you can do this in PHP:

$_REQUEST['test'][1] //this value will be 1 if the user 
                     //selected the first option, or 2 if 
                     //he selected the second in the first
                     //drop down
$_REQUEST['test'][2] //this value will be 1 if the user 
                     //selected the first option, or 2 if 
                     //he selected the second in the second
                     //drop down

So what you're doing, using arrays in the value attribute will not work. You could do something like this:

<select name="bus_info">
    <option value="{$k->bus_id},{$k->route_from},{$k->route_to}">{$k->route_from} To {$k->route_to},{$k->bus_time}</option>
</select>

And then in PHP:

$values = explode($_REQUEST['bus_info']);
//now $values is an array with 3 elements, the ID, the route_from and route_to

Upvotes: 3

jeroen
jeroen

Reputation: 91734

$_REQUEST['bid'] is just a string, not an array.

You can use some of php's string functions to convert it to an array though:

$result = $_REQUEST['bid'];
$result = strstr($result, '[');
$result = trim($result, '[]');
$array = explode(',' $result);

But personally I would have numerical values that map to my data on the server-end.

Upvotes: 1

Christoph Fink
Christoph Fink

Reputation: 23093

As the var_dump() is telling you, you hava string and not an array, means you need to use regex or split the string e.g. like:

Only print the content inside [] like:

<option value="{$k->bus_id},{$k->route_from},{$k->route_to}">{$k->route_from} To {$k->route_to},{$k->bus_time}</option>

Then you can get the array by the following:

$parts = explode(',', $bus_information);

Upvotes: 0

Related Questions