2011-04-26 149 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(); 
相关问题