我的数据库中有一个表calle“cat”。这就是“game1”,“game2”,“game3”等。我想知道如何在一个列表中输出这个,每个类别的链接都会像“http://mypage.com/game1”,“{{3} “等等。
我尝试了一些mysql输出,但我只是没有让它工作:/
答案 0 :(得分:1)
<?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()
函数是在数组的每个元素之间推送一些东西。
答案 1 :(得分:0)
<?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; ?>
希望,它会帮助你。