Jump to content

help fixing my code


cherubrock74

Recommended Posts

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";  
?>

Link to comment
Share on other sites

$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` ";

Link to comment
Share on other sites

$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?

 

Link to comment
Share on other sites

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)){

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

$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?

Link to comment
Share on other sites

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...

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.