此代碼可以正常處理下列數據。但是結果是一個無序列表,我需要它作爲下拉組合框。有沒有人不知道如何用SQL數據和結構在文章中做到這一點?分層數據PHP combobox
SQL
CREATE TABLE IF NOT EXISTS `categories` (
`catid` int(3) NOT NULL AUTO_INCREMENT,
`catname` varchar(45) NOT NULL,
`catparent` int(3) NOT NULL,
`catorder` int(4) NOT NULL,
`catdesc` varchar(1024) NOT NULL,
PRIMARY KEY (`catid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table `categories`
--
INSERT INTO `categories` (`catid`, `catname`, `catparent`, `catorder`, `catdesc`) VALUES
(1, 'Informática', 0, 1000, 'Computadores e acessórios'),
(2, 'Comunicações', 0, 1000, 'Sistemas de Comunicações'),
(3, 'Acessórios', 1, 2000, 'Acessórios para Computadores'),
(4, 'PABX', 2, 2000, 'Centrais telefónicas IP e TDM'),
(5, 'Controle de Acessos', 0, 1000, 'Controle de Acessos e Assiduidade'),
(6, 'Construção Civil', 0, 1000, 'Construçao Civil ');
代碼
<?php
include '../lib/configuration.php';
$mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $database);
/* check connection */
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT * FROM categories";
if ($result = $mysqli->query($query))
{
/* fetch associative array */
while($row = $result->fetch_assoc())
{
$arrayCategories[$row['catid']] = array("pid" => $row['catparent'], "name" => $row['catname']);
}
//createTree($arrayCategories, 0);
function createTree($array, $currentParent, $currLevel = 0, $prevLevel = -1)
{
foreach ($array as $categoryId => $category)
{
if ($currentParent == $category['pid'])
{
if ($currLevel > $prevLevel) echo " <ul> ";
if ($currLevel == $prevLevel) echo " </li> ";
echo '<li id="'.$categoryId.'" onclick=child(this.id);><span>'.$category['name'].'</span>';
if ($currLevel > $prevLevel) { $prevLevel = $currLevel; }
$currLevel++;
createTree ($array, $categoryId, $currLevel, $prevLevel);
$currLevel--;
}
}
if ($currLevel == $prevLevel) echo " </li> </ul> ";
}
?>
<div id="content" class="general-style1">
<?php
if($result->num_rows!=0)
{
?>
<ul>
<li id="0" class="root"><span>Categories</span>
<?php createTree($arrayCategories, 0); ?>
</li>
</ul>
<?php
}
}
?>
</div>
組合框是一個UI控件,它是下拉菜單(這是您在HTML中選擇時獲得的)和文本輸入(文本類型的輸入)的組合(因此是名稱)。 HTML沒有任何以組合框表示的本機控件。要獲得一個,你必須用一堆JavaScript構建。或者,您可能意味着「選擇元素」。你在問什麼? – Quentin
Sorr錯誤的名字。調用它然後下拉框 –