2011-04-26 147 views
-1

我需要幫助來顯示數據庫中無序列表中的條目列表。我現在面臨的問題是如何循環訪問數據庫中的數據以顯示幾十組。如何在無序列表中顯示類別

所以前十個類別將顯示在第一個。 而後來的類別也顯示在它們各自的s中。

EG。

<ul> 
<li>Category 1 Item 1</li> 
<li>Category 1 Item 1</li> 
... ......................... . ....... 
<li>Category 1 Item 10</li> 
</ul> 

<ul> 
<li>Category 2 Item 1</li> 
<li>Category 2 Item 1</li> 
... ......................... . ....... 
<li>Category 2 Item 10</li> 
</ul> 

任何幫助將不勝感激。由於

+1

你已經有一些PHP代碼?如果有,請顯示卡住的位置。 – 2011-04-26 15:53:22

+0

我的php代碼目前的行爲與@Doug T發佈的代碼類似,代碼如下 – 2011-04-26 16:01:42

+0

您的意思是說前10個「項目」將顯示在第一個列表中? – 2011-04-26 16:14:04

回答

1

我現在的問題是如何循環 通過數據從 數據庫未來的陣列組,以顯示 說幾萬。

您的類別如何組織?我假設你有一個你想要組織的單一「類別」列。並且您已使用ORDER BY在查詢中對其進行了排序。

然後假設你有這些排序行的一組,你可以跟蹤,當該類別改變組織爲數組的數組

$currCat = "NotaNumber"; 
$output = ""; 
while ($row = mysql_fetch_row($queryRes)) 
{ 
    # when the category changes, add closing and opening ul's 
    # we'll trim the excess </ul> and <ul> off later 
    # You can change this to also limit the number printed 
    # by combining with the other answer 
    if ($row['categoryColumn'] != $currCat) 
    { 
     $output .= "</ul>"; 
     $output .= "<ul>"; 
     $currCat = $row['categoryColumn']; 
    } 
    $output .= "<li> Category No: " . $row['categoryColumn'] . 
       " Data:" . $row['catData'] . "</li>"; 
} 
# Trim </ul> from front and <ul> from back by getting a substring 
$output = substr($output, 5, strlen($output) - 9); 
echo $output 

原來的答覆

印刷類:

function printCategories($catItems) 
{ 
    echo "<ul>"; 
    $numPrinted = 0; 
    foreach ($catItems as $catItem) 
    { 
     echo "<li> $catEntry </li>" 
     $numPrinted++; 
     if ($numPrinted > 10) 
     { 
      break; 
     } 
    } 

    echo "</ul>"; 
} 

foreach ($categories as $category) 
{ 
    printCategories($category); 
} 
+0

你甚至讀過我的問題嗎?除了標題 – 2011-04-26 16:00:13

+0

之外還有更多內容@Don然後展示一些實際有意義的代碼。 – 2011-04-26 16:17:25

+0

@唐,不知道我的編輯能幫助你。 – 2011-04-26 16:35:30

1
echo("<ul>"); 
$count = 0; 
while(...fetch next row...) { 
    if($count % 10 == 0) 
     echo("</ul><ul>"); 
    echo("<li>" . $catName . "</li>"); 
    $count++; 
} 

echo("</ul>"); 
0

有很多o f PHP標籤在那裏,但我假設你會在你的視圖中使用它,所以你將有HTML來填補它在任何情況下。

<?php $i = 0; ?> 
<?php foreach($categories as $catgory) : ?> 
    <?php if ($i == 0) : ?> 
     <ul> 
    <?php endif; ?> 

     <li>$catgory</li> 

    <?php if ($i == 9) : $i = 0 ?> 
     </ul> 
    <?php endif; ?> 

    <?php $i++; ?> 
<?php endforeach ?> 
0

使用PHPTAL

$categories = array_chunk($categories, 10); 
$tal = new PHPTAL; 
$tal->categories = $categories; 

$html = <<<HTML 
    <ul tal:repeat="category categories"> 
     <li tal:repeat="item category"> 
     Category ${repeat/category/number} 
     Item ${repeat/item/number} 
     </li> 
    </ul> 
HTML; 
$tal->setSource($html, __FILE__); 
echo $tal->execute(); 
相關問題