dongwuwu6104 2016-02-25 02:53
浏览 32
已采纳

SongOrganzier Isnt将歌曲添加到txt文件或Html表

Problem:

Create a Song Organizer script that stores songs in a text file. Include functionality that allows users to view the song list and prevents the same song name from being entered twice. Also, include code that sorts the songs by name, deletes duplicate entries, and randomizes the song list with the shuffle() function.

How Come my code isnt adding the song to the txt file or the table?

My Solution - > Broken, song isnt being appending to song list

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 
Strict//EN"
"http://w...content-available-to-author-only...3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://w...content-available-to-author-only...3.org/1999/xhtml">
<head>
<title>PHP Code Blocks</title>
<meta http-equiv="content-type"
     content="text/html; charset=iso-8859-1" />
</head>
<body>
<h1>Song Organizer</h1>
<?php

error_reporting(E_ALL);
ini_set('display_errors',1);

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
";
             else {
                  fwrite($SongStore, $NewSongs);
                  fclose($SongStore);
                  }
              }
          else
              unlink("SongOrganizer/songs.txt");     
              }

          }

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

     if (file_exists("SongOrganizer/songs.txt")
          && filesize("SongOrganizer/songs.txt")> 0) {
          $ExistingSongs = file("SongOrganizer/songs.txt");
         }

      }
   }
}



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


?>
<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=Shuffl e">
     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>
  • 写回答

1条回答 默认 最新

  • doutu6616 2016-02-25 04:19
    关注

    I just ran your code locally. You're never sending an action in the form so none of the code is executing.

    Inside the form, add something like: <input type="hidden" name="action" value="literally anything can go here" />

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效