bunny1 Posted March 5, 2007 Share Posted March 5, 2007 I am allowing users insert data into a database using a form. I want one of the fields "department" to display all existing departments in the database in a combo box for users to choose from and also have a field in the combo box for them to add a new department. Is there a way for me to do this in using php? I am using php to access the database Thanks Link to comment https://forums.phpfreaks.com/topic/41333-combo-box/ Share on other sites More sharing options...
pocobueno1388 Posted March 5, 2007 Share Posted March 5, 2007 I'm not exactly sure what you mean by "combo box"...but to display all departments from the DB you would do this: <?php $query = mysql_query("SELECT col1, col3, col3 FROM departments"); while ($row = mysql_fetch_assoc($query)){ //Start displaying departments echo $row['col1']; } ?> Link to comment https://forums.phpfreaks.com/topic/41333-combo-box/#findComment-200260 Share on other sites More sharing options...
chronister Posted March 5, 2007 Share Posted March 5, 2007 @pocobueno1388 - a combo box is just the technical name for a drop down or select list box Here is a function I created to populate a drop down box with values from a database. You can modify it to meet your needs. <?php function getcats(){ $query = "SELECT * FROM catagories "; $result = mysql_query($query); $num = mysql_num_rows($result); if ($num > 0){ while($row = mysql_fetch_row($result)){ ?> <option value=<? echo $row[1] ?>><? echo$row[2] ?></option> <? };//end while };//end if }//end function ?> As far as being able to add new items into this dropdown box...I have no idea how to do that...I don't think it is possible but I may be wrong. Link to comment https://forums.phpfreaks.com/topic/41333-combo-box/#findComment-200264 Share on other sites More sharing options...
boo_lolly Posted March 5, 2007 Share Posted March 5, 2007 what does your database table look like? is it just a table with all the possible departments? or is it a table where users have inputed data into the table, so there may be many instances of the same department? Link to comment https://forums.phpfreaks.com/topic/41333-combo-box/#findComment-200292 Share on other sites More sharing options...
Psycho Posted March 5, 2007 Share Posted March 5, 2007 @pocobueno1388 - a combo box is just the technical name for a drop down or select list box. This is HTML that is being created. There is no such thing as a combo-box in HTML. There is a select list. A combo box is a combination of a select list AND a text field (look at the address bar in IE for an example). This type of control does not exist in HTML. Link to comment https://forums.phpfreaks.com/topic/41333-combo-box/#findComment-200350 Share on other sites More sharing options...
chronister Posted March 5, 2007 Share Posted March 5, 2007 @pocobueno1388 - a combo box is just the technical name for a drop down or select list box. This is HTML that is being created. There is no such thing as a combo-box in HTML. There is a select list. A combo box is a combination of a select list AND a text field (look at the address bar in IE for an example). This type of control does not exist in HTML. ahhhhhh... gotcha I am just used to Dreamweaver calling it combo box Link to comment https://forums.phpfreaks.com/topic/41333-combo-box/#findComment-200367 Share on other sites More sharing options...
Barand Posted March 6, 2007 Share Posted March 6, 2007 Process something like this <?php include 'db.php'; if (isset($_GET['action'])) { $dept_id = $_GET['department']; $newdept = $_GET['newdept']; /** * was new dept name entered */ if ($newdept) { /** * is it an existing department */ $res = mysql_query("SELECT dept_id FROM department WHERE department = '$newdept'") or die (mysql_error()); if ($row = mysql_fetch_row($res)) { $dept_id = $row[0]; } /** * if not, add it add get new id */ else { mysql_query("INSERT INTO department(department) VALUES ('$newdept')"); $dept_id = mysql_insert_id(); } } /** * continue processing with $dept_id */ } ?> <form> Select existing department<br> <select name='department'> <option value=''> - select department -</option> <?php $sql = "SELECT dept_id, department FROM department ORDER BY department"; $res = mysql_query($sql) or die (mysql_error().'<p>$sql</p>'); while (list($id, $dept) = mysql_fetch_row($res)) { echo "<option value='$id'> $dept</option>\n"; } ?> </select> <br> or enter new department name<br> <input type='text' name='newdept' size='30'><br> <input type='submit' name='action' value='Submit'> </form> Link to comment https://forums.phpfreaks.com/topic/41333-combo-box/#findComment-200407 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.