我正在为我的学校制作一个PHP脚本。学校的网站目录有大约100个文本文件,每个学生一个文本文件。每个文本文件都有两行,如:
stu $id = '12701';
stu $name = 'alex';
我想在表中回显这个id和name值。但这两行是随机的。它可能位于第23和24行,或者有时是45和46。
这是我的尝试:
$directory = 'lessons/'; // The directory to the lesson text files
$linesToReturn = 50; // i tired to read top 50 lines , so i can grab from them.
// Find all files in the directory which are .txt files
foreach (glob($directory . "*.txt") as $filename)
{
// Open the file
if ($handle = @fopen($filename, "r"))
{
$x = 0; // Start the line counter
// Cycle each line until end or reach the lines to return limit
while (!feof($handle) or $x < $linesToReturn)
{
$line = fgets($handle); // Read the line
$lessons[$filename][] = $line;
$x++; // Increase the counter
}
}
}
// creates a blank list if no files or valid files were found.
if (!isset($lessons))
$lessons = array();
// The rest of the page just builds a simple table to display each lesson.-
echo '<h1>id & names</h1>';
echo '<table>';
echo '<th>id</th><th>name</th>';
foreach ($lessons as $file => $details)
{
echo '<tr><td>' . $details[0] . '</td><td>' . $details[1] . '</td> </tr> ';
}
echo '</table>';
但这不起作用。我做错了什么?