witham Posted July 11, 2006 Share Posted July 11, 2006 Can anyone help I need to select data from a database using two drop down boxes I want the first selection which is for a manufacturer to show all the option but only show the products made by them in the second drop down box instead of all products? Quote Link to comment https://forums.phpfreaks.com/topic/14321-php-select-problem/ Share on other sites More sharing options...
Barand Posted July 11, 2006 Share Posted July 11, 2006 I voted "help". No not that one, the other one.if your db structure is similar to this, with integer id fields, [pre]manufacturer product------------- ---------------mfrID <---+ prodIDmfrName | prodDesc +--- mfrID[/pre]then this will help.|[url=http://members.aol.com/barryaandrew/baaselect/baaselectguide.html]baaSelect[/url]| generate js and php code for dynamic linked dropdownsOr another approach is |[url=http://members.aol.com/barryaandrew/xmlhttp/article.html]AJAX/xmlhttp[/url]| article and sample application Quote Link to comment https://forums.phpfreaks.com/topic/14321-php-select-problem/#findComment-56421 Share on other sites More sharing options...
witham Posted July 11, 2006 Author Share Posted July 11, 2006 Thank you very much for your reply This is the code but I don't know what to add to the sqlPRODNAME to only show the data for that particular manufacturer.// the sql query to select the data for displaying later$sqlMANUFACTURERS = "SELECT MANID, MANNAME FROM man order by MANNAME";$sqlPRODNAME = "SELECT manid, manno, manname, prodno, proddesc from prodname, man where manno = manid order by proddesc;";<form action="weld2.php" method="get"><hr><tr><td colspan = 3><h3 align = "center"><i><b>PRODUCT DELETION</b></i></h3></td><tr><br><br><br><br<tr><td colspan = 2><h2 align=center><i>This screen allows users to add products to W.E.L.D.</h2></td></tr><tr><td><b>Manufacturer:</b></td><?//initiate the $result variable and populate it with the data from the $sqlMANUFACTURERS table$result = mysql_query($sqlMANUFACTURERS, $dblink) or die("<p>Error Processing Query</p><hr /><p>".mysql_error()."</p>\n");// mysql_fetch_array fetches the results from the query a row at a time each time it's called// the result is returned as an array that can be referenced either by field name or by it's index// this data is then put into a drop down box popualted by the updated manufacturers listecho '<td><select name="Manufacturer">';while ($row = mysql_fetch_array ($result)) // loop through the rows outputing them as html table rows { echo '<option value="' . $row["MANID"] . '">' . $row["MANNAME"] . "</option>"; } echo '</select></td></tr>';?><tr><td><b>Product Name:</b></td><?//initiate the $result variable and populate it with the data from the $sqlPRODNAME table$result = mysql_query($sqlPRODNAME, $dblink) or die("<p>Error Processing Query</p><hr /><p>".mysql_error()."</p>\n");// mysql_fetch_array fetches the results from the query a row at a time each time it's called// the result is returned as an array that can be referenced either by field name or by it's index// this data is then put into a drop down box popualted by the updated manufacturers listecho '<td><select name="Product Name">';while ($row = mysql_fetch_array ($result)) // loop through the rows outputing them as html table rows { echo '<option value="' . $row["prodno"] . '">' . $row["proddesc"] . "</option>"; } echo '</select></td></tr>';?> Quote Link to comment https://forums.phpfreaks.com/topic/14321-php-select-problem/#findComment-56431 Share on other sites More sharing options...
Barand Posted July 11, 2006 Share Posted July 11, 2006 If you use the baaSelect class, all you need is[code]<?phpinclude 'baaSelect.php';$sel = new baaSelect();$sel->addSelect('manufacturer', 'man', 'manid', 'manname', '', 1, '- manufacturer -');$sel->addSelect('product', 'prodname', 'prodno', 'proddesc', 'manno', 1, '- product -');?> <html><head><meta name="generator" content="PhpED Version 4.5 (Build 4513)"><title>baaSelect Sample</title><meta name="author" content="Barand"><?php $sel->makeScript() ?></head><body> <form> <table> <tr> <td> Manufacturer </td> <td> <?php $sel->makeSelect('manufacturer'); ?> </td> </tr> <tr> <td> Product </td> <td> <?php $sel->makeSelect('product'); ?> </td> </tr></table><input type="submit" name="submit" value="Submit"></form>?></body></html>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14321-php-select-problem/#findComment-56452 Share on other sites More sharing options...
witham Posted July 13, 2006 Author Share Posted July 13, 2006 [quote author=Barand link=topic=100227.msg395416#msg395416 date=1152651455]If you use the baaSelect class, all you need is[code]<?phpinclude 'baaSelect.php';$sel = new baaSelect();$sel->addSelect('manufacturer', 'man', 'manid', 'manname', '', 1, '- manufacturer -');$sel->addSelect('product', 'prodname', 'prodno', 'proddesc', 'manno', 1, '- product -');?> <html><head><meta name="generator" content="PhpED Version 4.5 (Build 4513)"><title>baaSelect Sample</title><meta name="author" content="Barand"><?php $sel->makeScript() ?></head><body> <form> <table> <tr> <td> Manufacturer </td> <td> <?php $sel->makeSelect('manufacturer'); ?> </td> </tr> <tr> <td> Product </td> <td> <?php $sel->makeSelect('product'); ?> </td> </tr></table><input type="submit" name="submit" value="Submit"></form>?></body></html>[/code][/quote] Quote Link to comment https://forums.phpfreaks.com/topic/14321-php-select-problem/#findComment-57537 Share on other sites More sharing options...
witham Posted July 13, 2006 Author Share Posted July 13, 2006 thanks very much but being a newie I need a little more assistance, I copied and pasted the code you kindly sent but got an error messageWarning: main(baaSelect.php) [function.main]: failed to open stream: No such file or directory in C:\Program Files\xampp\htdocs\delprod.php on line 32Warning: main() [function.include]: Failed opening 'baaSelect.php' for inclusion (include_path='.;C:\Program Files\xampp\php\pear\') in C:\Program Files\xampp\htdocs\delprod.php on line 32Fatal error: Class 'baaSelect' not found in C:\Program Files\xampp\htdocs\delprod.php on line 34The actual code as it stands currently is:Could you please, please tell me where I am going wrong?<?php//prevents cachingheader("Expires: Sat, 01 Jan 2000 00:00:00 GMT");header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");header("Cache-Control: post-check=0, pre-check=0",false);session_cache_limiter();session_start();require('C:\Program Files\xampp\htdocs\config.php');require('C:\Program Files\xampp\htdocs\functions.php');//this is group name or username of the group or person that you wish to allow access to// - please be advise that the Administrators Groups has access to all pages.if (allow_access(Administrators) != "yes"){include ('C:\Program Files\xampp\htdocs\no_access.html');exit;}?><?$dbuser = ''; // your database server user name$dbhost = 'localhost'; // name of the sql server$dbpass = ''; // your database server password$dbname = 'weld'; // the name of the database to connect to include 'baaSelect.php';$sel = new baaSelect();$sel->addSelect('manufacturer', 'man', 'manid', 'manname', '', 1, '- manufacturer -');$sel->addSelect('product', 'prodname', 'prodno', 'proddesc', 'manno', 1, '- product -');?> <html><head><meta name="generator" content="PhpED Version 4.5 (Build 4513)"><title>baaSelect Sample</title><meta name="author" content="Barand"><?php $sel->makeScript() ?></head><body> <form> <table> <tr> <td> Manufacturer </td> <td> <?php $sel->makeSelect('manufacturer'); ?> </td> </tr> <tr> <td> Product </td> <td> <?php $sel->makeSelect('product'); ?> </td> </tr></table><input type="submit" name="submit" value="Submit"></form>?></body></html> Quote Link to comment https://forums.phpfreaks.com/topic/14321-php-select-problem/#findComment-57541 Share on other sites More sharing options...
Barand Posted July 13, 2006 Share Posted July 13, 2006 Have you downloaded baaSelect.php and put it in your includes folder? Quote Link to comment https://forums.phpfreaks.com/topic/14321-php-select-problem/#findComment-57572 Share on other sites More sharing options...
witham Posted July 13, 2006 Author Share Posted July 13, 2006 sorry for my lack of knowledge but how do I do that please? Quote Link to comment https://forums.phpfreaks.com/topic/14321-php-select-problem/#findComment-57585 Share on other sites More sharing options...
Barand Posted July 13, 2006 Share Posted July 13, 2006 Click on the baaSelect link in my sig belowOn the new page, click the "Download class" link and save the zip file on your PCUnzip and put baaSelect.php in your includes folder Quote Link to comment https://forums.phpfreaks.com/topic/14321-php-select-problem/#findComment-57607 Share on other sites More sharing options...
akitchin Posted July 13, 2006 Share Posted July 13, 2006 you have the patience of a saint, barand. Quote Link to comment https://forums.phpfreaks.com/topic/14321-php-select-problem/#findComment-57628 Share on other sites More sharing options...
witham Posted July 14, 2006 Author Share Posted July 14, 2006 barand thank you very much for your help, I am sorry if the questions I asked seemed stupid but sometimes to learn you have to ask someone with greater knowledge than yourself. Once again thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/14321-php-select-problem/#findComment-58058 Share on other sites More sharing options...
Barand Posted July 14, 2006 Share Posted July 14, 2006 Ancient proverb: The only stupid question is the one that wasn't asked Quote Link to comment https://forums.phpfreaks.com/topic/14321-php-select-problem/#findComment-58061 Share on other sites More sharing options...
witham Posted July 14, 2006 Author Share Posted July 14, 2006 Barand another question please what does this mean?Manufacturer No Database SelectedWarning: mysql_free_result(): supplied argument is not a valid MySQL result resource in C:\Program Files\xampp\htdocs\baaSelect.php on line 283Product No Database SelectedWarning: mysql_free_result(): supplied argument is not a valid MySQL result resource in C:\Program Files\xampp\htdocs\baaSelect.php on line 283?> Quote Link to comment https://forums.phpfreaks.com/topic/14321-php-select-problem/#findComment-58075 Share on other sites More sharing options...
Barand Posted July 14, 2006 Share Posted July 14, 2006 After this code[code]$dbuser = ''; // your database server user name$dbhost = 'localhost'; // name of the sql server$dbpass = ''; // your database server password$dbname = 'weld'; // the name of the database to connect to[/code]you need[code]$connection = mysql_connect ($dbhost, $dbuser, $dbpass) or die (mysql_error());mysql_select_db($dbname, $connection) or die (mysql_error());[/code]The easiest thing to do is put all the above code in a file, say, "db.php" then at start of every script that uses the database[code]include 'db.php';[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14321-php-select-problem/#findComment-58082 Share on other sites More sharing options...
witham Posted July 14, 2006 Author Share Posted July 14, 2006 Barand thanks so very much for your help I have learned a great deal from this problem, hopefully one day I will be able to give to this forum instead of merely taking.Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/14321-php-select-problem/#findComment-58103 Share on other sites More sharing options...
Barand Posted July 14, 2006 Share Posted July 14, 2006 As a comparison for you, this is the same problem using ajax.Save this code, which provides a selection of products for a particular manufacturer as "prod_select.php"[code]<?php include 'db.php'; $manid = $_GET['man']; $sql = "SELECT prodno, proddesc FROM prodname WHERE manno='$manid' ORDER BY proddesc"; $res = mysql_query($sql) or die(mysql_error()); echo "<select name='product'>"; echo "<option value='0'>- select product -</option>"; while (list($id, $desc) = mysql_fetch_row($res)) { echo "<option value='$id'> $desc</option>"; } echo "</select>";?>[/code]Now save this main script as "ajax1.php" and run it.[code]<?phpinclude 'db.php';function manOptions($current) { $sql = "SELECT manid, manname FROM man ORDER BY manname"; $res = mysql_query($sql) or die(mysql_error()); $str = ''; while (list($id, $name) = mysql_fetch_row($res)) { $selected = $id==$current ? 'selected' : ''; $str .= "<option value='$id' $selected>$name</option>"; } return $str;}?><html><head><title>AJAX sample</title><meta name="author" content="Barand"><SCRIPT> var xmlhttp=false; try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { xmlhttp = false; } } if (!xmlhttp && typeof XMLHttpRequest!='undefined') { xmlhttp = new XMLHttpRequest(); } function handleMessages (url, resultID) { var obj; xmlhttp.open("GET", url, true); xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4) { obj = document.getElementById(resultID); obj.innerHTML = xmlhttp.responseText; } } xmlhttp.send(0); return 0; } function manChange(val) { var url = "prod_select.php?man=" + val; handleMessages (url, "prod"); }</SCRIPT></head><body><form> <table> <tr> <td> Manufacturer </td> <td> <select name='manufacturer' onchange='manChange(this.value)'> <option value='0'>- select mfr -</option> <?php echo manOptions($man) ?> </select> </td> </tr> <tr> <td> Product </td> <td> <DIV id='prod'> <select name='product'> <option>- select product -</option> </select> <DIV> </td> </tr></table><input type="submit" name="submit" value="Submit"></form></body></html>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14321-php-select-problem/#findComment-58166 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.