Jump to content

combo box


bunny1

Recommended Posts

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

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

@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

@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

@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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.