Re: Category/SubCategory
| From: | Bobo Wieland | Date: | Fri, 03 Dec 2004 00:05:54 +0000 |
| Subject: | Re: Category/SubCategory | ||
| References: | 1 | Groups: | php.windows |
| Request: | Send a blank email to php-windows+get-25109@lists.php.net to get a copy of this message | ||
Javascript/php seems to work nice for me (i give it to you as it is with no explainations... It's also a bit old so I don't know why I did it just like this and not in any other way):
function advancedSearch_js() {
global $link_id;
print ("<script language='JavaScript1.2' type='text/javascript'>\n");
// RESET_FORM()
print("\tfunction reset_form(theForm) {\n".
"\t\tvar f = theForm;\n".
"\t\tf.namn.value=\"\";\n".
"\t\tf.beskrivning.value=\"\";\n".
"\t\tf.h_grupp.selectedIndex=0;\n".
"\t\tf.u_grupp.options.length=1;\n".
"\t\tf.u_grupp.options[0].value=\"err\";\n".
"\t\tf.u_grupp.options[0].text=\"------------------------------------------\";\n".
"\t\tf.eplanta[1].checked = true;\n".
"\t\tf.zon.selectedIndex=0;\n".
"\t\tf.grannzon.checked = true;\n".
"\t\tf.match[1].checked = true;\n".
"\t}\n\n");
// SWITCH_UGRUPP()
print("\tfunction switch_ugrupp(theForm) {\n".
"\t\tswitch (theForm.h_grupp.value) {\n".
"\t\t\tcase \"alla\":\n".
"\t\t\t\ttheForm.u_grupp.options.length=1;\n\n".
"\t\t\t\ttheForm.u_grupp.options[0].value=\"err\";\n".
"\t\t\t\ttheForm.u_grupp.options[0].text=\"------------------------------------------\";\n".
"\t\t\tbreak;\n");
for ($i = 0; $i <= 6; $i++) { // this is probably some ugly hardcoded values that should be dynamic...
print ("\t\t\tcase \"".$i."\":\n");
$question = "SELECT ug_key AS id, ugrupp_namn AS text FROM sort_ugrupp WHERE f_key = '".$i."' ORDER BY ugrupp_namn";
$result = mysql_query($question, $link_id);
$rows = mysql_num_rows($result);
$j = 1;
print("\t\t\t\ttheForm.u_grupp.options.length=".($rows+1).";\n".
"\t\t\t\ttheForm.u_grupp.options[0].value=\"err\";\n".
"\t\t\t\ttheForm.u_grupp.options[0].text=\"------------------------------------------\";\n");
while ($ug = mysql_fetch_object($result)) {
print ("\t\t\t\ttheForm.u_grupp.options[".$j."].value=\"".$ug->id."\";\n".
"\t\t\t\ttheForm.u_grupp.options[".$j."].text=\"".$ug->text."\";\n");$j++; } mysql_free_result($result); print ("\t\t\tbreak;\n"); } print ("\t\t}\n". "\t\ttheForm.u_grupp.selectedIndex = 0;\n". "\t\treturn true;\n". "\t}\n". "</script>\n"); } So, about presentation of categories I have the following table with three fields setup in a db:
| id | root_id | name | 1 0 cat1 2 1 subcat to cat1 3 2 subcat to subcat 4 0 cat2id is an autoincr.int exept for the root category with a fixed id. root_id is the id for a subcategory's parent category. the root category itself is not presented in the table... all categories with root_id 0 is placed directly in the root... to print out a string looking something like: "cat1 : subcat to cat1 : subcat to subcat" when given the id of "subcat to subcat" I do by the following code: function catPathRec($id, $show_lnk = false, $rtn_val="") { global $link_id; $katname = strtoupper(mysql_result(mysql_query("SELECT kat_namn AS kat FROM text_kat WHERE id = ".$id, $link_id), "kat")); $hid = mysql_result(mysql_query("SELECT h_id AS hkat FROM text_kat WHERE id = ".$id, $link_id), "hkat"); if ($rtn_val == "") { if ($show_lnk) { return ($hid == 0) ? ("<a href='text.php?action=kat&kat_id=".$id."' style='font-weight:normal;' onfocus='this.blur()'>".$katname."</a>") : (catPathRec($hid, $show_lnk, " : <a href='text.php?action=kat&kat_id=".$id."' style='font-weight:normal;' onfocus='this.blur()'>".$katname."</a>")); } else { return ($hid == 0) ? ($katname) : (catPathRec($hid, $show_lnk, " : ".$katname)); } } else if ($hid != 0) { return catPathRec($hid, $show_lnk, " : <a href='text.php?action=kat&kat_id=".$id."' style='font-weight:normal;' onfocus='this.blur()'>".$katname."</a>".$rtn_val); } return "<a href='text.php?action=kat&kat_id=".$id."' style='font-weight:normal;' onfocus='this.blur()'>".$katname."</a>".$rtn_val."\n"; } ----- Original Message ----- From: "Tony Devlin" <tdevlin@airewaves.com> To: "Php-Windows" <php-windows@lists.php.net> Sent: Thursday, December 02, 2004 9:17 PM Subject: [PHP-WIN] Category/SubCategory
Hello to all my fellow PHP friends, I'm embarking on yet another trecherous adventure. This time Categories, Sub-Categories and Sub-Sub Categories.. This is for a shopping cart module where the admin can create categories, sub-categories, and sub-sub categories and then place in a certain sub-sub category. This is a two fold question, first I will draw an example display of what I am trying to accomplish. Example: Main Category| ---> Sub Category | ---> Sub-Sub Category | ---> ProductMain Category2| ---> Sub Category2 | ---> Sub-Sub Category2 | ---> Product2Question 1: Admin needs to be able to create new sub categories and assign them to a main category and be able to create a sub-sub category to assign to a sub category and then a product to assign to a sub-sub category. When adding a new product the admin needs to select a main category which then creates a dropdown box for a subcategory, which then (IF admin chooses) create a dropdown box for a sub-sub category. Any ideas on how to create a function to handle this? I've seen lots of unstable javascript code to do this (not to mention difficult to manage) Question 2: When displaying the category/sub/sub-sub category listing to the viewers, whats the best way to handle the output? Thanks in advance for all your help, Tony Devlin