PHP如何处理$ _GET varibles

时间:2015-09-23 20:56:38

标签: php arrays

我正在学习PHP。制作歌本网站的用户可以找到歌词和其他内容。在这里,我有很大的不。歌曲,我想在单独的字母页面列出。

$songbook = array( 

"a" => array( 
"153" => array( "title" => "", "content" => "", "tags" => "",),
"154" => array( "title" => "", "content" => "", "tags" => "",),
),

"b" => array( 
"155" => array( "title" => "", "content" => "", "tags" => "",),
"156" => array( "title" => "", "content" => "", "tags" => "",),
),

"c" => array( 
"157" => array( "title" => "", "content" => "", "tags" => "",),
"158" => array( "title" => "", "content" => "", "tags" => "",),
),);

歌曲表。

<table class="list">
<tbody>
<?php foreach ($songbook as $song => $lyric) { ?>

<tr><td class="title"><a href="publicsong.php?id=<?php echo $song; ?>"><?php echo $lyric["title"]; ?></a></td>
<td class="artist"><?php echo $lyric["artist"]; ?></td>
<td class="category"><?php echo $lyric["tags"]; ?></td>
</tr>
<?php } ?>
</tbody>

publicsong.php?id=153

if(isset($_GET['id'])) {
        $songbook = strip_bad_chars( $_GET['id'] );
        $song = $songbook[$songbook];
    }

如果删除字母数组"a" => array( "b" => array( "c" => array(,一切正常。但是我希望这个publicsearch.php?browse=a页面按字母顺序显示歌曲列表。

我不知道如何处理多维数组。

http://mysongbook.ca/网站可以帮助您更好地理解。请转到链接。在左侧有一个导航菜单(字母A B C D E F G ..)导航到艺术家歌曲列表。该列表指向id内容等页面。我想在我的网站中使用此网站方法。请帮忙。我是新手而且我被困住了。谢谢。

1 个答案:

答案 0 :(得分:2)

最好有两个数组。一个数组包含所有歌曲细节,按歌曲ID索引。第二个数组将按字母顺序对它们进行分组:

$allSongs = array(
    "153" => array( "title" => "", "content" => "", "tags" => "",),
    "154" => array( "title" => "", "content" => "", "tags" => "",),
    "155" => array( "title" => "", "content" => "", "tags" => "",),
    "156" => array( "title" => "", "content" => "", "tags" => "",),
    ...
);
$alphaSongs = array(
    'a' => array(153, 154);
    'b' => array(155, 156);
    ...
);

然后您可以使用$allSongs[$_GET['id']]查找带有ID的歌曲。

您还可以使用其他数组,例如$authorSongs$genreSongs

如果您是真的这样做,您可能会使用数据库而不是数组。