我有一个小的10行最大文本文件,可以容纳用户最喜欢的十首歌曲。我有一个表单,允许用户输入数字1-10并显示文件中的顶部“X”歌曲。如何打印出用户给出的X编号的文件的第一行“X”行?
这是我目前的剧本:
//if topSongs field is NOT empty
if(!empty($_POST['topSongs'])) {
$showSongs = $_POST['topSongs'];
if($showSongs <= 10) {
$allTunes = file_get_contents($tunesFile);
$tunesArray = explode("\n", $allTunes);
foreach($tunesArray as $tune) {
print($tune);
}
//if user input IS greater than 10
} else {
print(" <strong>A maximum of 10 songs are allowed</strong>");
}
//if topSongs field IS empty
} else {
print(" <strong>Please enter the number of songs to show</strong>");
}
$showSongs
变量包含要显示的给定数字
答案 0 :(得分:4)
为这样的事情改变foreach:
for($i = 0; $i < $showSongs; $i++) {
print($tunesArray[$i]);
}