dqt83336 2015-07-17 10:04
浏览 62

使用$ GET从文本文件中读取特定行,然后回显它

I have a text file which is already created and appended by user and I am using this format to append it. New line after every input and separated by semicolon:

Movie Title1; Movie Rating1; Plot1;

Movie Title2; Movie Rating2; Plot2;

Movie Title3; Movie Rating3; Plot3;

Etc. etc.

I have managed to fix the above and now I want to read a specific line from the text file then echo it. This specific line is to be found through $GET parameter and based on Movie Title.

For example I click on a link, in this case a title from a movie, then I get the title name through $GET: movies.php to change to movies.php?title=MovieTitle. And then I get this current Movie Title name and read this specific line and echo it. Here is my code:

//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."
");          
    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 ("
",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! 
    }
    }
?>

So I want to echo it out with movie title being in and and movie rating .I hope you understand my question, thanks in advance!

  • 写回答

2条回答 默认 最新

  • doujiunai2169 2015-07-17 10:13
    关注

    First off you have errors in your syntax. Secondly this is what a database is for your solution is not going to scale. However to get it to work do something like....

    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];
         }
      }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比