Hellow,我有一个简单的表格,如:
CAT ID PARENT_ID
------+----------
1 0
2 0
3 1
4 1
5 3
6 2
我做了一个类别树,但是我需要这样的东西:
我的代码是:
Cat 1
Subcat1
SubCat2
SubSubcat1
Cat 2
Subcat1
我需要打印每个类别的完整父母路径。
继承我的代码:
<?php
require_once 'config.php';
?>
<?php header('Content-Type: text/html; charset=UTF-8'); ?>
<?php
$username = "xxx";
$password = "xxx";
$hostname = "xxxx";
$databasename = "xxx";
$parent_id = 0;
$salida = " ";
$mysqli = new mysqli($hostname, $username, $password, $databasename);
$ruta = "";
//connect to mysql and select db
$conn = mysqli_connect($hostname, $username, $password, $databasename);
if (!empty($conn->connect_errno)) {
die("Error " . mysqli_error($conn));
}
echo "<select>";
echo "<option value='' disabled selected>--Seleccione CategorÃa--</option>";
//call the recursive function to print category listing
category_tree(0);
echo "</select>";
//
//echo gettheparent(168);
//Recursive php function
function category_tree($catid) {
global $conn;
$sql = "select * from oc_category where parent_id ='" . $catid . "'";
$result = $conn->query($sql);
while ($row = mysqli_fetch_object($result)):
$i = 0;
//$ruta=gettheparent(168);
if ($i == 0) {
echo "<option value='" . $row->category_id;
echo "'>" . getthename($row->category_id);
category_tree($row->category_id);
echo "</option>";
$i++;
}
if ($i > 0) {
//echo '</ul>';
//$ruta=gettheparent($catid);
}
endwhile;
}
function getthename($category_id) {
if ($category_id != 0) {
$username = "xxxx";
$password = "xxxxx";
$hostname = "xxxx";
$databasename = "xxxxx";
$mysqli = new mysqli($hostname, $username, $password, $databasename);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT name FROM oc_category_description WHERE category_id = '" . $category_id . "'";
$result = $mysqli->query($query);
while ($row = $result->fetch_array()) {
$rows[] = $row;
}
foreach ($rows as $row) {
$salida .= " - ";
$salida .= utf8_encode($row["name"]);
return $salida;
}
$result->close();
$mysqli->close();
}
}
任何帮助? 提前致谢