简体   繁体   English

PHP:如何在每个foreach结果上运行foreach循环?

[英]PHP: How do I run foreach loop on every foreach result?

Im pulling content from my database and running through it with a foreach loop. 我从我的数据库中提取内容并使用foreach循环运行它。 Every item I pull has a column with an ID, and a column with PARRENT ID (among other columns). 我拉的每个项目都有一个带有ID的列,以及一个带有PARRENT ID的列(以及其他列)。 First, im pulling all the rows where my PARRENT ID column equals 0, and output them via my foreach loop, but for every result I get, I want to run a new query to check for rows where PARRENT ID is equal to its ID. 首先,我拉出我的PARRENT ID列等于0的所有行,并通过我的foreach循环输出它们,但是对于我得到的每个结果,我想运行一个新查询来检查PARRENT ID等于其ID的行。 For every result I get here, I want to do the same again and again, for as many results it may find (Potentially this could go on forever). 对于我来到这里的每一个结果,我想一次又一次地做同样的事情,因为它可能会找到尽可能多的结果(这可能会永远持续下去)。

Let me try to visualize this 让我试着想象一下这个

 ID = 1, PARRENT ID = 0 ID = 2, PARRENT ID = 0 ID = 3, PARRENT ID = 0 ID = 4, PARRENT ID = 3 ID = 5, PARRENT ID = 3 ID = 6, PARRENT ID = 5 ID = 7, PARRENT ID = 3 ID = 8, PARRENT ID = 3 ID = 9, PARRENT ID = 0 ID = 10, PARRENT ID = 0 ID = 11, PARRENT ID = 10 ID = 12, PARRENT ID = 11 ID = 13, PARRENT ID = 12 ID = 14, PARRENT ID 13 

How do i go about this? 我该怎么做?

You could make a function which just returns all rows with a specific parent id. 您可以创建一个只返回具有特定父ID的所有行的函数。 And then make a recursive function which prints out a result set and all children of every element. 然后创建一个递归函数,打印出结果集和每个元素的所有子元素。 This is some quick pseudo code: 这是一些快速伪代码:

function getRowsForParentId($id){
    //build query with "WHERE parent = id", fetch all rows and return them
    ...
    return $rows;
}


function printResults($results, $level = 1){
    //loop over every result
    foreach($results as $row){
        //print an indented representation of that row
        echo sprintf("%sid: %s, parent: %s", str_repeat(" ", $level), $row->id, $row->parentId);
        //print out all childs one level deeper
        printResults(getRowsForParentId($row->id), $level++);
    }
}

$allRows = getRowsForParentId(0);//fetch all rows from db where parent = 0
printResults($allRows);

This should do what you want. 这应该做你想要的。

Also look up on storing hierarchical data in mysql, as this is what you are doing in here. 还要查看在mysql中存储分层数据,因为这是你在这里所做的。

What are the options for storing hierarchical data in a relational database? 在关系数据库中存储分层数据有哪些选项?

I figured out a solution myself, which works like I want it to, but I honestly don't know if is bad practice, but I'm very new to programming. 我自己想出了一个解决方案,就像我想要的那样,但老实说我不知道​​是不是很糟糕的做法,但我对编程很新。

I made a function, in which I can pass in a value. 我做了一个函数,我可以传入一个值。 Within this function, I select all rows with a PARRENT ID equal to my input value. 在此函数中,我选择PARRENT ID等于我的输入值的所有行。 Then I have a foreach loop, in which I get the ID of earch row. 然后我有一个foreach循环,在其中我获得了earch行的ID。 Within this foreach loop, I pass the function, and pass the ID as the value. 在这个foreach循环中,我传递函数,并传递ID作为值。

function get_parrents($input) {  

$sql = "SELECT * FROM table WHERE parrent_id = '" . $input . "'";
$result = mysqli_query($conn, $sql);
foreach($result as $item) {

    $new_item = $item['id'];

    get_parrents($new_item);
};

}; };

$a = 0; $ a = 0;

catch_replies($a); catch_replies($ A);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM