Jump to content

explode or something like that.


Birdman203

Recommended Posts

I have something like this:

[code]
<option value="1" style="padding-left: 20px; background: url(../img/flags/1.gif) no-repeat;" selected>United States</option><option value="2" style="padding-left: 20px; background: url(../img/flags/2.gif) no-repeat;" >Norway</option>
[/code]

And I need the value of it to be inserted as the ID in a table and the name of it thing which is United Stats and Norway in name.
I need this, because I have about 180 countries and I need all of them to be in a SQL database.
Link to comment
https://forums.phpfreaks.com/topic/13895-explode-or-something-like-that/
Share on other sites

A simple way round is to set to value of the options to:

[code]<option value="1|United States" style="padding-left: 20px; background: url(../img/flags/1.gif) no-repeat;" selected>United States</option>
<option value="2|Norway" style="padding-left: 20px; background: url(../img/flags/2.gif) no-repeat;" >Norway</option>[/code]

Now that the value is set to "1|United States" and all the others would be similar. When you submit the form, the value of the select field if it was called "country" for example:

[code]<?php
// The value of the countries above split by the pipe (|) character
$country = explode("|", $_POST['country']);

// The var $country is now an array
$country_id = $country[0];
$country_name = $country[1];

// Then run your MySQL query
mysql_query("UPDATE...");
?>[/code]

Hope that helps. :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.