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


首先.我的[栏目]表结构是这样的
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,然后再递归



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

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

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);  } }

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);  } }




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  表示全部是产品栏目

不用递归呢  可否实现

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

是啊.大哥

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

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);

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

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

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

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




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

后来我自已用别的思路解决了.
Statement
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
What is the full form of PHP?What is the full form of PHP?Apr 28, 2025 pm 04:58 PM

The article discusses PHP, detailing its full form, main uses in web development, comparison with Python and Java, and its ease of learning for beginners.

How does PHP handle form data?How does PHP handle form data?Apr 28, 2025 pm 04:57 PM

PHP handles form data using $\_POST and $\_GET superglobals, with security ensured through validation, sanitization, and secure database interactions.

Is PHP a case-sensitive language?Is PHP a case-sensitive language?Apr 28, 2025 pm 04:55 PM

PHP's case sensitivity varies: functions are insensitive, while variables and classes are sensitive. Best practices include consistent naming and using case-insensitive functions for comparisons.

How do you redirect a page in PHP?How do you redirect a page in PHP?Apr 28, 2025 pm 04:54 PM

The article discusses various methods for page redirection in PHP, focusing on the header() function and addressing common issues like "headers already sent" errors.

What is PDO in PHP?What is PDO in PHP?Apr 28, 2025 pm 04:51 PM

The article discusses PHP Data Objects (PDO), an extension for database access in PHP. It highlights PDO's role in enhancing security through prepared statements and its benefits over MySQLi, including database abstraction and better error handling.

What is Memcache and Memcached in PHP? Is it possible to share a single instance of a Memcache between several projects of PHP?What is Memcache and Memcached in PHP? Is it possible to share a single instance of a Memcache between several projects of PHP?Apr 28, 2025 pm 04:47 PM

Memcache and Memcached are PHP caching systems that speed up web apps by reducing database load. A single instance can be shared among projects with careful key management.

What are the steps to create a new database using MySQL and PHP?What are the steps to create a new database using MySQL and PHP?Apr 28, 2025 pm 04:44 PM

Article discusses steps to create and manage MySQL databases using PHP, focusing on connection, creation, common errors, and security measures.

Does JavaScript interact with PHP?Does JavaScript interact with PHP?Apr 28, 2025 pm 04:43 PM

The article discusses how JavaScript and PHP interact indirectly through HTTP requests due to their different environments. It covers methods for sending data from JavaScript to PHP and highlights security considerations like data validation and prot

See all articles

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

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor