dflow Posted November 22, 2009 Share Posted November 22, 2009 what am i doing wrong? i want to populate the country list according to the category i want to switch in case CategoryID=1 and CatID2=2 in the country_list table then call on "alpha" to populate the dropdown list via ajax the form: <!-- The following file was created by Iwo Kadziela at TECHROAM: http://www.techroam.com This file uses MySql database and AJAX to create autopopulating dynamic select boxes. <html> <head> <script src="request.js"></script> <script type="text/javascript"> function getfirst(dd1) { clearSelect = document.getElementById('thirdbox'); clearSelect.options.length = 1; //above code clears the third select box when re-clicking the first select box var idx = dd1.selectedIndex; var first = dd1[idx].value; var par = document.forms["theform"]; var parelmts = par.elements; var prezsel = parelmts["secondbox"]; if (first != "Select Something One") { Http.get({ url: "./alpha.php?alpha=" + first + "%", callback: fillPrez, cache: Http.Cache.Get }, [prezsel]); } } function getsecond(dd1) { var idx = dd1.selectedIndex; var second = dd1[idx].text; var par = document.forms["theform"]; var parelmts = par.elements; var prezsel = parelmts["thirdbox"]; if (second != "Select Something Two") { Http.get({ url: "./beta.php?state=" + second, callback: fillPrez, cache: Http.Cache.Get }, [prezsel]); } } function fillPrez(xmlreply, prezelmt) { if (xmlreply.status == Http.Status.OK) { var prezresponse = xmlreply.responseText; var prezar = prezresponse.split("|"); prezelmt.length = 1; prezelmt.length = prezar.length; for (o=1; o < prezar.length; o++) { prezelmt[o].text = prezar[o]; prezelmt[o].value = prezar[o]; } } else { alert("Cannot handle the AJAX call."); } } function getthird(dd1,dd2) { var idx = dd1.selectedIndex; var third = dd1[idx].text; if (third != "Select The Final") { Http.get({ url: "./final.php?state=" + dd2 + "&city=" + third, callback: getit, cache: Http.Cache.Get }); } } function getit(xmlreply){ if (xmlreply.status == Http.Status.OK) { document.getElementById("final").innerHTML= xmlreply.responseText; } else { alert("Cannot handle the AJAX call."); } } </script> <style> body { width:800px; margin: 0px auto; text-align:center; } p {display:inline;margin:20px;} div {clear:both;width:800px;border:1px solid #999;padding:20px;margin:20px 0px 20px 0px;} </style> </head> <body> <form name="theform"> <p> <select name="firstbox" size="13" style="position: relative; width: 215px;" onChange="getfirst(this);"> <option value="">Select Something One</option> <?php do { ?> <option value="<?php echo $row_RsCategories['CategoryID']?>"><?php echo $row_RsCategories['CategoryName']?></option> <?php } while ($row_RsCategories = mysql_fetch_assoc($RsCategories)); $rows = mysql_num_rows($RsCategories); if($rows > 0) { mysql_data_seek($RsCategories, 0); $row_RsCategories = mysql_fetch_assoc($RsCategories); } ?> </select> </p> <p> <select name="secondbox" size="13" style="position: relative; width: 215px;" onChange="getsecond(this);"> <option>Select Something Two</option> </select> </p> <p> <select id="thirdbox" name="thirdbox" size="13" style="position: relative; width: 215px;" onChange="getthird(this, document.theform.secondbox.options[document.theform.secondbox.selectedIndex].value);"> <option>Select The Final</option> </select> </p> </form> <center>THE FINAL CONTENT PULLED FROM DATABASE</center> <div id="final"></div> </body> </html> <?php mysql_free_result($RsCategories); ?> alpha.php </php $dbconnection = mysql_connect ("localhost", "root", "") or die ('I cannot connect to the database because: ' . mysql_error()); mysql_select_db ("international", $dbconnection) or die("Couldn't open database: ".mysql_error()); $alpha=$_GET['alpha']; switch ($alpha) { case 1: $result = mysql_query("SELECT CountryName from country_list WHERE CategoryID = '".$_GET['alpha']."'"); break; case 2: $result = mysql_query("SELECT CountryName from country_list WHERE CatID2 = '".$_GET['alpha']."'"); break; while ($row = mysql_fetch_array($result)) { echo "|" . $row['CountryName']; } ?> Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted November 22, 2009 Share Posted November 22, 2009 You're breaking from the switch conditions, which ends the switch clause.... but your while ($row = mysql_fetch_array($result)) {echo "|" . $row['CountryName']; line is actually before the } that closes the switch, but after the breaks Quote Link to comment Share on other sites More sharing options...
dflow Posted November 23, 2009 Author Share Posted November 23, 2009 You're breaking from the switch conditions, which ends the switch clause.... but your while ($row = mysql_fetch_array($result)) {echo "|" . $row['CountryName']; line is actually before the } that closes the switch, but after the breaks ok moved the line after the break and get nothing still Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted November 23, 2009 Share Posted November 23, 2009 ok moved the line after the break and get nothing still Check that your SQL queries do actually return data using PHPMyAdmin or similar Quote Link to comment Share on other sites More sharing options...
dflow Posted November 23, 2009 Author Share Posted November 23, 2009 ok moved the line after the break and get nothing still Check that your SQL queries do actually return data using PHPMyAdmin or similar queries work without a problem Quote Link to comment Share on other sites More sharing options...
cags Posted November 23, 2009 Share Posted November 23, 2009 You're breaking from the switch conditions, which ends the switch clause.... but your while ($row = mysql_fetch_array($result)) {echo "|" . $row['CountryName']; line is actually before the } that closes the switch, but after the breaks ok moved the line after the break and get nothing still So what does the code now look like? Quote Link to comment Share on other sites More sharing options...
dflow Posted November 23, 2009 Author Share Posted November 23, 2009 You're breaking from the switch conditions, which ends the switch clause.... but your while ($row = mysql_fetch_array($result)) {echo "|" . $row['CountryName']; line is actually before the } that closes the switch, but after the breaks ok moved the line after the break and get nothing still So what does the code now look like? <?php // The following file was created by Iwo Kadziela at TECHROAM: http://www.techroam.com // The following code assumes that localhost is your host, root is your username, you do not have a password, and test is the name of your database // Please make sure that these fields set in accordance with your database settings $dbconnection = mysql_connect ("localhost", "root", "") or die ('I cannot connect to the database because: ' . mysql_error()); mysql_select_db ("international", $dbconnection) or die("Couldn't open database: ".mysql_error()); $alpha=$_GET['alpha']; switch ($alpha) { case 1: $result = mysql_query("SELECT CountryName from country_list WHERE CategoryID = '".$_GET['alpha']."'"); break; case 2: $result = mysql_query("SELECT CountryName from country_list WHERE CatID2 = '".$_GET['alpha']."'"); break; } while ($row = mysql_fetch_array($result)) { echo "|" . $row['CountryName']; ?> Quote Link to comment Share on other sites More sharing options...
Anzeo Posted November 23, 2009 Share Posted November 23, 2009 Check if your alpha is set (isset($_GET['alpha']) and add a default case to your switch (default: ... break;) Quote Link to comment Share on other sites More sharing options...
cags Posted November 23, 2009 Share Posted November 23, 2009 Also try amending your queries to take this format... $sql = "SELECT CountryName from country_list WHERE CategoryID = '".$_GET['alpha']."'"; $result = mysql_query() or trigger_error("SQL: {$sql}, ERROR: " . mysql_query(), E_USER_ERROR); Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted November 23, 2009 Share Posted November 23, 2009 so, when you run that code above (with a closing } to your while() loop of course) as a standalone script (not being called by AJAX or anything), you get no results? if so, add: or trigger_error (mysql_error()); to your queries. as well, you should have a default: case in your switch() statement to catch any anomalies: <?php switch ($action) { case one: //stuff; break; case two: //stuff; break; default: //will show if no case is matched; break; } ?> Quote Link to comment Share on other sites More sharing options...
dflow Posted November 23, 2009 Author Share Posted November 23, 2009 Also try amending your queries to take this format... $sql = "SELECT CountryName from country_list WHERE CategoryID = '".$_GET['alpha']."'"; $result = mysql_query() or trigger_error("SQL: {$sql}, ERROR: " . mysql_query(), E_USER_ERROR); this following code works without the switch - gets for example $alpha =1 and shows all countries in the first category $result = mysql_query("SELECT CountryName from country_list WHERE CategoryID = '".$_GET['alpha']."'"); while ($row = mysql_fetch_array($result)) { echo "|" . $row['CountryName']; } ?> all queries work in the the data base the code you gave me does not work and trigger's no error Quote Link to comment Share on other sites More sharing options...
Anzeo Posted November 23, 2009 Share Posted November 23, 2009 Not sure if this has anything to do with it, but in your case 2, the query is as follows: mysql_query("SELECT CountryName from country_list WHERE CatID2 = '".$_GET['alpha']."'"); But in case 1 you use "CategoryID" as your field in the WHERE clause, could this have anything to do with? If so you do not need a switch, so I guess this was pointless to point out. Quote Link to comment Share on other sites More sharing options...
dflow Posted November 23, 2009 Author Share Posted November 23, 2009 Not sure if this has anything to do with it, but in your case 2, the query is as follows: mysql_query("SELECT CountryName from country_list WHERE CatID2 = '".$_GET['alpha']."'"); But in case 1 you use "CategoryID" as your field in the WHERE clause, could this have anything to do with? If so you do not need a switch, so I guess this was pointless to point out. i still want to get the country list per category i need to set CatID2 (true/false) CatID3 (true/false) for there are countries that are in several lists and some that are not Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted November 23, 2009 Share Posted November 23, 2009 Not sure if this has anything to do with it, but in your case 2, the query is as follows: mysql_query("SELECT CountryName from country_list WHERE CatID2 = '".$_GET['alpha']."'"); But in case 1 you use "CategoryID" as your field in the WHERE clause, could this have anything to do with? If so you do not need a switch, so I guess this was pointless to point out. i still want to get the country list per category i need to set CatID2 (true/false) CatID3 (true/false) for there are countries that are in several lists and some that are not so, what is the field type of CatID2, 3, 4, etc.? boolean or numeric (int/varchar, etc.)? Quote Link to comment Share on other sites More sharing options...
dflow Posted November 23, 2009 Author Share Posted November 23, 2009 Not sure if this has anything to do with it, but in your case 2, the query is as follows: mysql_query("SELECT CountryName from country_list WHERE CatID2 = '".$_GET['alpha']."'"); int But in case 1 you use "CategoryID" as your field in the WHERE clause, could this have anything to do with? If so you do not need a switch, so I guess this was pointless to point out. i still want to get the country list per category i need to set CatID2 (true/false) CatID3 (true/false) for there are countries that are in several lists and some that are not so, what is the field type of CatID2, 3, 4, etc.? boolean or numeric (int/varchar, etc.)? Quote Link to comment Share on other sites More sharing options...
Anzeo Posted November 23, 2009 Share Posted November 23, 2009 Could you explain exactly why you need that much columns? Seems to me, that something is wrong with your data structure. Quote Link to comment Share on other sites More sharing options...
dflow Posted November 23, 2009 Author Share Posted November 23, 2009 i have 3 categories i want to break down into category-->country-->region or city villas -->list of countries that have villas-->REGION in chosen country apartment -->list of countries that have apartments-->CITY in chosen country ski accommodations -->list of countries that have ski accommodations-->REGION in chosen country so the list for countries that have villas isn't the same as the list that have apartments and ski accomodations Quote Link to comment Share on other sites More sharing options...
Anzeo Posted November 23, 2009 Share Posted November 23, 2009 Correct me if I'm wrong, but all accommodations are in the same table(country_list)? Woudn't it be easier to have just one column which would indicate your category? This column could be 1,2 or 3 according to which category you want. This way you do not need a switch. Or am I totally missing the ball here? Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted November 23, 2009 Share Posted November 23, 2009 well, there is absolutely no reason this code should not return results from either query assuming $_GET['alpha'] makes a match in the db to one of the two fields in question, CategoryID, and CatID2, right? this isn't rocket science. if $_GET['alpha'] == 1, then CategoryID must equal 1 or you get no results, else if $_GET['alpha'] == 2, then CatID2 must equal 2. if $_GET['alpha'] equals ANYTHING else, you will get no return. unless i am completely missing something here, or you are leaving something crucial out. otherwise, this code *should work. <?php // The following file was created by Iwo Kadziela at TECHROAM: http://www.techroam.com // The following code assumes that localhost is your host, root is your username, you do not have a password, and test is the name of your database // Please make sure that these fields set in accordance with your database settings $dbconnection = mysql_connect ("localhost", "root", "") or die ('I cannot connect to the database because: ' . mysql_error()); mysql_select_db ("international", $dbconnection) or die("Couldn't open database: ".mysql_error()); $alpha=$_GET['alpha']; switch ($alpha) { case 1: $result = mysql_query("SELECT CountryName from country_list WHERE CategoryID = '".$_GET['alpha']."'"); break; case 2: $result = mysql_query("SELECT CountryName from country_list WHERE CatID2 = '".$_GET['alpha']."'"); break; } while ($row = mysql_fetch_array($result)) { echo "|" . $row['CountryName']; } ?> Quote Link to comment Share on other sites More sharing options...
dflow Posted November 23, 2009 Author Share Posted November 23, 2009 well, there is absolutely no reason this code should not return results from either query assuming $_GET['alpha'] makes a match in the db to one of the two fields in question, CategoryID, and CatID2, right? this isn't rocket science. if $_GET['alpha'] == 1, then CategoryID must equal 1 or you get no results, else if $_GET['alpha'] == 2, then CatID2 must equal 2. if $_GET['alpha'] equals ANYTHING else, you will get no return. unless i am completely missing something here, or you are leaving something crucial out. otherwise, this code *should work. <?php // The following file was created by Iwo Kadziela at TECHROAM: http://www.techroam.com // The following code assumes that localhost is your host, root is your username, you do not have a password, and test is the name of your database // Please make sure that these fields set in accordance with your database settings $dbconnection = mysql_connect ("localhost", "root", "") or die ('I cannot connect to the database because: ' . mysql_error()); mysql_select_db ("international", $dbconnection) or die("Couldn't open database: ".mysql_error()); $alpha=$_GET['alpha']; switch ($alpha) { case 1: $result = mysql_query("SELECT CountryName from country_list WHERE CategoryID = '".$_GET['alpha']."'"); break; case 2: $result = mysql_query("SELECT CountryName from country_list WHERE CatID2 = '".$_GET['alpha']."'"); break; } while ($row = mysql_fetch_array($result)) { echo "|" . $row['CountryName']; } ?> i agree in the same notion the results say otherwise if i use the switch i get only the first case and that's it so the switch does not work Quote Link to comment Share on other sites More sharing options...
Anzeo Posted November 23, 2009 Share Posted November 23, 2009 A switch always returns only one case, that's the point of a switch Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted November 23, 2009 Share Posted November 23, 2009 switch() a switch statement is similar to IF/ELSE statements. <?php if ($_GET['alpha'] == 1) { //do query; } elseif ($_GET['alpha'] == 2) { //do query; } ?> Quote Link to comment Share on other sites More sharing options...
dflow Posted November 23, 2009 Author Share Posted November 23, 2009 A switch always returns only one case, that's the point of a switch sure the idea of this script is to switch between options on the fly Quote Link to comment Share on other sites More sharing options...
dflow Posted November 27, 2009 Author Share Posted November 27, 2009 ok i figured out the first part it is switching now now the second dropdown isnt passing a value if i enter: beta.php?CountryID=2 i get correct results echoed in the index.php i get a list of countries now i would like to select the country in the list and get in the third dropdown either a city_list or a region_list currently im not getting anything mybe it's the ajax? how can i ADD options value to the echoed dropdown? if i change var second = dd1[idx].value; to var second = dd1[idx].text; i get the result by CountryName but i would like to be according to CountryID index.php: <?php require_once('../Connections/international.php'); ?> <?php mysql_query("SET NAMES 'utf8'")?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; } $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } mysql_select_db($database_international, $international); $query_RsCategories = "SELECT CategoryID, CategoryName, CategoryName_heb FROM category_list ORDER BY CategoryName ASC"; $RsCategories = mysql_query($query_RsCategories, $international) or die(mysql_error()); $row_RsCategories = mysql_fetch_assoc($RsCategories); $totalRows_RsCategories = mysql_num_rows($RsCategories); ?> <!-- The following file was created by Iwo Kadziela at TECHROAM: http://www.techroam.com <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script src="request.js"></script> <script type="text/javascript"> function getfirst(dd1) { clearSelect = document.getElementById('thirdbox'); clearSelect.options.length = 1; //above code clears the third select box when re-clicking the first select box var idx = dd1.selectedIndex; var first = dd1[idx].value; var par = document.forms["theform"]; var parelmts = par.elements; var prezsel = parelmts["secondbox"]; if (first != "Select Something One") { Http.get({ url: "./alpha_switch.php?alpha=" + first + "%", callback: fillPrez, cache: Http.Cache.Get }, [prezsel]); } } function getsecond(dd1) { var idx = dd1.selectedIndex; var second = dd1[idx].value; var par = document.forms["theform"]; var parelmts = par.elements; var prezsel = parelmts["thirdbox"]; if (second != "Select Something Two") { Http.get({ url: "./beta.php?CountryID=" + second, callback: fillPrez, cache: Http.Cache.Get }, [prezsel]); } } function fillPrez(xmlreply, prezelmt) { if (xmlreply.status == Http.Status.OK) { var prezresponse = xmlreply.responseText; var prezar = prezresponse.split("|"); prezelmt.length = 1; prezelmt.length = prezar.length; for (o=1; o < prezar.length; o++) { prezelmt[o].text = prezar[o]; prezelmt[o].value = prezar[o]; } } else { alert("Cannot handle the AJAX call."); } } function getthird(dd1,dd2) { var idx = dd1.selectedIndex; var third = dd1[idx].value; if (third != "Select The Final") { Http.get({ url: "./final.php?state=" + dd2 + "&city=" + third, callback: getit, cache: Http.Cache.Get }); } } function getit(xmlreply){ if (xmlreply.status == Http.Status.OK) { document.getElementById("final").innerHTML= xmlreply.responseText; } else { alert("Cannot handle the AJAX call."); } } </script> <style> body { width:800px; margin: 0px auto; text-align:center; } p {display:inline;margin:20px;} div {clear:both;width:800px;border:1px solid #999;padding:20px;margin:20px 0px 20px 0px;} </style> </head> <body> <form name="theform"> <p> <select name="firstbox" size="13" style="position: relative; width: 215px;" onChange="getfirst(this);"> <option value="">Select Something One</option> <?php do { ?> <option value="<?php echo $row_RsCategories['CategoryID']?>"><?php echo $row_RsCategories['CategoryName']?></option> <?php } while ($row_RsCategories = mysql_fetch_assoc($RsCategories)); $rows = mysql_num_rows($RsCategories); if($rows > 0) { mysql_data_seek($RsCategories, 0); $row_RsCategories = mysql_fetch_assoc($RsCategories); } ?> </select> </p> <p> <select name="secondbox" size="13" style="position: relative; width: 215px;" onChange="getsecond(this);"> <option>Select Something Two</option> </select> </p> <p> <select id="thirdbox" name="thirdbox" size="13" style="position: relative; width: 215px;" onChange="getthird(this, document.theform.secondbox.options[document.theform.secondbox.selectedIndex].value);"> <option>Select The Final</option> </select> </p> </form> <center>THE FINAL CONTENT PULLED FROM DATABASE</center> <div id="final"></div> </body> </html> <?php mysql_free_result($RsCategories); ?> the beta.php <?php // The following file was created by Iwo Kadziela at TECHROAM: http://www.techroam.com // The following code assumes that localhost is your host, root is your username, you do not have a password, and test is the name of your database // Please make sure that these fields set in accordance with your database settings $dbconnection = mysql_connect ("localhost", "root", "") or die ('I cannot connect to the database because: ' . mysql_error()); mysql_select_db ("international", $dbconnection) or die("Couldn't open database: ".mysql_error()); if (isset($CountryID)) { } $result = mysql_query("SELECT city_list.CityName from city_list WHERE CountryID = '".$_GET['CountryID']."'"); while ($row = mysql_fetch_array($result)) { echo "|" . $row['CityName']; } ?> alpha_switch.php <?php mysql_query("SET NAMES 'utf8'")?> <?php // The following file was created by Iwo Kadziela at TECHROAM: http://www.techroam.com // The following code assumes that localhost is your host, root is your username, you do not have a password, and test is the name of your database // Please make sure that these fields set in accordance with your database settings $dbconnection = mysql_connect ("localhost", "root", "") or die ('I cannot connect to the database because: ' . mysql_error()); mysql_select_db ("international", $dbconnection) or die("Couldn't open database: ".mysql_error()); $alpha=$_GET['alpha']; if (isset($alpha)) { echo "This var is set so I will print."; } switch ($alpha) { case 1: $result = mysql_query("SELECT country_list.CountryName,country_list.CountryName_heb from country_list WHERE CategoryID = '".$_GET['alpha']."'"); break; case 2: $result = mysql_query("SELECT country_list.CountryName,country_list.CountryName_heb from country_list WHERE CatID2 = '".$_GET['alpha']."'"); break; case 3: $result = mysql_query("SELECT country_list.CountryName,country_list.CountryName_heb from country_list WHERE CatID3 = '".$_GET['alpha']."'"); break; case 4: $result = mysql_query("SELECT country_list.CountryName,country_list.CountryName_heb from country_list WHERE CatID4 = '".$_GET['alpha']."'"); break; case 5: $result = mysql_query("SELECT country_list.CountryName,country_list.CountryName_heb from country_list WHERE CatID5 = '".$_GET['alpha']."'"); break; } while ($row = mysql_fetch_array($result)) { echo "|" .$row['CountryName']; } ?> Quote Link to comment 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.