Php 从类别中输出一个好的列表

Php 从类别中输出一个好的列表,php,sql,Php,Sql,我的数据库中有一个名为“cat”的表。这是“game1”、“game2”、“game3”等条目。我想知道如何将其输出到一个列表中,每个类别上都有一个链接,如“”,等等 我尝试了一些mysql输出,但我就是没能让它工作://p> <?php //Array with data from table. $array = array('game1', 'game2', 'game3', 'game4', 'game5'); echo '<pre>'; print_r($array);

我的数据库中有一个名为“cat”的表。这是“game1”、“game2”、“game3”等条目。我想知道如何将其输出到一个列表中,每个类别上都有一个链接,如“”,等等

我尝试了一些mysql输出,但我就是没能让它工作://p>

<?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\u r()
函数,主要用于检查数组填充的内容


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

你能告诉我们你尝试了什么吗?分享你的研究成果对每个人都有帮助。告诉我们您尝试了什么,以及为什么它不能满足您的需求。这表明你花了时间来帮助自己,它使我们避免重复显而易见的答案,最重要的是,它帮助你得到一个更具体和相关的答案!还可以看看如果有500多个类别,每天添加10个新的get?有没有办法让它自动输出所有类别?在“cat”表中,每个类别都有一行,如“game1”、“game2”等。我说的是这样的内容>如果有500多个类别,每天添加10个新的get,会怎么样?有没有办法让它自动输出所有类别?在“cat”表中,每个类别都有一行,如“game1”、“game2”等。我说的是类似这样的内容>好,添加新的示例以及您需要的输出,等等。检查,我只是根据您的需要更新了代码。希望它现在能帮助你。哦,上帝!非常感谢你的帮助!!:D
<?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; ?>