Log in

View Full Version : fix my php code



cherubrock74
05-12-2009, 01:46 PM
Can someone please help me fix my code?
I can get the first two drop down menus to work but the third (subcategory2)wont display any information...



<?

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

forum_amnesiac
05-12-2009, 02:31 PM
Is it creating the drop down box with '======== Subcategory2 ========' in it.

cherubrock74
05-12-2009, 02:41 PM
Is it creating the drop down box with '======== Subcategory2 ========' in it.

Thanks for the reply...I dont think so...the first two menus are working fine...and they have the ======= in it too...
I tried to get rid of it but no luck...

this is my BD structure if it helps

category: cat_id - category
subcategory: subcat_id - cat_id - subcategory
subcategory2: subcat_id - subcat2

any idea of why it is not working?

forum_amnesiac
05-12-2009, 02:49 PM
Without access to your data it is difficult to debug, I did however get the subcategory2 box when I did the following code change to that section.


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

Are you sure that there are some records in the database with 'subcategory2' as their name, if not, then that will be your problem.

In my test I did hardcode $data to be 'subcategory2'.

cherubrock74
05-12-2009, 03:02 PM
Without access to your data it is difficult to debug, I did however get the subcategory2 box when I did the following code change to that section.


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

Are you sure that there are some records in the database with 'subcategory2' as their name, if not, then that will be your problem.

In my test I did hardcode $data to be 'subcategory2'.

in my database subcategory2 includes subcat_id and subcat2 with the description of the items I would like to display in the third drop down menu...
do I need to change something

forum_amnesiac
05-12-2009, 03:07 PM
Try pasting in the code I posted, to replace from the last 'else if' to 'echo' in your code.

It may not work but it is a step towards finding the problem.

cherubrock74
05-12-2009, 03:14 PM
Try pasting in the code I posted, to replace from the last 'else if' to 'echo' in your code.

It may not work but it is a step towards finding the problem.

If I replace the code I get "Category :
Parse error: syntax error, unexpected $end in /homepages/5/d180349627/htdocs/ajax/locale2.php on line 47"

and like 47 corresponds to ?>

any idea why? EDIT I found why...it was missing a } ... no errors now but still no luck with the third menu

also found another error:
changed
while(list($subcat_id, $subcategory2)=mysql_fetch_array($subcategory2)){
to

while(list($subcat_id, $subcategory2)=mysql_fetch_array($result)){

still no luck...help!

do you need access to more code? how can I provide you more information to help me work this out?

let me know...

forum_amnesiac
05-12-2009, 03:23 PM
Send me some code to create and load a table so I can test your code

cherubrock74
05-12-2009, 03:24 PM
Send me some code to create and load a table so I can test your code

This database will eventually be deleted...so if you want I can private message you the access...
let me know...

cherubrock74
05-12-2009, 03:33 PM
Send me some code to create and load a table so I can test your code

Can you show me what code you used when you were able to get the subcategory2?
Also could you show me the DB structure for the data you tested on?

forum_amnesiac
05-12-2009, 05:00 PM
it's ok to PM me with the access details, I didn't run it against a database, I just set the variable to test the logic of the code

cherubrock74
05-12-2009, 08:54 PM
PM sent let me know when you can

forum_amnesiac
05-13-2009, 09:38 AM
I found a few errors in your code for subcategory2, but this code worked for me.


} 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`, `subcat2` FROM subcategory2 WHERE `subcat_id` = '$val2' AND subcat_id LIKE '$val%' ORDER BY `subcat2` ");
while(list($subcat_id, $subcat2)=mysql_fetch_array($result)){
echo "<option value=\"$subcat_id\" >$subcat2</option> \n" ;
}
}
}
echo "</select>\n";
?>

Fit this into the right place in your code and give it a test

cherubrock74
05-14-2009, 08:24 PM
I switched the code like you suggested and the third drop down menu is now working, however there is no correspondece yet between the selection made in the second drop down with the items displayed in the third...
I am trying to tweak the code you provided me to see if I can get it to work...
thanks because it is a great move forward towards the solution...
Right now I am looking at phpmyadmin for the DB to understand why the other records from subcategory2 are not showing at all...only certain initial records are displayed, indipendently from what selection is made in drop down menu #2