Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 紧急啊.我被递归给郁闷死了.请大家帮我一下

紧急啊.我被递归给郁闷死了.请大家帮我一下

Jun 20, 2016 pm 12:59 PM


首先.我的[栏目]表结构是这样的
id   uid   titile  class
1    0       首页  url
2   0       新闻  news
3   0       产品  url
4   3      科技产品  product
5   4      电脑   product

现在的问题的  我想做一下 无限级的下拉框

用递归的方法,可以实现  where  uid = 0  开始循环  然后 get_str($row['id']); 递归


但是问题来了.  因为我的栏目 有一个CLASS字段  表示栏目类型...url表示外部链接  news表示新闻 product表示产品栏目

一级产品首先是一个外部链接类型的..他的下级栏目 是产品类型的
我在后台发布产品的时候.选择归属栏目的时候..............应该只把 class=prodcut和他的下级全部显示出来
那么我用递归的方法  在SQL加上条件语句  where class='product'

这样的话 没办法往下递归啊......  因为这个条件 如果不递归的话 就显示  科技产品    电脑

我在网上找的归递代码 都是UID 从 0开始的..........如果我  where class='product' 加上这个条件   那么科技产品的UID是3或者是其它数字 比如56789 也可能是0

这样就没办法递归啊...直接什么也不显示了...有什么好法方吗? 不知道我说的清不清楚.


回复讨论(解决方案)

列出 class=prodcut和他的下级 时,就不是从 where  uid = 0 开始了
对你示例的数据就是从 where  uid = 3 开始

所以你应该先获得起点的 id,然后再递归

列出 class=prodcut和他的下级 时,就不是从 where  uid = 0 开始了
对你示例的数据就是从 where  uid = 3 开始

所以你应该先获得起点的 id,然后再递归



求大神.帮帮我吧,,,帮我写一个我这个要求的  无限分类 的代码好吗

论坛是交流的地方,要代码请左转威客网

1

function foo($uid=0, $ext='') {  $sql = "select * from tbl_name where uid=$uid" . ($uid && $ext ? " and class='$ext'" : '');  $rs = mysql_query($sql);  while($row = mysql_fetch_assoc($rs)) {    foo($row['id'], $ext);  } }

Copy after login
Copy after login

1

function foo($uid=0, $ext='') {  $sql = "select * from tbl_name where uid=$uid" . ($uid && $ext ? " and class='$ext'" : '');  $rs = mysql_query($sql);  while($row = mysql_fetch_assoc($rs)) {    foo($row['id'], $ext);  } }

Copy after login
Copy after login




function foo($uid=0, $ext='') {
$sql = "select * from column where uid=$uid" . ($uid && $ext ? " and class='$ext'" : '');
$rs = mysql_query($sql);
while($row = mysql_fetch_assoc($rs)) { 这行报错啊
echo $row['id'];
foo($row['id'], $ext);
}
}

echo foo(0, "product") ;

哥哥

报的什么错?

报错我解决了

但是这个还是必须是 UID 从0开始啊..

如果UID是空 那么class='$ext' 就报错啊

foo(3, "product")
不就从 产品 下面开始了吗?

要不要 product 无所谓

现在的问题是 这个 3 不是固定的啊

比如我的栏目有 首页 新闻 产品 视频

那么我在后台 发布产品的时候., 可选栏目中 只把产品栏目列出来啊/

那么 栏目中 可能有 多个 产品栏目.... 用于区分栏目类型 就靠 class字段中的值 比如product 表示全部是产品栏目

不用递归呢 可否实现

我一开始就讲了,既然你试要从某一个节点开始,就要先找到这个节点
无论这个节点存放在什么位置,都可以按其唯一的属性找到他

是啊.大哥

可是我不会从节点开始循环啊......怎么办,,,百度很多资料了.全是递归的

1

function foo($uid,$class=''){    if($class==''){        $sql = "select * from table where uid='".$uid."'";    }else{        $sql = "select * from table where uid='".$uid."' and class='".$class."'";    }    $query = mysql_query($sql) or die(mysql_error());    while($result = mysql_fetch_assoc($query)){        echo $result['id'].' '.$result['title'].'<br>';        foo($result['id'],$result['class']);    }}foo(0);

Copy after login

你能从根节点上递归,不就能从子节点上递归吗?
有什么区别呢?

用递归还是不用递归,这取决于你的需要。我在精华区有好几个帖子,你可以先看看

你能从根节点上递归,不就能从子节点上递归吗?
有什么区别呢?

用递归还是不用递归,这取决于你的需要。我在精华区有好几个帖子,你可以先看看




帮助我太多了.把分给您了

后来我自已用别的思路解决了.
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)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles