...

View Full Version : Make An Array Into A Variable



ideffect
09-24-2005, 02:00 AM
Hello,

I have a array simply constructed from a post which I need to set it up into a variable to submit the data to a database. Here's the code...

HTML


<select name='intl_ship[]' size='5' multiple='multiple'>
<option>Afghanistan</option>
<option>Albania</option>
<option>Algeria</option>
<option>American Samoa</option>
<option>Andorra</option>
<option>Angola</option>
<option>Anguilla</option>
<option>Antarctica</option>
<option>Antigua and Barbuda</option>
</select>


PHP


foreach ($intl_ship as $ship_con)
{
echo"$ship_con<br>";
}

So if I select Afghanistan, Albania, Algeria it puts out:
Afghanistan
Albania
Algeria
If possible I would also like to submit a - at the end of each selected item.

Thanks for the help!

missing-score
09-24-2005, 02:20 AM
Firstly, a big security issue...

You are using $intl_ship to get the submitted data, this can lead to security issues and importable code, and is generally frowned upon. Use the $_POST global instead (shown in example below)

Im not 100% sure what your problem is here... If you want to store the array to use again, you could use:



$variable = serialize($_POST['intl_ship']);

ideffect
09-24-2005, 03:09 AM
Thanks for the reply. Do you have any more info on the security issue pertiaining to not using $_POST?

I will give your solution a try.

ideffect
09-24-2005, 03:15 AM
That did work but it is ugly. Any way to only list the countries?
Here's what it outputs:
a:3:{i:0;s:11:"Afghanistan";i:1;s:7:"Albania";i:2;s:7:"Algeria";}

missing-score
09-24-2005, 03:20 AM
The security hole isnt a wide one, but it can happen. Say you have a function that takes $var as a parameter... If you forget to set $var, and then someone puts yourpage.php?var=something_bad in the URL, they can run whatever they like though your function.

Secondly, $_GET, $_POST, $_COOKIE, $_SESSION and $_SERVER are good coding practise, and you should use these and not rely on global variables.

In response to your second post, you can make the data back into an array using unserialize(), on that jumbled mess of values. But if you just want the countries listed the why not use:



$variable = implode(',', $_POST['intl_ship']);

ideffect
09-24-2005, 03:28 AM
That's perfect!

Thank you sooo much for your help and letting me know about $_POST issue. I'll defintly use it from now on...

missing-score
09-24-2005, 03:30 AM
No Problem at all :)

bcarl314
09-24-2005, 04:03 AM
If possible I would also like to submit a - at the end of each selected item.

Another option is to assign values in your option tags:



<option value="Afghanistan -">Afghanistan</option>
.
.
.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum