我有一个已经创建并由用户附加的文本文件,我使用这种格式来附加它。
:电影Title1;电影Rating1;Plot1;
电影Title2;电影Rating2;Plot2;
电影Title3;电影Rating3;Plot3;
等。等。
我已经设法修复了上面的问题,现在我想从文本文件中读取特定的行,然后回显它。这一行是通过$GET参数根据Movie Title找到的。
例如,我点击一个链接,在本例中是电影的标题,然后我通过$ get: movies.php获取标题名,将其更改为movies.php?title=MovieTitle。然后我得到这个当前的Movie Title名称并读取这一行并回显它。下面是我的代码:
//Writes input from user to movies.txt
<?php
if (isset($_POST["submit"])) {
$title = $_POST['movieTitle'];
$rating = $_POST['movieRatings'];
$plot = $_POST['plot'];
$handle = fopen('movies.txt', 'a');
$names_array = array("$title","$rating","$plot");
$string = implode(';', $names_array);
fwrite($handle, $string."'n");
fclose($handle);
}
?>
//Reads line from movies.txt and adds it to a li, with movie title becoming a link
<?php
$filename = 'movies.txt';
$handle = fopen($filename, 'r');
$datain = fread($handle, filesize($filename));
$lines = explode ("'n",trim($datain));
foreach($lines as $line)
{
list($title,$rating,$plot) = explode(";",$line,3);
echo '<li><a href="movies.php?title='.$title.'">'.$title.'</a><span>'.$rating.'</span></li>';
}
?>//So far so good
//And now I want to read title name, and based on the
//title name find a specific line with rating and plot
//which belongs to current clicked movie title...
<?php
if (isset($_GET["title"])) {
$readin = file('movies.txt');
foreach ($readin as $fname)
$names_array = explode(';', $fname);
{
echo '<h1>'.$names_array[0]./*MovietitleName*/'</h1>''<h2>'.$rating.'</h2>'.$plot;//So I want to echo the specific "movieTitle movie, movieRating and moviePlot". What I've done so far is wrong, I need help here!
}
}
?>
所以我想用电影标题和电影评级来回应它。我希望你能理解我的问题,提前谢谢!
试试这个代码
if (isset($_GET["title"])) {
$readin = file('movies.txt');
foreach ($readin as $fname)
{
$names_array = explode(';', $fname);//this has to go here
if($_GET['title']==$names_array[0]){
echo '<h1>'.$names_array[0].'</h1><h2>'.$names_array[1].'</h2>'.$names_array[2];
}
}
}
首先你的语法有错误。其次,这就是数据库的作用,因为你的解决方案无法扩展。但是,要使其工作,请执行....
if (isset($_GET["title"])) {
$readin = file('movies.txt');
foreach ($readin as $fname)
{
$names_array = explode(';', $fname);//this has to go here
if($_GET['title']===$names_array[0]){//only echo if the title mathces
echo '<h1>'.$names_array[0]./*MovietitleName*/'</h1>''<h2>'.$names_array[1].'</h2>'.$names_array[2];
}
}
}