我有一个小的10行最大文本文件,该文件可容纳用户最喜欢的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
变量保存给定的数字以显示
更改类似的内容:
for($i = 0; $i < $showSongs; $i++) {
print($tunesArray[$i]);
}