使用php从txt文件中检索数据

时间:2018-11-06 06:09:01

标签: php persistent

我正在做一个基于Web的编程入门实验室。我有一个html文件,只有一个提交按钮“可以教我有关地理的知识”。该文件使用php文件从大写/国家txt文件中检索数据,以显示不同国家/地区的不同大写字母。

这是我的html文件

<html>
<head>
    <title>Modify2</title>
    <link rel ="stylesheet" type="text/css" href="sample.css">
</head>
<body>

    <h1>Modify2</h1>

<p>
    <form action = "modify2.php" method = "post" >


    <input type = "submit" value = "Teach me some Geography">


    </form>
</p>

</body>
</html>

这是我的php文件

<html>
<head>
    <title>Modify2</title>
    <link rel ="stylesheet" type="text/css" href="sample.css">
</head>
<body>

    <h1>Modify2</h1>

    <?php
        $capitalFile = fopen("capital-info.txt","r");   // open the file for          read operations
        $countryRecord1 = fgets($capitalFile);      // read a line from the file
        $countryRecord2 = fgets($capitalFile);
        $countryRecord3 = fgets($capitalFile);
        $countryRecord4 = fgets($capitalFile);
        fclose($capitalFile);

        list ($country1, $capital1) = explode(":", $countryRecord1);
        list ($country2, $capital2) = explode(":", $countryRecord2);
        list ($country3, $capital3) = explode(":", $countryRecord3);
        list ($country4, $capital4) = explode(":", $countryRecord4);

        print("<p>$capital1 is the capital city of $country1.</p>");
        print("<p>$capital2 is the capital city of $country2.</p>");
        print("<p>$capital3 is the capital city of $country3.</p>");
        print("<p>$capital4 is the capital city of $country4.</p>");

    ?>

</body>
</html>

我得到的输出是“ $ capital1是$ country1的首都”,依此类推 即时通讯使用xampp作为网络服务器。

任何帮助将不胜感激

0 个答案:

没有答案