SunQuest
 
           MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
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  
Old December 18th, 2002, 10:13 AM
steve55 steve55 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: UK
Posts: 44 steve55 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Send a message via AIM to steve55 Send a message via Yahoo to steve55
Exclamation Drop Down Menu - populated from a mysql database

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

Reply With Quote
  #2  
Old December 18th, 2002, 11:17 AM
chakotha chakotha is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 2 chakotha User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Try something along the lines of:

PHP Code:
<?
...
mysql cnx code
...

$sql="SELECT id, thing FROM table";
$result=mysql_query($sql);

$options="";

while (
$row=mysql_fetch_array($result)) {

    
$id=$row["id"];
    
$thing=$row["thing"];
    
$options.="<OPTION VALUE=\"$id\">".$thing;
}
?>
...
html code
...

<SELECT NAME=thing>
<OPTION VALUE=0>Choose
<?=$options?>
</SELECT>
...


Hope that helps

Last edited by chakotha : December 18th, 2002 at 03:17 PM.

Reply With Quote
  #3  
Old December 19th, 2002, 03:29 AM
steve55 steve55 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: UK
Posts: 44 steve55 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Send a message via AIM to steve55 Send a message via Yahoo to steve55
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!

Reply With Quote
  #4  
Old December 23rd, 2002, 03:50 PM
wap wap is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 20 wap User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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:
 $query_aConnection "SELECT * FROM tablename
WHERE field1='$_GET[a]' 
AND field2='$_GET[b]' 
AND field3='$_GET[c]' 
ORDER BY id DESC"



Like I say i'm new to scripting so someone may have a better solution but the above works for me.

Reply With Quote
  #5  
Old March 4th, 2004, 11:03 PM
cygtoad cygtoad is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 1 cygtoad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Wink

Quote:
Originally Posted by chakotha
PHP Code:
<?
...
mysql cnx code
...

$sql="SELECT id, thing FROM table";
$result=mysql_query($sql);

$options="";

while (
$row=mysql_fetch_array($result)) {

    
$id=$row["id"];
    
$thing=$row["thing"];
    
$options.="<OPTION VALUE=\"$id\">".$thing;
}
?>
...
html code
...

<SELECT NAME=thing>
<OPTION VALUE=0>Choose
<?=$options?>
</SELECT>
...


Hope that helps



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.

Reply With Quote
  #6  
Old March 5th, 2004, 02:57 AM
chakotha chakotha is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 2 chakotha User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Damn you're dead right - hope not too much confusion caused!

Also it would be better to use

<SELECT NAME=id>

Reply With Quote
  #7  
Old January 27th, 2005, 10:42 PM
smudge smudge is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 2 smudge User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 28 m 7 sec
Reputation Power: 0
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.

Reply With Quote
  #8  
Old January 28th, 2005, 12:00 AM
smudge smudge is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 2 smudge User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 28 m 7 sec
Reputation Power: 0
*sigh*

Happens more than I care to admit.. but I figured this out about an hour after posting this.

Thanks!!

Reply With Quote
  #9  
Old February 11th, 2005, 12:26 PM
Sneef Sneef is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 1 Sneef User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 49 sec
Reputation Power: 0
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.

Reply With Quote
  #10  
Old May 26th, 2005, 09:31 AM
stubbsini stubbsini is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 8 stubbsini User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 47 m 15 sec
Reputation Power: 0
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

Reply With Quote
  #11  
Old June 25th, 2007, 01:42 PM
sublime82 sublime82 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Location: Washington D.C.
Posts: 1 sublime82 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 41 m 46 sec
Reputation Power: 0
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:
 $sqlOptions "SELECT title, id FROM table";
    
$resultOptions mysql_query($sqlOptions);
    
$example[] = mosHTML::makeOption('X'"Select a Category");
    
    while(
$opt mysql_fetch_array($resultOptions))
        {
            
$example[] = mosHTML::makeOption($opt['id'], $opt['title']);
        }    
    
$lists["tableid"] = mosHTML::selectList($example"text"'class="inputbox" 
size="1"'
"value""text"$opt['title']);
    unset(
$example); 


...Here is the code that displays the drop down menu...

PHP Code:
<?php echo $lists['tableid']; ?>


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.

Reply With Quote
  #12  
Old October 21st, 2007, 09:39 PM
Entity Entity is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2007
Posts: 1 Entity User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m 16 sec
Reputation Power: 0
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.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMySQL Development > Drop Down Menu - populated from a mysql database


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |