|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Double drop down in page php using data from mysql (recall)
Hi,
I have just posted my question about create drop down button using data with mysql code. May be the questions are not very clear, so I think that I repost this item one more time. I have two questions. 1- I need to create two drop down in page.php, one for category and the other for article. The data of category and article will be retrieved from mysql. The drop down for article depends on the value selected on category drop down. Actually, the results of my code is only the creation of one drop down category but I cannot retrieve article from data base to create the other drop down for article which is depended to the value selected of the category. How I could change value with php to create article drop down. I tried onChange but it didn't work. <?php include "inc/common_db.inc"; $categorie=$_POST['categorie']; $article=$_POST['description']; $no_article=$_POST['no_article']; $no_categorie=$_POST['no_categorie']; $action=$_POST['action']; // Connection to BD $id_lien = db_connect("novo163_bdevengarsal"); if(!$id_lien) die(sql_error()); //Request categories into BD $categorie = "SELECT no_categorie, categorie FROM categorie_t"; $resultat_cat = mysql_query($categorie); if(!$resultat_cat) error_message(sql_error()); $options_cat=""; //make category drop down while ($row_cat=mysql_fetch_array($resultat_cat)) { $no_categorie=$row_cat["no_categorie"]; $categorie=$row_cat["categorie"]; $options_cat.="<OPTION VALUE=\"$no_categorie\">".$categorie; } ?> //code in html <tr> <td align="left" valign="top"> <select name="categorie"> <Option value=0>Categories <?=$options_cat?> </select> </td> <td align="left" valign="top"> <select name="articles"> <option value=0>Articles </select> </td> 2- I have to add and remove the article of drop down in another area text and otherwise (by add and remove button) to make a list of products, so I liked to know if I have to make an array to keep all items? How I could do that? I am very appreciated about your help. Mr_Bonds |
|
#2
|
|||
|
|||
|
//Perhaps something along these lines:
//The database fields and some variable names are in danish, but you should be able to figure it out anyway :) //quick translation: //grupper = groups //varer = products //navn = name <html> <body> <?php $server = "localhost"; $brugernavn = "******"; $password = "******"; mysql_connect($server, $brugernavn, $password) or die( "Unable to connect\n". mysql_error() ); mysql_select_db("test") or die("Unable to select db ".mysql_error()."\n"); $id = $_GET['id']; echo'<form name="testform">'; $q = mysql_query("SELECT * FROM grupper"); echo"<select name=\"gruppe\" onChange=\"Load_id()\">"; while($row = mysql_fetch_array($q)) { $selected = ($row["gruppe_id"] == $id)? "SELECTED":""; echo"<option value=\"".$row['gruppe_id']."\"". $selected." >".$row['gruppe_navn']."</option>"; } echo"</select>"; $q2 = mysql_query("SELECT * FROM varer WHERE vare_gruppe_id = $id"); echo"<select name=\"vare\">"; while($row = mysql_fetch_array($q2)) { echo"<option value=\"".$row['vare_id']."\">".$row['vare_navn']."</option>"; } echo"</select></form>"; ?> <script type="text/javascript"> function Load_id() { var id = document.testform.gruppe.options[document.testform.gruppe.selectedIndex].value var id_txt = "?id=" location = id_txt + id } </script> </BODY> </HTML> //DB_DUMPS//////////////////////////// CREATE TABLE `grupper` ( `gruppe_id` int(11) NOT NULL auto_increment, `gruppe_navn` varchar(60) NOT NULL default '', PRIMARY KEY (`gruppe_id`) ) TYPE=MyISAM AUTO_INCREMENT=3 ; INSERT INTO `grupper` VALUES (1, 'testgruppe1'); INSERT INTO `grupper` VALUES (2, 'testgruppe2'); CREATE TABLE `varer` ( `vare_id` int(11) NOT NULL auto_increment, `vare_gruppe_id` int(11) NOT NULL default '0', `vare_navn` varchar(60) NOT NULL default '', PRIMARY KEY (`vare_id`) ) TYPE=MyISAM AUTO_INCREMENT=5 ; INSERT INTO `varer` VALUES (1, 1, 'testvare1'); INSERT INTO `varer` VALUES (2, 1, 'testvare2'); INSERT INTO `varer` VALUES (3, 2, 'testvare3'); INSERT INTO `varer` VALUES (4, 2, 'testvare4'); |
|
#3
|
|||
|
|||
|
Mr Bonds - Did this work?
Hello Mr Bonds. I am trying to do much the same and wondering if "dejbjerg's" solution worked? My database tables will eventually continue to grow, so I don't hard coding will be any good - the best way is from the MySql tables, such as:
MAKER makeID makeName Ford 1 Pontiac 2 Chevrolet 3 Hyundai 4 MODELS modelName model_ID makeID Escort 1 1 Mustang 2 1 Firebird 6 2 Camaro 1 3 Elantra 3 4 I would appreciate any help with code to solving this. Many thanks. cjp_qld |
![]() |
| Viewing: Dev Articles Community Forums > Databases > MySQL Development > Double drop down in page php using data from mysql (recall) |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|