0

db_connection.php

<?php
define('_HOST_NAME', 'localhost');
define('_DATABASE_USER_NAME', 'root');
define('_DATABASE_PASSWORD', '');
define('_DATABASE_NAME', 'test_database');

 $dbConnection = new mysqli(_HOST_NAME, _DATABASE_USER_NAME, _DATABASE_PASSWORD, _DATABASE_NAME);
 if ($dbConnection->connect_error) {
      trigger_error('Connection Failed: '  . $dbConnection->connect_error, E_USER_ERROR);
 }
 $_GLOBAL['dbConnection'] = $dbConnection; 
?>

函数.php

<?php 
function categoryParentChildTree($parent = 0, $spacing = '', $category_tree_array = '') {
global $dbConnection;
$parent = $dbConnection->real_escape_string($parent);
if (!is_array($category_tree_array))
    $category_tree_array = array();

$sqlCategory = "SELECT id,name,parent_id FROM tbl_categories WHERE parent_id = $parent ORDER BY id ASC";
$resCategory=$dbConnection->query($sqlCategory);

if ($resCategory->num_rows > 0) {
    while($rowCategories = $resCategory->fetch_assoc()) {
        $category_tree_array[] = array("id" => $rowCategories['id'], "name" => $spacing . $rowCategories['name']);
        $category_tree_array = categoryParentChildTree($rowCategories['id'], '&nbsp;&nbsp;&nbsp;&nbsp;'.$spacing . '-&nbsp;', $category_tree_array);
    }
}
return $category_tree_array;
}
?>

索引.php

<?php
require_once 'db_connection.php';
require_once 'functions.php';

$categoryList = categoryParentChildTree(); 
foreach($categoryList as $key => $value){
    echo $value['name'].'<br>';
}
?>

谁能帮我翻译一下,这样我就可以在codeigniter上使用了吗?递归函数对不起,我是codeigniter的新手,请帮忙

4

0 回答 0