2016-03-24 3 views
0

In meinem Skript in Zeile 67 wird die Echo-Anweisung "Your Song wurde zur Liste hinzugefügt" angezeigt, bevor ich Songs zur Liste hinzufüge. Kann mir jemand sagen warum? Es ist unter dem Kommentar Schritt 8 und Schritt 9. Ich habe xdebug mit erhabenen Text 3 eingerichtet, aber ich habe keine Ahnung, wie man es benutzt. Wenn ich Haltepunkte setze und das Skript ausführe, erhalte ich eine Reihe nicht initialisierter Variablen in meinem Kontextbereich.Mein SongOrganizer-Skript zeigt sonst ein Echo an, wenn dies nicht der Fall ist. Zeile 67. Wie repariere ich es?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 

<head> 
<title>Song Organizer</title> 
</head> 

<body> 

<h1>Song Organizer</h1> 
<?php 

    if (isset($_GET['action'])) { 
     if ((file_exists("SongOrganizer/songs.txt")) && (filesize("SongOrganizer/songs.txt") != 0)) { 
      $SongArray = file("SongOrganizer/songs.txt"); 
      switch ($_GET['action']) { 
       case 'Remove Duplicates': 
        $SongArray = array_unique($SongArray); 
        $SongArray = array_values($SongArray); 
        break; 
       case 'Sort Ascending': 
        sort($SongArray); 
        break; 
       case 'Shuffle': 
        shuffle($SongArray); 
        break; 

      } // End of the Switch Statement 


      if (count($SongArray)>0) { 
       $NewSongs = implode($SongArray); 
       $SongStore = fopen("SongOrganizer/songs.txt", "wb"); 
       if ($SongStore === false) 
        echo "There was an error updating the song file\n"; 
      else { 
       fwrite($SongStore, $NewSongs); 
       fclose($SongStore); 
       } 
      } 
      else 
       unlink("SongOrganizer/songs.txt"); 
     } 
    } 


    // Step 7 
    if (isset($_POST['submit'])) { 
     $SongToAdd = stripslashes($_POST['SongName']) . "\n"; 
     $ExistingSongs = array(); 
     if (file_exists("SongOrganizer/songs.txt") && filesize("SongOrganizer/songs.txt") > 0) { 
      $ExistingSongs = file("SongOrganizer/songs.txt"); 

     } 
    } 

      // Step 8 and Step 9  
      if (in_array($SongToAdd, $ExistingSongs)) { 
       echo "<p>The song you entered already exists!<br />\n"; 
       echo "Your song was not added to the list.</p>"; 
      } else { 
       $SongFile = fopen("SongOrganizer/songs.txt", "ab"); 
       if ($SongFile === false) 
        echo "There was an error saving your message!\n"; 
       else { 
        fwrite($SongFile, $SongToAdd); 
        fclose($SongFile); 
        echo "Your Song has been added to the list.\n"; 
       } 
      } 

    // Step 10 
    if ((!file_exists("SongOrganizer/songs.txt")) || (filesize("SongOrganizer/songs.txt") == 0)) 
     echo "<p>There are no songs in the list.</p>\n"; 
    else { 
     $SongArray = file("SongOrganizer/songs.txt"); 
     echo "<table border=\"1\" width=\"100%\" style=\"background-color:lightgray\">\n"; 
     foreach ($SongArray as $Song) { 
      echo "<tr>\n"; 
      echo "<td>" . htmlentities($Song) . "</td>"; 
      echo "</tr>\n"; 
     } 
     echo "</table>\n"; 
    } 
?> 

<p> 
<a href="SongOrganizer.php?action=Sort%20Ascending">Sort Song List</a><br /> 
<a href="SongOrganizer.php?action=Remove%20Duplicates">Remove Duplicate Songs</a><br /> 
<a href="SongOrganizer.php?action=Shuffle">Randomize Song List</a><br /> 
</p> 

<form action="SongOrganizer.php" method="post"> 
<p>Add a New Song</p> 
<p>Song Name: <input type="text" name="SongName" /></p> 
<p><input type="submit" name="submit" value="Add Song to List" /><input type="reset" name="reset" value="Reset Song Name" /></p> 
</form> 

</body> 

</html> 
+0

ich diese Zeilen am Anfang hinzugefügt zu finden Fehler: error_reporting (E_ALL); \t ini_set ('display_errors', 1); – Olympus

+0

Ich fügte auch ein Echo 'Datei gefunden, Ausführung fortsetzen' hinzu; - Gleich nach der ersten verschachtelten if-Anweisung – Olympus

+0

kann ich das Problem immer noch nicht finden – Olympus

Antwort

0

Ich glaube, die Antwort, mein Problem war, dass Schritt 8 und Schritt 9 sollte innerhalb von Schritt if-Anweisung verschachtelten gewesen sein 7. etwa so:

// Step 7 
if (isset($_POST['submit'])) { 
    $SongToAdd = stripslashes($_POST['SongName']) . "\n"; 
    $ExistingSongs = array(); 
    if (file_exists("SongOrganizer/songs.txt") && filesize("SongOrganizer/songs.txt") > 0) { 
     $ExistingSongs = file("SongOrganizer/songs.txt"); 


     // Step 8 and Step 9  
     if (in_array($SongToAdd, $ExistingSongs)) { 
      echo "<p>The song you entered already exists!<br />\n"; 
      echo "Your song was not added to the list.</p>"; 
     } else { 
      $SongFile = fopen("SongOrganizer/songs.txt", "ab"); 
      if ($SongFile === false) 
       echo "There was an error saving your message!\n"; 
      else { 
       fwrite($SongFile, $SongToAdd); 
       fclose($SongFile); 
       echo "Your Song has been added to the list.\n"; 
      } 
     } 

    } 
} 
Verwandte Themen