Link to home
Start Free TrialLog in
Avatar of assaultkitty
assaultkitty

asked on

PHP program

I am having a problem with the program at line 79.  I am suspecting that I am missing an else.  I am working on a literal program from this book

http://books.google.com/books?id=yr02MM9UMA8C&pg=PT390&lpg=PT390&dq=$SongFile+%3D+fopen(+++++++++++++%22SongOrganizer/songs.txt%22,&source=bl&ots=kLi1Xwhh5C&sig=IEaOVy4Titxm79HWk9I2nq92NdY&hl=en&ei=jzfkTuXQIoXNtgeJ-_WUDg&sa=X&oi=book_result&ct=result&resnum=1&sqi=2&ved=0CB0Q6AEwAA#v=onepage&q=%24SongFile%20%3D%20fopen(%20%20%20%20%20%20%20%20%20%20%20%20%20%22SongOrganizer%2Fsongs.txt%22%2C&f=false

I can get the program to work with the song.txt without the SongOrganizer.  I do not know what the errors.  Can you help me?  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Song Organizer</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</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;
                  }
                  if (count($SongArray)>0)
                  {
                        $NewSongs = implode($SongArray);
                        $SongStore = fopen("SongOrganizer/songs.txt","w+");
                        if ($SongStore === false)
                              echo "There was an error updating the song file. {$_GET['action']}\n";
                        else
                        {
                              fwrite($SongStore, $NewSongs);
                              fclose($SongStore);
                        }
                  }
                  else
                                    unlink("SongOrganizer/songs.txt");
            }
}
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");
            }
if (in_array($SongToAdd, $ExistingSongs))
{
      echo "<p>The song you entered already exists!<br />\n";
      echo "Your song was not added to the lists.</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";
      }
}
if ((!file_exists("SongOrganizer/songs.txt"))
      || (filesize("SongOrganizer/songs.txt")
      == 0))
      echo "<p>There are no songs in the list.</p>\n";
}
// display songs
      $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 Song</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>


Avatar of tcremel
tcremel
Flag of France image

yes before the line
// display songs

Open in new window

add a
else {

Open in new window


then add a } after the line
echo "</table>\n";

Open in new window

I think the problem was in line because the file does not exist, but you try to open it !
You might want to reconsider your choice of books.  I notice that this one has only one review, and that is a big red flag.  On the first page I looked at, following the link posted with the question, I found a script that modified the underlying data model on the basis of a GET request.  There may be other things about the book that are good, but to me that is another, giant, red flag that screams "technically incompetent!"

Here is a book that I can recommend.  Very readable with good examples and a downloadable code library you can copy and modify for your own applications.  Now in its 4th printing, it has been a permanent part of my professional library since Version One.
http://www.sitepoint.com/books/phpmysql4/

To the instant case, you probably want to add error_reporting(E_ALL) to the top of your PHP script.  You also might want to have a default action path in the switch/case logic.  This instruction looks suspicious to me: $NewSongs = implode($SongArray);  See the man page here:
http://php.net/manual/en/function.implode.php

Implode() is one of those PHP functions that is left over from earlier, unsophisticated version of PHP.  If you're going to use it to turn an array of data strings into a single long string, you probably want to be using the "glue" argument.  For that you would probably want specify PHP_EOL (the standard end-of-line character).
Avatar of rinfo
rinfo

Just put a curly bracket start after this line
if ((!file_exists("SongOrganizer/songs.txt"))
      || (filesize("SongOrganizer/songs.txt")
      == 0))
I have checked there is nothing wrong syntactically.
Script will run albeit not with desired results without the start curly bracket i have mentioned.
Avatar of assaultkitty

ASKER

Do I name the file SongOrganizer/song.txt?
results.docx
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial