2010-07-24 13 views
0

你好我想要得到一個多列顯示(如果它可以這樣調用),下面的代碼是結果:alt text http://img842.imageshack.us/img842/7732/res.png。有人可以看到爲什麼這些葉子被打破了?或者告訴我,哪一個更好:一個連接到次級餐廳的分類表,或者一個鄰接模型列表。有關在colunm中顯示數據的幫助PHP MySQL(修改的預定義樹遍歷)

$stmt = $conn->prepare("SELECT node.idCat_ad ,node.".$cat_name.",(COUNT(parent.".$cat_name.") - 1) AS depth 
         FROM cat_ad AS node 
         CROSS JOIN cat_ad AS parent 
         WHERE node.left_node BETWEEN parent.left_node AND parent.right_node 
         GROUP BY node.idCat_ad 
         ORDER BY node.left_node"); 

$stmt->execute(); 
$treeArray = $stmt->fetchAll(); 
?> 

<div class="column_in_categories"> 
<div class="menucategories"><ul> 

<?php 
$x = 0; //counter 
$num_cols = 3; //3 colums 
$num_rows = count($resTree); //num columns 

$result = ''; 

$newArray =array_slice($resTree,1); //removing the root tree 

foreach ($newArray as $currNode) { 

    if ($currNode['depth'] ==1) { //no links in root 

    $result .= '<li class="header '.$classSprites.'">' . $currNode[$cat_name] . '</li>'; 

    } else{ //if child, we put a link 

    $result .= '<li class="subcat"><a href="#">' . $currNode[$cat_name] . '</a></li>'; 
    } 

$x++; // incrementing da counter 

if ($x == ceil($num_rows/$num_cols)) { //if a colunm, we clause it and begin another 

    $result .= '</ul></div> <div class="menucategories"><ul><li style="display:none;">&nbsp;</li>'; 
    $x=0; // 
    } 

} 
$result .="</ul></div>"; 
print $result; 

序列化版本link text

+0

任何機會,你可以提供'$ resTree'的序列化版本,所以我可以嘗試和沙坑爲此解決方案? – 2010-07-24 06:19:24

+0

@Lucanos,我上傳了序列化版本。找到它下面的代碼 – jartaud 2010-07-24 16:54:54

回答

0

更換

foreach ($newArray as $currNode) { 

    if ($currNode['depth'] ==1) { //no links in root 

    $result .= '<li class="header '.$classSprites.'">' . $currNode[$cat_name] . '</li>'; 

    } else{ //if child, we put a link 

    $result .= '<li class="subcat"><a href="#">' . $currNode[$cat_name] . '</a></li>'; 
    } 

$x++; // incrementing da counter 

if ($x == ceil($num_rows/$num_cols)) { //if a colunm, we clause it and begin another 

    $result .= '</ul></div> <div class="menucategories"><ul><li style="display:none;">&nbsp;</li>'; 
    $x=0; // 
    } 

} 

通過

foreach ($newArray as $currNode) { 

$x++; // incrementing da counter 

if ($x >= ceil($num_rows/$num_cols) and $currNode['depth'] ==1) { //if a colunm, we clause it and begin another 

    $result .= '</ul></div> <div class="menucategories"><ul><li style="display:none;">&nbsp;</li>'; 
    $x=0; // 
    } 

    if ($currNode['depth'] ==1) { //no links in root 

    $result .= '<li class="header '.$classSprites.'">' . $currNode[$cat_name] . '</li>'; 

    } else{ //if child, we put a link 

    $result .= '<li class="subcat"><a href="#">' . $currNode[$cat_name] . '</a></li>'; 
    } 

} 

只有當新的標題開始這將啓動新列。

+0

感謝您的時間。實際上,新的標題欄只能在新標題後纔開始。現在出現了一個新問題:如果$ num_cols設置爲3,我得到2列,如果它達到[1∞] - {3} :),我會得到一列。 – jartaud 2010-07-24 17:06:54