Home php教程 php手册 无限分类 ---(商品搜索)--- 痛并快乐着

无限分类 ---(商品搜索)--- 痛并快乐着

Jun 13, 2016 am 10:52 AM
about share Classification merchandise quick search unlimited First stage project

第一个项目阶段接近尾声了,分享一个关于无限分类自定义的回调函数(说实话,当时写出来也是懵的,现在再看更懵,O(∩_∩)O~)
//项目需求:想在首页做个商品搜索
//做法:写了个下拉列表,列表项就是下表中的9个categoryname.。。我想任意选择一个类别都可以一次性搜索出商品表中的该类别的商品(包括下边的子类)。
//思想过程:最开始也想,,,,where path like .%0-1%.......之类的来着,但是后来发现分页的时候遇到很大问题,因为当时我上传商品没有在有子类的id中上传商品(当循环去查询商品的时候会有空循环--当该类别还有子类的时候)。

越说越乱了,还是拿表出来直接示意吧、、、
category表数据结构示意:
+----+-----+-------+--------------+
| id | pid | path | categoryname |
+----+-----+-------+--------------+
| 1 | 0 | 0 | 所有商品 1
| 2 | 1 | 0-1 | 国产商品 2
| 4 | 2 | 0-1-2 | 国产水货 4
| 5 | 2 | 0-1-2 | 国产行货 5
| 3 | 1 | 0-1 | 进口商品 3
| 6 | 3 | 0-1-3 | 进口水货 6
| 7 | 3 | 0-1-3 | 进口行货 7
| 8 | 1 | 0-1 | 测试类 8
| 9 | 1 | 0-1 | 杂货专区 9
+----+-----+-------+--------------+
看着表就好多了,我的想法是如果我在下拉列表中选择的是“所有商品”,那么只要是属于所有商品的所有子类都应该被我拿到(并且我还不希望拿到如上表中的“国有商品”、"进口商品",因为他们下面还有子类)。写到这里,我似乎觉得可以不用我这么麻烦去写回调了。~~~~(>_ mysql_connect('localhost','root','admin');
mysql_select_db('dzshop');
mysql_query("set names utf8");

//测试
echo rtrim( list_typeid(1), ',' ); //输出 4,5,6,7,8,9 ^o^ 此时可以 select * from goods where typeid in(4,5,6,7,8,9) 拿出所有商品不是问题了。
echo rtrim( list_typeid(2), ',' ); //输出 4,5
echo rtrim( list_typeid(4), ',' ); //输出 4

/*
* 函数:list_typeid
* 功能:获取给定的类别,获取该指定typeid(即category表的id)下的所有没有子类的category.id(category.id==goods.typeid)
* $id:需要查询的id
* $category_name:分类表的表名
*/
function list_typeid($id,$category_name='category'){
$sql="select b.id,b.pid from {$category_name} a,{$category_name} b where a.id={$id} and b.pid={$id}";
$result=mysql_query($sql);
if(mysql_affected_rows()>0){ //影响行数大于0表示该分类下有子类,进去继续执行
while( $arr = mysql_fetch_assoc($result) ){
$list_id.=list_typeid($arr['id']);
}
}else{ //没有影响行,表示该子类下没有子类了,就把这个id拿出来
return $id.','; //方便SQL语句的书写 .... WHERE ID IN($id) ....
}
return $list_id;
}


作者:zdrjlamp
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to share Quark Netdisk to Baidu Netdisk? How to share Quark Netdisk to Baidu Netdisk? Mar 14, 2024 pm 04:40 PM

Quark Netdisk and Baidu Netdisk are very convenient storage tools. Many users are asking whether these two softwares are interoperable? How to share Quark Netdisk to Baidu Netdisk? Let this site introduce to users in detail how to save Quark network disk files to Baidu network disk. How to save files from Quark Network Disk to Baidu Network Disk Method 1. If you want to know how to transfer files from Quark Network Disk to Baidu Network Disk, first download the files that need to be saved on Quark Network Disk, and then open the Baidu Network Disk client. , select the folder where the compressed file is to be saved, and double-click to open the folder. 2. After opening the folder, click "Upload" in the upper left corner of the window. 3. Find the compressed file that needs to be uploaded on your computer and click to select it.

Can AI conquer Fermat's last theorem? Mathematician gave up 5 years of his career to turn 100 pages of proof into code Can AI conquer Fermat's last theorem? Mathematician gave up 5 years of his career to turn 100 pages of proof into code Apr 09, 2024 pm 03:20 PM

Fermat's last theorem, about to be conquered by AI? And the most meaningful part of the whole thing is that Fermat’s Last Theorem, which AI is about to solve, is precisely to prove that AI is useless. Once upon a time, mathematics belonged to the realm of pure human intelligence; now, this territory is being deciphered and trampled by advanced algorithms. Image Fermat's Last Theorem is a "notorious" puzzle that has puzzled mathematicians for centuries. It was proven in 1993, and now mathematicians have a big plan: to recreate the proof using computers. They hope that any logical errors in this version of the proof can be checked by a computer. Project address: https://github.com/riccardobrasca/flt

A closer look at PyCharm: a quick way to delete projects A closer look at PyCharm: a quick way to delete projects Feb 26, 2024 pm 04:21 PM

Title: Learn more about PyCharm: An efficient way to delete projects. In recent years, Python, as a powerful and flexible programming language, has been favored by more and more developers. In the development of Python projects, it is crucial to choose an efficient integrated development environment. As a powerful integrated development environment, PyCharm provides Python developers with many convenient functions and tools, including deleting project directories quickly and efficiently. The following will focus on how to use delete in PyCharm

How to share NetEase Cloud Music to WeChat Moments_Tutorial on sharing NetEase Cloud Music to WeChat Moments How to share NetEase Cloud Music to WeChat Moments_Tutorial on sharing NetEase Cloud Music to WeChat Moments Mar 25, 2024 am 11:41 AM

1. First, we enter NetEase Cloud Music, and then click on the software homepage interface to enter the song playback interface. 2. Then in the song playback interface, find the sharing function button in the upper right corner, as shown in the red box in the figure below, click to select the sharing channel; in the sharing channel, click the "Share to" option at the bottom, and then select the first "WeChat Moments" allows you to share content to WeChat Moments.

WPS table cannot find the data you are searching for, please check the search option location WPS table cannot find the data you are searching for, please check the search option location Mar 19, 2024 pm 10:13 PM

In the era dominated by intelligence, office software has also become popular, and Wps forms are adopted by the majority of office workers due to their flexibility. At work, we are required not only to learn simple form making and text entry, but also to master more operational skills in order to complete the tasks in actual work. Reports with data and using forms are more convenient, clear and accurate. The lesson we bring to you today is: The WPS table cannot find the data you are searching for. Why please check the search option location? 1. First select the Excel table and double-click to open it. Then in this interface, select all cells. 2. Then in this interface, click the "Edit" option in "File" in the top toolbar. 3. Secondly, in this interface, click "

How to share files with friends on Baidu Netdisk How to share files with friends on Baidu Netdisk Mar 25, 2024 pm 06:52 PM

Recently, Baidu Netdisk Android client has ushered in a new version 8.0.0. This version not only brings many changes, but also adds many practical functions. Among them, the most eye-catching is the enhancement of the folder sharing function. Now, users can easily invite friends to join and share important files in work and life, achieving more convenient collaboration and sharing. So how do you share the files you need to share with your friends? Below, the editor of this site will give you a detailed introduction. I hope it can help you! 1) Open Baidu Cloud APP, first click to select the relevant folder on the homepage, and then click the [...] icon in the upper right corner of the interface; (as shown below) 2) Then click [+] in the "Shared Members" column 】, and finally check all

How to search for stores on mobile Taobao How to search for store names How to search for stores on mobile Taobao How to search for store names Mar 13, 2024 am 11:00 AM

The mobile Taobao app software provides a lot of good products. You can buy them anytime and anywhere, and everything is genuine. The price tag of each product is clear. There are no complicated operations at all, making you enjoy more convenient shopping. . You can search and purchase freely as you like. The product sections of different categories are all open. Add your personal delivery address and contact number to facilitate the courier company to contact you, and check the latest logistics trends in real time. Then some new users are using it for the first time. If you don’t know how to search for products, of course you only need to enter keywords in the search bar to find all the product results. You can’t stop shopping freely. Now the editor will provide detailed online methods for mobile Taobao users to search for store names. 1. First open the Taobao app on your mobile phone,

Solve the problem that Discuz WeChat sharing cannot be displayed Solve the problem that Discuz WeChat sharing cannot be displayed Mar 09, 2024 pm 03:39 PM

Title: To solve the problem that Discuz WeChat shares cannot be displayed, specific code examples are needed. With the development of the mobile Internet, WeChat has become an indispensable part of people's daily lives. In website development, in order to improve user experience and expand website exposure, many websites will integrate WeChat sharing functions, allowing users to easily share website content to Moments or WeChat groups. However, sometimes when using open source forum systems such as Discuz, you will encounter the problem that WeChat shares cannot be displayed, which brings certain difficulties to the user experience.

See all articles