Add new entry to a Drop Down Menu in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Catherine
Forum Newbie
Posts: 11
Joined: Mon Feb 09, 2004 2:57 am

Add new entry to a Drop Down Menu in PHP

Post by Catherine »

I have designed a web page using Macromedia Dreamweaver MX with PHP and I would like to know how to enable the users to add another option to the drop down menu at a later date. This problem has been annoying me for so long that any help would be appreciated.

Thanks
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

You'll need somewhere to store the menu options then, what did you have in mind? A flat file, database etc.. ?
Catherine
Forum Newbie
Posts: 11
Joined: Mon Feb 09, 2004 2:57 am

DROP DOWN MENU

Post by Catherine »

It is a form created in Macromedia using HTML code. We are using PHP code to insert the information from the form to a table in MySQL database. We don't store the options in the drop down menu anywhere unless it is selected by the user and then only their option is stored in the database.
Sorry I wasn't to clear.
Thanks for getting back so fast.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

If the options arn't going to change that often, then it might be better just to store them in a text file, rather than hit the database everytime, so you'de want something like this:

Code: Select all

<form method="post" action="">
<select name="thesel">
<?php
$options = file('options.txt'); //this txt file hold the options
foreach($options as $opt){ //loop through each of the options
  echo '<option value="'.$opt.'">'.$opt.'</option>';
}
?>
</select>
</form>
Where options.txt is just a plain file that looks like:

Code: Select all

one
two
three
four
..add a newline to the options.txt file and it will appear as a select option. I hope this is what you meant ;)
Catherine
Forum Newbie
Posts: 11
Joined: Mon Feb 09, 2004 2:57 am

Post by Catherine »

Im really sorry for annoying you! I just tried your suggestion but that outputs the options in a line instead of in a drop down menu.
The following is a piece of my code:
<table>
<tr>
<td width="33"> Year:</td>
<td width="60"><select name="Year2">
<option>2004</option>
<option>2005</option>
</select>
<tr>
</table>
For this i would like to be able enble the users to add in another year. This will be done from another form.
Basically the users have an open form with these years in a drop down menu. At the bottom of this form i have a link to another form which should enables them to add a new year to the existing drop down menu! This is where im having the problem
Sorry and thanks :oops:
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Ok, we're getting closer now ;) But one more question..
If they have a form where they can enter a new year, and this year they enter appears in the drop down box then..is this a permanent thing? i.e Should this year they enter always appear in the list of options, or just for that one time they enter it? I'm just trying to work out if you need to store the options somewhere or if is always a one time thing. Also would you want the years in order, i.e if they enter '2003' as the year, should it appear before 2004? (i'd say yes, but i'm just checking ;))
Catherine
Forum Newbie
Posts: 11
Joined: Mon Feb 09, 2004 2:57 am

Drop Down Menu

Post by Catherine »

Sorry I have been so long getting back but things have been busy busy. If they enter the year we want the year to be permantly entered in the drop down menu with the '2003' before '2004', etc. We also need to store these copies as hard copies some where, not sure where is best so if you have any suggestions please let me know.
Thanks again for your help and replying so quickly
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post by liljester »

what benefit is there to using a text file over mysql, if the site already uses mysql?
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

in that case Catherine, you should use a database to store the new 'item', cuz you'll be having lots of text files if u plan to make one for every user
Post Reply