cherubrock74 Posted May 12, 2009 Share Posted May 12, 2009 Can someone please help me to fix my code? It works three drop down menus but only the first two are showing information correctly. The third one (saubcategory2) does not display any information. Here is the code I am using, let me know if you need more: <? header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header ("Cache-Control: no-cache, must-revalidate"); header ("Pragma: no-cache"); header("content-type: application/x-javascript; charset=tis-620"); $data=$_GET['data']; $val=$_GET['val']; $dbhost = "????????????"; $dbuser = "????????????"; $dbpass = "????????????"; $dbname = "????????????"; mysql_pconnect($dbhost,$dbuser,$dbpass) or die ("Unable to connect to MySQL server"); if ($data=='category') { echo "<select name='category' onChange=\"dochange('subcategory', this.value)\">\n"; echo "<option value='0'>==== Category ====</option>\n"; $result=mysql_db_query($dbname,"select `cat_id`, `category` from category order by `category`"); while(list($cat_id, $category)=mysql_fetch_array($result)){ echo "<option value=\"$cat_id\" >$category</option> \n" ; } } else if ($data=='subcategory') { echo "<select name='subcategory' onChange=\"dochange('subcategory2', this.value)\">\n"; echo "<option value='0'>======== Subcategory ========</option>\n"; $val2=$val; $val = substr($val,0,2); $result=mysql_db_query($dbname,"SELECT `cat_id`, `subcategory` FROM subcategory WHERE 'cat_id' != '$val2' AND cat_id LIKE '$val%' ORDER BY 'subcategory' "); while(list($cat_id, $subcategory)=mysql_fetch_array($result)){ echo "<option value=\"$cat_id\" >$subcategory</option> \n" ; } } else if ($data=='subcategory2') { echo "<select name='subcategory2' >\n"; echo "<option value='0'>======== Subcategory2 ========</option>\n"; $val2=$val; $val = substr($val,0,4); $result=mysql_db_query($dbname,"SELECT `subcat_id` FROM subcategory2 WHERE `subcat_id` != '$val2' AND subcat_id LIKE '$val%' ORDER BY `subcategory2` "); while(list($subcat_id, $subcategory2)=mysql_fetch_array($subcategory2)){ echo "<option value=\"$subcat_id\" >$subcategory2</option> \n" ; } } echo "</select>\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/157820-help-fixing-my-code/ Share on other sites More sharing options...
redarrow Posted May 12, 2009 Share Posted May 12, 2009 echo your query out and add the mysql_error() function. Quote Link to comment https://forums.phpfreaks.com/topic/157820-help-fixing-my-code/#findComment-832369 Share on other sites More sharing options...
cherubrock74 Posted May 12, 2009 Author Share Posted May 12, 2009 echo your query out and add the mysql_error() function. Thanks for the reply...I am a beginner...sorry! could you please provide me some code? Quote Link to comment https://forums.phpfreaks.com/topic/157820-help-fixing-my-code/#findComment-832379 Share on other sites More sharing options...
Adam Posted May 12, 2009 Share Posted May 12, 2009 $result = mysql_db_query($dbname,"SELECT `subcat_id` FROM subcategory2 WHERE `subcat_id` != '$val2' AND subcat_id LIKE '$val%' ORDER BY `subcategory2` ") or die(mysql_error()); ... if no error is returned try printing out the SQL: print "SELECT `subcat_id` FROM subcategory2 WHERE `subcat_id` != '$val2' AND subcat_id LIKE '$val%' ORDER BY `subcategory2` "; Quote Link to comment https://forums.phpfreaks.com/topic/157820-help-fixing-my-code/#findComment-832386 Share on other sites More sharing options...
cherubrock74 Posted May 12, 2009 Author Share Posted May 12, 2009 $result = mysql_db_query($dbname,"SELECT `subcat_id` FROM subcategory2 WHERE `subcat_id` != '$val2' AND subcat_id LIKE '$val%' ORDER BY `subcategory2` ") or die(mysql_error()); ... if no error is returned try printing out the SQL: print "SELECT `subcat_id` FROM subcategory2 WHERE `subcat_id` != '$val2' AND subcat_id LIKE '$val%' ORDER BY `subcategory2` "; Tried to add the string you suggested but no error... how do I "print" the SQL? where should I input the print string in my code? Quote Link to comment https://forums.phpfreaks.com/topic/157820-help-fixing-my-code/#findComment-832396 Share on other sites More sharing options...
cherubrock74 Posted May 12, 2009 Author Share Posted May 12, 2009 Thought it may help if I post the structure of my DB here it is: I have category, subcategory and subcategory2 category includes: cat_id - category subcategory includes: subcat_id - cat_id - subcategory subcategory2 includes: subcat_id - subcat2 Quote Link to comment https://forums.phpfreaks.com/topic/157820-help-fixing-my-code/#findComment-832400 Share on other sites More sharing options...
Adam Posted May 12, 2009 Share Posted May 12, 2009 Just spotted problem I think, change: while(list($subcat_id, $subcategory2)=mysql_fetch_array($subcategory2)){ To: while(list($subcat_id, $subcategory2)=mysql_fetch_array($result)){ Quote Link to comment https://forums.phpfreaks.com/topic/157820-help-fixing-my-code/#findComment-832405 Share on other sites More sharing options...
cherubrock74 Posted May 12, 2009 Author Share Posted May 12, 2009 Just spotted problem I think, change: while(list($subcat_id, $subcategory2)=mysql_fetch_array($subcategory2)){ To: while(list($subcat_id, $subcategory2)=mysql_fetch_array($result)){ I switched but had no luck any other error you may find? do you need more code? this is the ajax script if it may help <? echo "<form name=sel>\n"; echo "Category : <font id=category><select>\n"; echo "<option value='0'>============</option> \n" ; echo "</select></font>\n"; echo "Subcategory : <font id=subcategory><select>\n"; echo "<option value='0'>==== Subcategory ====</option> \n" ; echo "</select></font>\n"; echo "Subcategory2 : <font id=subcategory2><select>\n"; echo "<option value='0'>==== Subcategory2 ====</option> \n" ; echo "</select></font>\n"; ?> <script language=Javascript> function Inint_AJAX() { try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {} //IE try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE try { return new XMLHttpRequest(); } catch(e) {} //Native Javascript alert("XMLHttpRequest not supported"); return null; }; function dochange(src, val) { var req = Inint_AJAX(); req.onreadystatechange = function () { if (req.readyState==4) { if (req.status==200) { document.getElementById(src).innerHTML=req.responseText; } } }; req.open("GET", "locale2.php?data="+src+"&val="+val); req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header req.send(null); } window.onLoad=dochange('category', -1); </script> Quote Link to comment https://forums.phpfreaks.com/topic/157820-help-fixing-my-code/#findComment-832412 Share on other sites More sharing options...
Ken2k7 Posted May 12, 2009 Share Posted May 12, 2009 $result=mysql_db_query($dbname,"SELECT `cat_id`, `subcategory` FROM subcategory WHERE 'cat_id' != '$val2' AND cat_id LIKE '$val%' ORDER BY 'subcategory' "); See the quotes around cat_id after the WHERE clause? Take them down. Quote Link to comment https://forums.phpfreaks.com/topic/157820-help-fixing-my-code/#findComment-832417 Share on other sites More sharing options...
cherubrock74 Posted May 12, 2009 Author Share Posted May 12, 2009 $result=mysql_db_query($dbname,"SELECT `cat_id`, `subcategory` FROM subcategory WHERE 'cat_id' != '$val2' AND cat_id LIKE '$val%' ORDER BY 'subcategory' "); See the quotes around cat_id after the WHERE clause? Take them down. If I take them down the second menu wont work anymore...with the quotes back is working... for MrAdam: you were right that code was an error but even if I changed I had no luck... Any other idea? Do you need anything else to investigate? Quote Link to comment https://forums.phpfreaks.com/topic/157820-help-fixing-my-code/#findComment-832437 Share on other sites More sharing options...
Ken2k7 Posted May 12, 2009 Share Posted May 12, 2009 So you want $val2 to not be equal to the string cat_id or the column cat_id? If you mean the column, then take out the quotes. It's possible that "it" not working is from other causes. Quote Link to comment https://forums.phpfreaks.com/topic/157820-help-fixing-my-code/#findComment-832445 Share on other sites More sharing options...
cherubrock74 Posted May 12, 2009 Author Share Posted May 12, 2009 So you want $val2 to not be equal to the string cat_id or the column cat_id? If you mean the column, then take out the quotes. It's possible that "it" not working is from other causes. Ok let me provide you more info... in my DB I have: category which includes: cat_id - category (category has the descriptions displayed in the drop down menu) subcategory which includes: subcat_id - cat_id - subcategory (subcategory has the descriptions displayed in the drop down menu) subcategory2 which includes: subcat_id - subcat2 (subcat2 has the descriptions I want to display and that are not showing in the third drop down menu) The original code I am using was referring to a DB that had 1 column and many rows...maybe that's why it was using the !=val2 If there is a simpler way to have my third menu working please let me know how to change the code... Quote Link to comment https://forums.phpfreaks.com/topic/157820-help-fixing-my-code/#findComment-832462 Share on other sites More sharing options...
cherubrock74 Posted May 12, 2009 Author Share Posted May 12, 2009 I am very close to accomplish what I need but I need help with the code please... the fact that the data i want to display on the third drop down menu for subcategory2 is under a row called subcat2 makes a difference? Should I consequently change anything in the code? please let me know thank you Quote Link to comment https://forums.phpfreaks.com/topic/157820-help-fixing-my-code/#findComment-832490 Share on other sites More sharing options...
cherubrock74 Posted May 12, 2009 Author Share Posted May 12, 2009 Can someone please look at my code again and help me get the third menu populated? my database structure category which includes: cat_id - category (category has the descriptions displayed in the drop down menu) subcategory which includes: subcat_id - cat_id - subcategory (subcategory has the descriptions displayed in the drop down menu) subcategory2 which includes: subcat_id - subcat2 (subcat2 has the descriptions I want to display and that are not showing in the third drop down menu) code so far including previous suggestions (3rd drop down menu not displaying any info) <? header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header ("Cache-Control: no-cache, must-revalidate"); header ("Pragma: no-cache"); header("content-type: application/x-javascript; charset=tis-620"); $data=$_GET['data']; $val=$_GET['val']; $dbhost = "??????????"; $dbuser = "??????????"; $dbpass = "??????????"; $dbname = "?????????????"; mysql_pconnect($dbhost,$dbuser,$dbpass) or die ("Unable to connect to MySQL server"); if ($data=='category') { echo "<select name='category' onChange=\"dochange('subcategory', this.value)\">\n"; echo "<option value='0'>==== Category ====</option>\n"; $result=mysql_db_query($dbname,"select `cat_id`, `category` from category order by `category`"); while(list($cat_id, $category)=mysql_fetch_array($result)){ echo "<option value=\"$cat_id\" >$category</option> \n" ; } } else if ($data=='subcategory') { echo "<select name='subcategory' onChange=\"dochange('subcategory2', this.value)\">\n"; echo "<option value='0'>======== Subcategory ========</option>\n"; $val2=$val; $val = substr($val,0,2); $result=mysql_db_query($dbname,"SELECT `cat_id`, `subcategory` FROM subcategory WHERE 'cat_id' != '$val2' AND cat_id LIKE '$val%' ORDER BY 'subcategory' "); while(list($cat_id, $subcategory)=mysql_fetch_array($result)){ echo "<option value=\"$cat_id\" >$subcategory</option> \n" ; } } else { if ($data=='subcategory2') { echo "<select name='subcategory2' >\n"; echo "<option value='0'>======== Subcategory2 ========</option>\n"; $val2=$val; $val = substr($val,0,4); $result=mysql_db_query($dbname,"SELECT `subcat_id` FROM subcategory2 WHERE `subcat_id` != '$val2' AND subcat_id LIKE '$val%' ORDER BY `subcategory2` "); while(list($subcat_id, $subcat2)=mysql_fetch_array($result)){ echo "<option value=\"$subcat_id\" >$subcat2</option> \n" ; } } } echo "</select>\n"; ?> please help! thank you Quote Link to comment https://forums.phpfreaks.com/topic/157820-help-fixing-my-code/#findComment-832522 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.