Jump to content

Add entries to array


eliZZZa

Recommended Posts

Don´t know, if the title is correct for my request.

What I want to achieve:

A select list in a form should grow with every data entry.

Example:

I have a list of towns to choose from for a single entry.

If there is a town, not in the list, I would like to have the option to enter one in a text field.

Next time I approch the input form, the newly entered town shows up in the select list.

 

What do I have to do, to expand the select list (resp. the table behind)?

 

Thanks in advance and kind regards from Austria

eliZZZa

Link to comment
https://forums.phpfreaks.com/topic/61111-add-entries-to-array/
Share on other sites

Im assuming you want to use MYSQL to handle this unless your a fan of flat file

 

Its pretty simple

 

Query the table of towns to see if the town the user has imputted is already stored in there

 

If its not, insert it

 

You will need to populate your <SELECT> direct from a mysql query aswell, it obviously cant be static

 

Thats how its done, can translate that into code, if not perhaps post a snippet of your html and let one of us turn it into some working dynamic code :)

Link to comment
https://forums.phpfreaks.com/topic/61111-add-entries-to-array/#findComment-304123
Share on other sites

There's no error checking or database handling here, and I haven't tested the code, but it should work:

 

SQL: (State is obviously optional)

Create Table Location(
      LocID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
      City VARCHAR(100),
      State VARCHAR(2)
);

 

PHP for building the options:

(note there isn't any error checking here, and I haven't tested this out, so I'm not sure if it works)

$result = mysql_query("SELECT City FROM Location WHERE STATE='". $State . "'");
echo "<select name='city'>";
while ($record = mysql_fetch_row($result, MYSQL_ASSOC)) {
    $city = $record['City'];
    echo "<option value='$city'>$city</option>";
}
echo "</select>\n";

 

PHP for building adding the new city to the database:

$city = $_POST['City']; // or whatever the name of the text field in the HTML is
$State = ""; // Not sure how you want to handle this.
$result = mysql_query("INSERT INTO Location SET City='". $city . "', State='" . $State ."'");

 

 

Link to comment
https://forums.phpfreaks.com/topic/61111-add-entries-to-array/#findComment-304136
Share on other sites

WOW - I´m overwhelmed by all of your replies! Many thanks, I am right now going, if I get it to work.

Everything runs out of a huge MySQL database anyway, I even managed to set up lookup tables and to query them (I have no idea about PHP, but I am good in copy-paste+logic >;o))

 

I will give feedback, if I succeeded >;o)

eliZZZa

Link to comment
https://forums.phpfreaks.com/topic/61111-add-entries-to-array/#findComment-304312
Share on other sites

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.