X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Changing design (https://forum.x-cart.com/forumdisplay.php?f=51)
-   -   Search Upgrade (https://forum.x-cart.com/showthread.php?t=3684)

machnhed1 07-21-2003 07:00 PM

Search Upgrade
 
Hi Everyone,

I have modified the advanced search so that the customer can search not only the main categories, but also the first level of sub categories. For example currently x-cart has:
Code:

All
Books
CD - DVD - Video
Computer hardware
Electronics
Games and toys
Household
Men Clothes
Software
Sport


I modified is so that it has the following:
Code:

All
Books
  Books/Internet
  Books/Software
CD - DVD - Video
  CD - DVD - Video/Subcat1
  CD - DVD - Video/Subcat2
etc...


Here's how I did it. I modified categories.php by adding the following...
Code:

#
# Mod: Get all main categories and level 1 subcatgories and store them in an array
#
$categories_data1 = func_query("select * from $sql_tbl[categories] where category NOT LIKE '%/%' and avail='Y' group by $sql_tbl[categories].categoryid order by order_by");

foreach ($categories_data1 as $key=>$value) {
        $categories_data2[$key][] = func_query("select * from $sql_tbl[categories] where category LIKE '".$value[category]."/%' and category NOT LIKE '".$value[category]."/%/%' and avail='Y' group by $sql_tbl[categories].categoryid order by order_by");
        $categories_data2[$key][category] = strrchr($categories_data2[$key][category], "/");
        }


Then I modified the customer/main/advancedsearch.tpl by replacing the drop down box code with the following:
Code:

<select name="in_category">
          <option value="">All</option>         
{section name=cat_num loop=$categories_level_1}
          <option value="{ $categories_level_1[cat_num].categoryid}" {if $smarty.get.in_category eq $categories_level_1[cat_num].categoryid or $cat eq $categories_level_1[cat_num].categoryid}selected{/if}>
          {$categories_level_1[cat_num].category|escape}
          </option>

{section name=cat_num1 loop=$categories_level_2}
          {if $categories_level_2[cat_num][0][cat_num1].categoryid ne ""}<option value="{ $categories_level_2[cat_num][0][cat_num1].categoryid}" {if $smarty.get.in_category eq $categories_level_2[cat_num][0][cat_num1].categoryid or $cat eq $categories_level_2[cat_num][0][cat_num1].categoryid}selected{/if}>
          {$categories_level_2[cat_num][0][cat_num1].category|escape}
          </option>{/if}     
{/section}
                         
{/section}
        </select>


My question is this: Can anyone figure out how I can modify the subcategories so that only the subcategory shows up in the drop down box? So it would be -
Code:

All
Books
  Internet
  Software
CD - DVD - Video
  Subcat1
  Subcat2
etc...


I would really appreciate any help I am brain farting at the moment. So, if you like code return a favor :)

Thanks everyone.

funkydunk 07-22-2003 11:15 PM

{$categories_level_2[cat_num][0][cat_num1].category_name|escape}

I spend ages working that one out for a drop down menu system for a site. :lol:

Nice coding 8)

machnhed1 07-23-2003 05:43 AM

FD,

Thanks for the complement.

I tried implementing your solution. It printed the values of the select options, but not the select options.

I don't have a category_name column in my table (3.3.6) so that may be the problem. Here is a list of my xcart_categories table columns:

categoryid
image_x
image_y
category
description
meta_tags
avail
views_stats
order_by
membership
threshold_bestsellers

Any ideas?

Thanks again :!:

funkydunk 07-23-2003 06:21 AM

There isn't in mine either - but it does a little ditty with the dbase query in include/categories.php:

This may be missing from your coding :)

funkydunk 07-23-2003 06:28 AM

My categories.php code: (version 3.4.2)

Code:

<?
/*****************************************************************************\
+-----------------------------------------------------------------------------+
| X-Cart                                                                      |
| Copyright (c) 2001-2003 Ruslan R. Fazliev <rrf@rrf.ru>                      |
| All rights reserved.                                                        |
+-----------------------------------------------------------------------------+
| PLEASE READ  THE FULL TEXT OF SOFTWARE LICENSE AGREEMENT IN THE "COPYRIGHT" |
| FILE PROVIDED WITH THIS DISTRIBUTION. THE AGREEMENT TEXT IS ALSO AVAILABLE  |
| AT THE FOLLOWING URL: http://www.x-cart.com/license.php                    |
|                                                                            |
| THIS  AGREEMENT  EXPRESSES  THE  TERMS  AND CONDITIONS ON WHICH YOU MAY USE |
| THIS SOFTWARE  PROGRAM  AND  ASSOCIATED  DOCUMENTATION  THAT  RUSLAN  R. |
| FAZLIEV (hereinafter  referred to as "THE AUTHOR") IS FURNISHING  OR MAKING |
| AVAILABLE TO YOU WITH  THIS  AGREEMENT  (COLLECTIVELY,  THE  "SOFTWARE").  |
| PLEASE  REVIEW  THE  TERMS  AND  CONDITIONS  OF  THIS  LICENSE AGREEMENT |
| CAREFULLY  BEFORE  INSTALLING  OR  USING  THE  SOFTWARE.  BY INSTALLING, |
| COPYING  OR  OTHERWISE  USING  THE  SOFTWARE,  YOU  AND  YOUR  COMPANY |
| (COLLECTIVELY,  "YOU")  ARE  ACCEPTING  AND AGREEING  TO  THE TERMS OF THIS |
| LICENSE  AGREEMENT.  IF  YOU    ARE  NOT  WILLING  TO  BE  BOUND BY THIS |
| AGREEMENT, DO  NOT INSTALL OR USE THE SOFTWARE.  VARIOUS  COPYRIGHTS  AND |
| OTHER  INTELLECTUAL  PROPERTY  RIGHTS    PROTECT  THE  SOFTWARE.  THIS |
| AGREEMENT IS A LICENSE AGREEMENT THAT GIVES  YOU  LIMITED  RIGHTS  TO  USE |
| THE  SOFTWARE  AND  NOT  AN  AGREEMENT  FOR SALE OR FOR  TRANSFER OF TITLE.|
| THE AUTHOR RETAINS ALL RIGHTS NOT EXPRESSLY GRANTED BY THIS AGREEMENT.      |
|                                                                            |
| The Initial Developer of the Original Code is Ruslan R. Fazliev            |
| Portions created by Ruslan R. Fazliev are Copyright (C) 2001-2003          |
| Ruslan R. Fazliev. All Rights Reserved.                                    |
+-----------------------------------------------------------------------------+
\*****************************************************************************/

#
# $Id: categories.php,v 1.46.2.1 2003/06/02 11:57:44 svowl Exp $
#

#
# For users some categories may be disabled
#

if ($current_area == "C") {
        $membership_condition = " AND ($sql_tbl[categories].membership='$user_account[membership]' OR $sql_tbl[categories].membership='') ";
} else {
        $membership_condition = "";
}

$categories_data = func_query("select $sql_tbl[categories].*, SUBSTRING_INDEX($sql_tbl[categories].category, '\/', -1) as category_name from $sql_tbl[categories] ".($current_area=="C"?"where $sql_tbl[categories].avail='Y' $membership_condition ":"")." group by $sql_tbl[categories].categoryid order by ".($current_area=="C" ? "order_by" : "category"));


#
# Put all categories into $all_categories array and find current_category
# by categoryid stored in $cat
#
if(!$categories_data) $categories_data = array();


foreach($categories_data as $k=>$category_data) {
        if ($current_area == "C") {
                $int_res = func_query_first("SELECT * FROM $sql_tbl[categories_lng] WHERE code='$store_language' AND categoryid='$category_data[categoryid]'");
                if ($int_res["category"])
                        $categories_data[$k]["category_name"] = $category_data["category_name"] = $int_res["category"];
                if ($int_res["description"])
                        $categories_data[$k]["description"] = $category_data["description"] = $int_res["description"];
        }
        if ($category_data["categoryid"]==$cat) {
                $current_category = $category_data;
        }
       
        // funkydunk code
        // funky
        if(strstr($category_data["category"],"/")) {
        $topcat=strtok($category_data["category"],"/");
        // echo $topcat; // finds the first part of the cat name pre /
        $categories_data[$k]["topcat"]=$topcat;
        $topcat = "";
        }
        // end

}

$all_categories = $categories_data;

#
# Put all root categories to $categories array
# Put all subcategories of current_category to $categories array
#

foreach($all_categories as $all_category) {

        $category=$all_category["category"];

        $cur_dir_len = strlen($current_category["category"]);

        if(!strstr($category,"/")) {
            $categories[]=$all_category;
                        if(empty($current_category)) {
                                $subcategories[]=$all_category;
                        }
        }

        if(substr($category,0,$cur_dir_len+1) == $current_category["category"]."/" and $category!=$current_category["category"])
                if(!strstr(substr($category,$cur_dir_len+1),"/")) {
                        $all_category["category"]=ereg_replace("^.*/","",$all_category["category"]);
                        $subcategories[]=$all_category;
                }

}


#
# Put  subcategory_count  to $subcategories array
#
if ($subcategories)
foreach($subcategories as $key =>$subcategory) {
       
        $subcategory["subcategory_count"]=0;

        foreach($all_categories as $all_category) {
                if ($all_category["categoryid"]==$subcategory["categoryid"]) {               
                        $cur_dir_len = strlen($all_category["category"]);
                        $current_subcategory=$all_category["category"];
                }               
        }
       
        foreach($all_categories as $all_category) {
                $category=$all_category["category"];
                if(substr($category,0,$cur_dir_len+1) == $current_subcategory."/" and $category!=$current_subcategory)
                        if(!strstr(substr($category,$cur_dir_len+1),"/"))
                                $subcategory["subcategory_count"]++;
       
        }
        $subcategories[$key]["subcategory_count"]=$subcategory["subcategory_count"];
       
}
               

#
# Generate category sequence, i.e.
# Books, Books/Poetry, Books/Poetry/Philosophy ...
#

$current_category_array = explode("/",$current_category["category"]);
$prev = $current_category_array[0];
list($key,$val)=each($current_category_array);

$new_array = array($val);

while(list($key,$val)=each($current_category_array)) {
        $new_array[] = $prev."/".$val;
        $prev = $prev."/".$val;
}
unset($current_category_array);

#
# Generate array for displaying categories sequence in location
#

$category_location=array();
reset($all_categories);

$my_cats = array ();

foreach($all_categories as $all_category) {

        $categoryid=$all_category["categoryid"];
        $category=$all_category["category"];

        $my_cats [$categoryid] = $category;
}

asort ($my_cats);

foreach ($my_cats as $categoryid => $category) {
        reset ($new_array);
        while(list($key,$val)=each($new_array)) {
                if ($val==$category) {
                        foreach($categories_data as $category_data) {
                                if ($category_data["categoryid"] == $categoryid)
                                        $val = $category_data["category_name"];
                        }
                        $category_location[]=array(ereg_replace(".*/","",$val),"home.php?cat=".$categoryid);
                }
        }
}

#
# Assign Smarty variables
#

$smarty->assign("allcategories",$all_categories);
$smarty->assign("categories",$categories);
$smarty->assign("subcategories",$subcategories);
$smarty->assign("current_category",$current_category);
$smarty->assign("cat",$cat);
?>


Then in the categories.tpl you could have something like:

Code:

{section name=cat_num loop=$categories}
{ $categories[cat_num].category|escape }
 
{section name=cat_num1 loop=$allcategories}
{if $allcategories[cat_num1].topcat eq $categories[cat_num].category_name}
-{ $allcategories[cat_num1].category_name|escape }

{/if}
{/section}
{/section}


This shows each main section then that section subsections under it...

Cat 1
- sub cat 1
- sub cat 2
Cat 2
- sub cat 3
- sub cat 4

etc

hth :)

machnhed1 07-24-2003 08:04 AM

Heck yeah, it worked really well. I simply updated my SQL statement from:

Code:

$categories_data2[$key][] = func_query("select * from $sql_tbl[categories] where category LIKE '".$value[category]."/%' and category NOT LIKE '".$value[category]."/%/%' and avail='Y' group by $sql_tbl[categories].categoryid order by order_by");

To:
Code:

$categories_data2[$key][] = func_query("select $sql_tbl[categories].*, SUBSTRING_INDEX($sql_tbl[categories].category, '\/', -1) as category_name from $sql_tbl[categories] where category LIKE '".$value[category]."/%' and category NOT LIKE '".$value[category]."/%/%' and avail='Y' group by $sql_tbl[categories].categoryid order by order_by");

Thanks a ton, I would have never of guessed to updated my SQL statement, I was stuck on a PHP solution. :oops:

funkydunk 07-24-2003 08:11 AM

I do that...get caught up solving a really big issue and some smart r's comes along :lol:

BTW - if someone wants to use the code above to produce a javascript drop down menu (that's what I used it for - obviously stripped the JS out for this post) then feel free.

kpayne 07-24-2003 08:14 AM

I didn't catch that the first 3 times, can you please repeat it?

:lol:

groovico 07-24-2003 08:14 AM

Yeah alright already, we heard you the first time ;)

Someone prod funky he's gone into a loop.

funkydunk 07-24-2003 08:30 AM

slight connection problem - just kept pressing submit :lol:

Code:

<?php
$marlboro_light_supply = 20;
while ($workrequired === "shedloads"){
  $caffeine_level ++;
  $sleepmode = "false";
  $mummified_state = "true";
  $chocolate ++;
  $caffeine_level ++;
  $marlboro_light_supply = $marlboro_light_supply - 1;
    {if ($marlboro_light_supply < 2){
    $panicMode = "true";
    $go_to_Shops = "true";
    $run_home_for_money = "true";
    $marlboro_light_supply = 20;
    }
  }
$sleepmode = "true";
?>


welcome to my loop :)


All times are GMT -8. The time now is 08:53 AM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.