从类别中输出一个漂亮的列表


output a nice list from a category

我的数据库中有一个名为"cat"的表。这是"game1"、"game2"、"game3"等条目。我想知道如何在每个类别上都有一个链接的列表中输出它http://mypage.com/game1","http://mypage.com/game2"等等

我尝试了一些mysql输出,但我只是没有让它工作:/

<?php
//Array with data from table.
$array = array('game1', 'game2', 'game3', 'game4', 'game5');
echo '<pre>';
print_r($array);
echo '</pre>';
//This shows
//Array
//(
//    [0] => game1
//    [0] => game2
//    [0] => game3
//    [0] => game4
//    [0] => game5
//)
//Now we gonna use the implode() function.
echo '<ul>';
echo '<li>' . implode("</li><li>", $array) . '</li>';
echo '</ul>';
//This shows
//   game1
//   game2
//   game3
//   game4
//   game5
?>

print_r()函数,主要用于检查数组的填充情况。

implode()函数是在数组的每个元素之间推送一些东西。

<?php
# Replace $base_url = "http://mypage.com/" with your Website URL you need to set
$base_url = "http://mypage.com/";
# --- Remove this piece of code, it's for demonstration purpose only
# List of all categories from database "cat" table
foreach(range('Z', 'A') as $char){
    //$categories[$char] = array("$char-Category-1","$char-Category-2");
    $categories[] = "$char-Category-1";
    $categories[] = "$char-Category-2";
}
# --- Remove above code
# $categories will be your database cat table Categories
$category_by_alphabets = array();
foreach ($categories as $cat_name){
    $character = strtoupper(substr($cat_name, 0, 1));
    $category_by_alphabets[$character][] = $cat_name;
}
ksort($category_by_alphabets);
?>
<?php 
    if(!empty($category_by_alphabets) && is_array($category_by_alphabets)):
?>
    <ul>
        <?php foreach($category_by_alphabets as $alphabets => $categories): ?>
            <li> <?php echo $alphabets;?></li>
            <ul>
                <?php foreach($categories as $category_name): ?>
                <li> 
                    <a href="<?php echo $base_url. $category_name;?>"> 
                    <?php echo $category_name;?></a>
                </li>
                <?php endforeach; ?>
            </ul>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>

希望,它对你有帮助。