2016-12-07 63 views
0

我有2个问题。第一个可能很简单,但我不知道该怎么做。我制作了一段代码,可以让您保存一位音乐演奏者和一首歌曲,保存后可以打开它,但是我想用一个按钮打开它,我该如何做。将按钮更改为<a>

第二个问题是当你打开保存的文件时,我得到了20行空的东西,1是满的(抱歉我的英文不好)。我如何将它改成正常线条,以便只在需要时生成线条。这里是我的2码:

文件名SEB2.php

<!DOCTYPE html> 
<html> 
<head> 
    <title></title> 
    <?php 

    if (!empty($_POST)) {   
       $artiest = $_POST["artiest"]; 
       $single = $_POST["single"]; 
        $fp = fopen("muziekcollectie.txt", "a+"); 
        fputs($fp, $artiest."\r\n"); 
        fputs($fp, $single."\r\n"); 
        fclose($fp); 
      } 

    ?> 
</head> 
<body> 
    <form name="woorden" method="post"> 
     Artiest:<input type="text" name="artiest"><br> 
     Single:<input type="text" name="single"><br> 
     <input type="submit" name="Add" value="Add"><br><br> 
    <form> 
     <a href="SEB2.php"><!-- <input type="submit" name="Watch" value="Watch" action="SEB2.php"> -->TEST</a> 
</body> 
</html> 

文件名SEB2.php

<!DOCTYPE html> 
<html lang="nl"> 
    <head> 
     <meta charset="utf-8"> 
     <title>Het gastenboek lezen</title> 
    </head> 
    <body> 
     <?php 
      $bestandsnaam = "muziekcollectie.txt"; 
      // aantal regels per bijdrage om in te lezen 
      // elke bijdrage bestaat uit 5 regels 
      $aantal_regels = 2; 
      // teller om bij te houden hoeveel regels 
      // zijn gelezen 
      $teller = 0; 
      // Aantal bijdragen wordt bijgehouden. 
      $aantal_bijdragen = 0; 
      // gastenboek openen om te lezen 
      if (file_exists($bestandsnaam)){ 
      $fp = fopen($bestandsnaam, "r"); 
      } 
      else{ 
      echo "<h2>De muziekcollectie is nog leeg!</h2> 
      <a href= 'SEB1.php'> 
      Wees de eerste die erin schrijft!</a> "; 
      exit; 
      } 
      while (!feof($fp)){ 
      $regel = fgets($fp); 
      if (!feof($fp)){ 
      if ($teller % $aantal_regels == 0){ 
      // kop afdrukken 
      $aantal_bijdragen++; 
      // echo "<hr>"; 

      echo "<h3>Bijdrage: $aantal_bijdragen</h3>"; 

      } 
      } 
      // regel afdrukken 
      echo "$regel <br>"; 
      // echo "<hr>"; 
      $teller++; 

      } 

      echo "<a href='SEB1.php'>Terug naar de 
      homepage</a>"; 
     ?> 
    </body> 
</html> 
+0

对于第一个问题,你可以改变的“一”标签的CSS,使它看起来像一个按钮,或者你可以使用“<按钮的onclick =“window.location的=“SEB1.php”> Terug naar de homepage“。第二个问题对我来说还不清楚,你可以多解释一下。 –

+0

也显示第二个问题的输出 –

回答

0

按钮链接并不难

 <button onclick="window.location.href='SEB2.php';"></button> 

我注意到你没有关闭你的表单,所以当你测试它时可能是问题。

我不确定你对第二部分的含义,但我认为这只是因为你的帖子没有空的检查,所以空的输入也会被添加到文件中。如果是这样的问题,这将解决这个问题:

if (!empty($_POST['artiest']) && !empty($_POST['single']))