|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
Hi,
I have a table in a database and I would like to populate a dropdown menu with some fields from this table, so that a user can select from them and then a search or a display page would be produced on the basis of their selections..... I am fairly new to php, but have a MySQL database to which I can add/edit/del and produce results from, but am unsure how to do the above.. All help appreciated - please can you make any replies easy to follow..! Thank You |
|
#2
|
|||
|
|||
|
Try something along the lines of:
PHP Code:
Hope that helps Last edited by chakotha : December 18th, 2002 at 03:17 PM. |
|
#3
|
|||
|
|||
|
Hi,
Thanks for the reply - it was spot on. I have used your code, which was very easy to understand and have now got a drop down menu which is populated from my database. However currently it doesn't do anything as yet.. I would also like to add a second drop down menu and possibly even a third.... One menu might be a country, one a town, and one a village for example and then based on their selections after hitting a submit button a page would be returned showing an entrie(s) from the database that match their 'search' criteria as they selected from the drop down menus.... Yet again I haven't a clue how to do this - I could presumably populate the drop down menus now, but wouldn't know how to link them up to produce a results page.. Any more help would be greatly appreciated! |
|
#4
|
|||
|
|||
|
I'm new to php myself but use something similar on my site when doing a search and use the following code assuming the 3 dropdown boxes are named a, b and c
PHP Code:
Like I say i'm new to scripting so someone may have a better solution but the above works for me. |
|
#5
|
|||
|
|||
|
Quote:
Reading this post answered my questions, however, if it is going to function as intended for input, you will need to close the option tag like this: $options.="<OPTION VALUE=\"$id\">".$thing.'</option>'; The former produces a dropdown but won't produce legitimate code for picking a value for input into the database. I played with this a bit before I figured it out. I thought I would post to save others some time. |
|
#6
|
|||
|
|||
|
Damn you're dead right - hope not too much confusion caused!
Also it would be better to use <SELECT NAME=id> |
|
#7
|
|||
|
|||
|
Eureka.
I'm new to php/mysql... but I've gotten this to work... kinda.
The code, as present, works fine. I have a page that draws from multiple tables into multiple different dropdown boxes. This is an admin page that will allow an admin to create characters on the fly for a MMO. The admin will input things such as name, age, etc himself, but will use the dropdown boxes to select from a set of predefined options. For instance, one my tables is 'occupation'. I want the admin to be able to choose an occupation from all the selections in this table. The table is lined out as such: typeID / name / description 1 / artist / I paint stuff. 2 / janitor / I clean stuff 3 / boxer / I hit stuff I have a page addCharacter.php that populates a dropdown box with the 'name'(s) of the occupations listed in table 'occupation'. What I'm having difficulty doing, is when the admin chooses a profession for his character, I need it to create a character and assign the corresponding 'occupationID" into the 'occupation' in the 'characters' table. For instance. Each character in my 'character' table has the following: characterID / name / age / occupation a record might look like this: 1 / Mike Tyson / 34 / 3 .. where the '3' in the in 'occupation' column of the 'characters' table is a foreign key to 'occupationID' in the 'occupation' table and actually represents 'boxer'. Does this make sense? Confusing I know. So, the problem I'm having, is when the admin selects 'boxer' when they are creating a character, I need my form to input in the 'character' table the value '3' in the 'occupation' column... and I can't get it to do that.. it just enters a '0'. (I can, however, get all other records to update i.e. name, age, etc. I can create a new character just fine.. I just cannot get that value to input correctly). Here is the code I have: //this code is bringing in the values for the dropdown. $sql="SELECT name FROM occupation"; $result=mysql_query($sql); $options=""; while ($row=mysql_fetch_array($result)) { $id2=$row["occupationId"]; $occupationName=$row["name"]; $options.="<OPTION VALUE=\"$id2\">".$occupationName.'</option>'; <br>Name: <br><input type="text" name="name" <?php echo "value=\"$name\"" ?>> <br> <br>Age: <br><input type="text" name="age" <?php echo "value=\"$age\"" ?>> <br> <br>Occupation: <br><SELECT NAME=id2> <OPTION VALUE=0>Choose <?=$options?> </SELECT> <br> Again... I need the corresponding numeral value of whatever occupation the user selects to be entered into the table.. not the name of the occupation. So if the admin creates 'janitor', I need the value '2' input into the characters 'occupation' column in the 'characters' table. Thanks in advance for your help.. sorry for the lengthy explanation. |
|
#8
|
|||
|
|||
|
*sigh*
Happens more than I care to admit.. but I figured this out about an hour after posting this.
Thanks!! |
|
#9
|
|||
|
|||
|
Needed echo
Had an "unexpected =" when using your code at:
<OPTION VALUE=0> -> <?php=$options?> </select></td> So I replaced the "=" with "echo" (I'm a noob, so let me know if this is wrong) to get this: <OPTION VALUE=0> <?php echo $options ?> </select></td> Works fine for me now, I just hope it returns both "id" and "thing" in the array itself. |
|
#10
|
|||
|
|||
|
I tried this using the following code, but didn't get anything other than a value of 'Choose' in my dropdown box:
Code:
<?
$sql="SELECT id, name FROM artist";
$result=mysql_query($sql);
$options="";
while ($row=mysql_fetch_array($result)) {
$id=$row["id"];
$name=$row["name"];
$options.="<OPTION VALUE=\"$id\">".$name.'</option>';
}
?>
<SELECT NAME=id>
<OPTION VALUE=0>Choose
<? echo $options?>
</SELECT>
The table I am trying to connect to is called artist, and has the two fields (id and name). Is it something obvious that I'm missing here? *I have tried this without the single quotes around </option> *I have tried this with = instead of echo |
|
#11
|
|||
|
|||
|
Greetings,
I would greatly appreciate any help on this problem I am having. ...Here is the working code that I currently have for creating a drop down menu that is populated by a table... PHP Code:
...Here is the code that displays the drop down menu... PHP Code:
Here is my question... Does anyone know how to turn this piece of code into a multiple drop down menu? Meaning a user would select an option from the first drop down menu. Based off of that users selection another drop down menu would populate from a table underneath of it. For example a user selects a State, then is able to select a city located inside of that state, then is able to select an area code within that city... Something along those lines. So essentially all three menu's are populated from tables in a database, but the queries for those drop down menu's are based off of drop down menu selections. I would greatly appreciate any solutions if available. |
|
#12
|
|||
|
|||
|
Im having trouble with this script. The info gets put into the variable fine enough, but while in a certain area of the page, it doesn't get added to the box. heres a copy of what i have...
Code:
foreach($category['games'] as $game){
echo '<tr>
<td'.$class.' style="width:6%">'.$game['gId'].'</td>
<td'.$class.'>'.$game['gName'].'</td>
<td'.$class.'>'.$game['gSwfFile'].'</td>
<td'.$class.' style="width:8%">';
if($game['gVisible'] == 1){
echo 'yes <a href="?cmd=dovisible&gid='.$game['gId'].'&value=0">Hide</a>';
} else {
echo 'no <a href="?cmd=dovisible&gid='.$game['gId'].'&value=1">Show</a>';
}
echo '</td>
<td'.$class.'>'.$game['Played'].' times <a href="'.$_SERVER['PHP_SELF'].'?cmd=reset&id='.$game['gId'].'">(Reset)</a></td>
<td'.$class.' style="width:7%"><a href="'.$_SERVER['PHP_SELF'].'?cmd=delg&id='.$game['gId'].'">Delete</a></td>
<td'.$class.' style="width:10%"><a href="'.$_SERVER['PHP_SELF'].'?cmd=upg&id='.$game['gId'].'">Up</a> |
<a href="'.$_SERVER['PHP_SELF'].'?cmd=downg&id='.$game['gId'].'">Down</a></td>
<td>
<select name="id">
<?php echo $catopts?>
</select>
</td>
</tr>';
}
echo '</table>';
Catops is the variable with the info. When i tried this at the end of the page, outside of everything, it worked fine. Also, how do i DO things with this? IE i want to make a button that applies the settings when pushed. |
![]() |
| Viewing: Dev Articles Community Forums > Databases > MySQL Development > Drop Down Menu - populated from a mysql database |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|