GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Aug-2004, 12:04
mytwocents mytwocents is offline
New Member
 
Join Date: Aug 2004
Posts: 1
mytwocents is on a distinguished road

Pulldown menu with data from 2 tables or 1 with an ENUM choice?


Not sure the best way to go about what I want to do so I'll start with just trying to explain what I ultimatley am trying to do. I would like to have a database in which people enter certain information about a univerisity of their choice.... Rather than have a field which has every possible university listed (which would be a lot of names, many of which would never be chosen) I'd like to have a pulldown list that holds names of perhaps the more popular universities which I would initially put in, and then if the university is not there, they would have the option of adding it and this would then be added to the preset list of names. I'm not sure if I should use 2 tables, one which would hold the main data and one that would just hold the university names? Or would I have 1 table with an ENUM field which would have the possible choices...? (this would also have to allow for NEW choices to be added though).
  #2  
Old 10-Aug-2004, 16:30
tubedogg tubedogg is offline
New Member
 
Join Date: Aug 2004
Posts: 11
tubedogg will become famous soon enough
Use two tables, one for storing university names that are 'known', and one for storing each user's choice (assuming you are going to be storing each user's choice).

Table 1 would have a university ID and the university's name.
Table 2 would have a field for the university's ID alongside the other data you're storing for each user.

Something like this:
PHP Code:

<?php

mysql_connect('localhost', 'username', 'password');
mysql_select_db('mydatabase');

if ($_REQUEST['cmd'] == 'list') {
    echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'">'."\n";
    echo '<input type="hidden" name="cmd" value="update" />'."\n";
    echo '<table border="0" cellpadding="0" cellspacing="0">'."\n";
    echo '<tr><td>Choose a university, or enter your own.</td></tr>'."\n";
    echo "<tr><td>\n";
    echo 'Choose: <select name="universityid">'."\n";
    $all = mysql_query("SELECT * FROM university ORDER BY name");
    while ($uni = mysql_fetch_array($all)) {
        echo '<option value="'.$uni['universityid'].'">'.$uni['name'].'</option>'."\n";
    }
    echo "</select><br />\n";
    echo 'Or enter your own: <input type="text" name="other" value="" size="30" />'."\n";
    echo "</td></tr>\n<td><tr>\n";
    echo '<input type="submit" name="submit" value="Submit" />'."\n";
    echo "</table>\n</form>";
}

if ($_POST['cmd'] == 'update') {
    if (!empty($_POST['other'])) {
        mysql_query("INSERT INTO university SET name = '".addslashes($_POST['other'])."'");
        $id = mysql_insert_id();
    } else {
        $id = intval($_POST['universityid']);
    }
    mysql_query("INSERT INTO userchoice SET universityid = $id");
}

mysql_close();

?>


Obviously this is simplified but you get the idea.
 
 

Recent GIDBlog2nd Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 15:13

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:49.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.