2013-01-12 83 views
0

此代碼可以正常處理下列數據。但是結果是一個無序列表,我需要它作爲下拉組合框。有沒有人不知道如何用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> 
+0

組合框是一個UI控件,它是下拉菜單(這是您在HTML中選擇時獲得的)和文本輸入(文本類型的輸入)的組合(因此是名稱)。 HTML沒有任何以組合框表示的本機控件。要獲得一個,你必須用一堆JavaScript構建。或者,您可能意味着「選擇元素」。你在問什麼? – Quentin

+0

Sorr錯誤的名字。調用它然後下拉框 –

回答

0

這應該爲你工作,但你需要在cat_tree_array修改字段名。

function cat_tree_array($items) { 
    $childs = array(); 
    foreach ($items as &$item) { 
     $item = (array) $item; 
     $childs[$item['id_parent']][] = &$item; 
    } 
    unset($item); 

    foreach ($items as &$item) { 
     $item = (array) $item; 
     if (isset($childs[$item['id']])) { 
      $item['_subs'] = $childs[$item['id']]; 
     } 
    } 
    return $childs[0]; 
} 

用法:

<? 
// items: all data that you fetch from db (so just an array) 
// e.g 
// $qry = mysql_query("select * from categorys"); 
// while ($fch = mysql_fetch_array($qry)) $items[] = $fch; 
$tree = cat_tree_array($items); 
?> 

HTML列表:

<ul> 
<? foreach ($tree as $t): ?> 
    <li><?=$t['category_name']?></li> 
    <? if (!empty($t['_subs'])): ?> 
     <ul> 
      <? foreach ($t['_subs'] as $s): ?> 
       <li><?=$s['category_name']?></li> 
      <? endforeach; ?> 
     </ul> 
    <? endif; ?> 
<? endforeach; ?> 
</ul> 

HTML選擇:

<select> 
<? foreach ($tree as $t): ?> 
    <option><?=$t['category_name']?></option> 
    <? if (!empty($t['_subs'])): ?> 
     <optgroup> 
      <? foreach ($t['_subs'] as $s): ?> 
       <option><?=$s['category_name']?></option> 
      <? endforeach; ?> 
     </optgroup> 
    <? endif; ?> 
<? endforeach; ?> 
</select> 
+0

此代碼使用一個無序列表,如我已經準備好的代碼。我需要的是在子類別上加入縮進的下拉框。 –

+0

好的!查看答案的最後部分。 –

+0

我正在嘗試它,但需要您的幫助與數組鍵匹配他們到我的數組。可以idntify'category_name:'catname','id_parent':'catparent',但'_subs',貓匹配我的任何鍵。你能幫我解決這個問題嗎? –

0

@qeremy

這是你的代碼適應(tryed到)我的SQL:

<?php 
    include '../lib/configuration.php'; 

    function cat_tree_array($items) { 
     $childs = array(); 
     foreach ($items as &$item) { 
      $item = (array) $item; 
      $childs[$item['catparent']][] = &$item; 
     } 
     unset($item); 

     foreach ($items as &$item) { 
      $item = (array) $item; 
      if (isset($childs[$item['id']])) { 
       $item['catid'] = $childs[$item['id']]; 
      } 
     } 
     return $childs[0]; 
    } 

    $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()) 
    { 
    $tree = cat_tree_array($items); 
    } 
    ?> 

    <select> 
    <?php foreach ($tree as $t): ?> 
     <option><?php = $t['catname']?></option> 
     <?php if (!empty($t['catid'])): ?> 
      <optgroup> 
       <?php foreach ($t['catid'] as $s): ?> 
        <option><?php = $s['catname']?></option> 
       <?php endforeach; ?> 
      </optgroup> 
     <?php endif; ?> 
    <?php endforeach; ?> 
    </select> 

我得到一個錯誤:在d語法錯誤,意想不到的 '=':

解析錯誤\ Web數據\ MYDL \ ADMIN \上線43

這test1.php是第43行:

<option><?php = $t['catname']?></option> 

Iºm不是一個PHP專家,但真的不明白,代碼

+0

您不需要更改'_subs'鍵,因爲它與您的數據堆棧無關。不要在第二個「foreach」中觸摸'_subs'鍵。只需在打印出來的時候檢查一下if(!empty($ t ['_ subs']))'。此外,該行是錯誤的,將其更改爲:'<?php echo $ t ['catname']; ?>'或'<?= $ t ['catname']?>'。 –

+0

不起作用。給出一個錯誤:解析錯誤:語法錯誤,在第52行的D:\ Web Data \ mydl \ admin \ test1.php中出現意外的$ end,這是最後一行 –