Jump to content

php dropdown lists


Guest

Recommended Posts

I'm trying to work out an easier way (for me) to select categories and sub-categories. What I'm trying to do is create two dropdown menus. The first will list all of my main categories and the second will list only the sub-categories below the main category.

I've gotten the main category dropdown list to work fine, but I have no idea how to link the sub-category drop down list to it, so that it will populate based on my main category selection.

Anybody have any idea how to do this in php?

Link to comment
Share on other sites

That site looks great OS.

Question on the Dynamic Tree installation you did,

If a customer adds new categories and subs to their cart after installing and configuring the DHTML tree in the .php page, will the script you used have to be modified in the PHP page to reflect the update, or does the script you used allow for it to rebuild on it's own?

Link to comment
Share on other sites

  • 2 weeks later...

do some javascirpt, way easier and less time consuming somehting like i did on www.hardtimehusslin.com

Can someone tell me where to edit the box menus, without upsetting things.

Would apreciate a little help, so that I can change mine to a diffrent level.

Also is it possible to add more custom boxes.

Cheers in advance -

Love the design you have

Link to comment
Share on other sites

  • 4 months later...
Guest rickbradford

I'm trying to work out an easier way (for me) to select categories and sub-categories. What I'm trying to do is create two dropdown menus. The first will list all of my main categories and the second will list only the sub-categories below the main category.

I've gotten the main category dropdown list to work fine, but I have no idea how to link the sub-category drop down list to it, so that it will populate based on my main category selection.

Hi, Booker. This is exactly what I would like to do and since I haven't had any luck in my search yet, I thought I'd reply to this thread.

Is there an answer to this problem? I would very much like to use one dropdown menu for my main categories and a second for sub-categories categories -- but only second-tier categories, if possible. To be specific, I sell books and comics and I'd like to use a second dropdown for a list of the authors I stock (all of which I've entered into the shop as second-tier sub-categories).

It seems like it should be pretty simple but I don't know php and I don't know how to identify the sub-categories in the code.

Any suggestions would be much appreciated. In the meantime, I'll keep searching through the archives.

cheers,

--Rb

Link to comment
Share on other sites

What I did was remove the current system for selecting a category, and replaced it with a two-tier system. I edited both the admin/add_product.php and admin/edit_product.php files using the following code:

//This new form is for selecting the category and sub-category:

$id = $_GET['id'];

$id2 = $_GET['id2'];

echo'<form name="catform">';

$maincat = mysql_query("SELECT * FROM ".$prefix."store_category where cat_father_id = '0' order by $cat_order");

echo"<select name=\"cat_father_id\" onChange=\"Load_id()\">

<option value=\"\">Select a Category</option>";

while($row = mysql_fetch_array($maincat)) {

$id_dd = ($row["cat_id"] == $id)? "SELECTED":"";

$cat_father_id_dd=$row[cat_id];

$cat1=$row[category];

echo"<option value=\"$cat_father_id_dd\"". $id_dd.">";

print"$cat1";

unset($cat1);

echo"</option>";

}

echo"</select>";

print"<font color=\"990000\"><b>*</b></font>";

echo"</td></tr>



<tr bgcolor=\"$colour_3\">

    <td width=\"200\" valign=\"top\"><b>Sub-$la_category:</b></td>

    <td width=\"400\" valign=\"top\">";

$subcat = mysql_query("SELECT * FROM ".$prefix."store_category where cat_father_id = $id order by $cat_order");

echo"<select name=\"cat_id\" onChange=\"Load_id()\">

<option value=\"999\">Select a Sub-Category</option>";

while($row = mysql_fetch_array($subcat)) {

$id2_dd = ($row["cat_id"] == $id2)? "SELECTED":"";

$cat_id_dd=$row[cat_id];

$cat2=$row[category];

echo"<option value=\"$cat_id_dd\"". $id2_dd.">";

print"$cat2";

unset($cat2);

echo"</option>";

}

echo"</select>";

print"<font color=\"990000\"><b>*</b></font>";

echo"</td></tr>

</form>";

?>



<script language="JavaScript">

<!--

function Load_id()

{

var id = document.catform.cat_father_id.options[document.catform.cat_father_id.selectedIndex].value

var id2 = document.catform.cat_id.options[document.catform.cat_id.selectedIndex].value

var id_txt = "?id="

var id2_txt = "&id2="

location = id_txt + id + id2_txt + id2

}



var ns6=document.getElementById&&!document.all



function restrictinput(maxlength,e,placeholder){

if (window.event&&event.srcElement.value.length>=maxlength)

return false

else if (e.target&&e.target==eval(placeholder)&&e.target.value.length>=maxlength){

var pressedkey=/[a-zA-Z0-9\.\,\/]/ //detect alphanumeric keys

if (pressedkey.test(String.fromCharCode(e.which)))

e.stopPropagation()

}

}



function countlimit(maxlength,e,placeholder){

var theform=eval(placeholder)

var lengthleft=maxlength-theform.value.length

var placeholderobj=document.all? document.all[placeholder] : document.getElementById(placeholder)

if (window.event||e.target&&e.target==eval(placeholder)){

if (lengthleft<0)

theform.value=theform.value.substring(0,maxlength)

placeholderobj.innerHTML=lengthleft

}

}





function displaylimit(theform,thelimit){

var limit_text='<b><span id="'+theform.toString()+'">'+thelimit+'</span></b> <?echo"characters remaining on your description limit";?> '

if (document.all||ns6)

document.write(limit_text)

if (document.all){

eval(theform).onkeypress=function(){ return restrictinput(thelimit,event,theform)}

eval(theform).onkeyup=function(){ countlimit(thelimit,event,theform)}

}

else if (ns6){

document.body.addEventListener('keypress', function(event) { restrictinput(thelimit,event,theform) }, true);

document.body.addEventListener('keyup', function(event) { countlimit(thelimit,event,theform) }, true);

}

}



//-->

</script>





<?php




You have to place this outside of the current form (I think you can't run one form inside another form...), so what I did was put this code at the top of the table, then remove the <form> tag from the beginning of the table and place it directly underneath this code. Within that second form, you will need to add the following two hidden fields to pass your category details along:




      <input type=\"hidden\" name=\"cat_id\" value=\"$id2\">

      <input type=\"hidden\" name=\"cat_father_id\" value=\"$id\">

I can't remember if any other changes were needed to do this.. :whistle: It's been a while (say, about, 5 months...)

Link to comment
Share on other sites

Guest rickbradford

This has proven to be a bit much for me so I've decided to go with your Manufacturers List mod instead! It pretty much does what I need it to do, anyway.

Never fear, however, I've posted some questions about that one in the appropriate thread!

Thanks again,

--Rb

Link to comment
Share on other sites

Guest twisted

Found it when i was looking for something similar for my shipping_zones mod. Wasn't QUITE what i was after, but I filed it away for future reference. ;)

and yvw.

Link to comment
Share on other sites

Guest Abyss

Is there anyway this can be implimented with the search box? Select your category, search for a specific item, and instead of 50 other results coming up for OTHER categories, it will filter them out and only display similar named items in the category selected?

Link to comment
Share on other sites

I don't think you would want a chained select for the search feature, unless you *really* want to narrow your options. If you want a drop-down list containing your categories, that shouldn't be too hard to add to the search function.

I don't have time to go into details on a mod at the moment, but basically, create a simple drop-down of your categories, in the header file, within your search form, where the category id number is used as the passed on variable. Then, in your search.php file, make sure the code where you are calling the inventory data in the database includes "where cat_id=$category" or whatever variable you used.

Link to comment
Share on other sites

Guest Abyss

Lol, uhm, I kinda' got what you were saying... But im still lost... I don't really know what Im calling and where in the search.php .. :)

Link to comment
Share on other sites

These are the lines you'll need to change in search.php:

Line 191:

$query_count = "select * from ".$prefix."store_inventory where $like";


to:


$query_count = "select * from ".$prefix."store_inventory where $like AND cat_id='$category'";




Line 203:


$query = "select * from ".$prefix."store_inventory where $like LIMIT $limitvalue, $limit ";


to:


$query = "select * from ".$prefix."store_inventory where $like AND cat_id='$category' LIMIT $limitvalue, $limit ";

Remember to change '$category' to whatever name you used for your variable in your drop-down list.

Link to comment
Share on other sites

  • 4 months later...

These are the lines you'll need to change in search.php:

Line 191:

$query_count = "select * from ".$prefix."store_inventory where $like";


to:


$query_count = "select * from ".$prefix."store_inventory where $like AND cat_id='$category'";




Line 203:


$query = "select * from ".$prefix."store_inventory where $like LIMIT $limitvalue, $limit ";


to:


$query = "select * from ".$prefix."store_inventory where $like AND cat_id='$category' LIMIT $limitvalue, $limit ";

Remember to change '$category' to whatever name you used for your variable in your drop-down list.

I did change the lines that Booker listed and that does help with the search. It seemed to cutoff a good 25% of the non-sense that would appear in the searched listing.

Thanks Booker!

Link to comment
Share on other sites

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.   Paste as plain text instead

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