Home Backend Development PHP Tutorial PHP code PHP code that outputs content according to the hierarchical relationship between upper and lower levels

PHP code PHP code that outputs content according to the hierarchical relationship between upper and lower levels

Jul 29, 2016 am 08:43 AM
php code

Copy code The code is as follows:


function getSubComments($parent = 0, $level = 0) {
$db = &JFactory::getDBO();
$sql = "..."; // SQL for querying records
$db->setQuery($sql);
$rows = $db->loadObjectList();
$list = array();
// First get the record set from the data, and then compare the records Add level, parent level = 0, its subordinate level = 1, and so on
foreach ($rows as $row) {
$row->level = $level;
$list[] = $row;
$ tmpArr = getSubComments($row->id, $level + 1); // Recursive call
if (count($tmpArr)) {
foreach ($tmpArr as $tmpRow) {
$list[] = $tmpRow;
}
}
}
return $list;
}
$list = array();
foreach ($tmpList as $row) {
$row->level = 0;
$list[] = $row;
$tmpList2 = getSubComments($row->id, 1);
foreach ($tmpList2 as $row2) {
$list[] = $row2;
}
}
// Output content hierarchically by level
if ($row->level) {
$pre = '';
for ($n = 0; $n < $row->level; $n++)
$pre .= '----';
echo $pre . '|- ';
}
echo strip_tags($row->content);

The above introduces the PHP code that outputs content according to the hierarchical relationship between upper and lower levels, including the content of PHP code. I hope it will be helpful to friends who are interested in PHP tutorials.

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 Article Tags

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 write PHP code in the browser and keep the code from being executed? How to write PHP code in the browser and keep the code from being executed? Mar 10, 2024 pm 02:27 PM

How to write PHP code in the browser and keep the code from being executed?

How to use regular expressions to batch modify PHP code to meet the latest code specifications? How to use regular expressions to batch modify PHP code to meet the latest code specifications? Sep 05, 2023 pm 03:57 PM

How to use regular expressions to batch modify PHP code to meet the latest code specifications?

PHP code implements request parameter encryption and decryption processing of Baidu Wenxinyiyan API interface PHP code implements request parameter encryption and decryption processing of Baidu Wenxinyiyan API interface Aug 16, 2023 pm 11:40 PM

PHP code implements request parameter encryption and decryption processing of Baidu Wenxinyiyan API interface

How to use PHP code testing function to improve code maintainability How to use PHP code testing function to improve code maintainability Aug 11, 2023 pm 12:43 PM

How to use PHP code testing function to improve code maintainability

Analyze PHP code testing function and its importance Analyze PHP code testing function and its importance Aug 11, 2023 pm 03:12 PM

Analyze PHP code testing function and its importance

Debugging errors and unexpected behavior in PHP code Debugging errors and unexpected behavior in PHP code Aug 12, 2023 pm 04:49 PM

Debugging errors and unexpected behavior in PHP code

How to automatically check whether PHP code complies with the latest code specifications? How to automatically check whether PHP code complies with the latest code specifications? Sep 06, 2023 pm 12:33 PM

How to automatically check whether PHP code complies with the latest code specifications?

PHP code static analysis and vulnerability detection technology PHP code static analysis and vulnerability detection technology Aug 07, 2023 pm 05:21 PM

PHP code static analysis and vulnerability detection technology

See all articles