Jump to content

zimbabweo

Members
  • Posts

    6
  • Joined

  • Last visited

zimbabweo's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Thanks for the reply. I just tried moving the in_array stuff into that block and it didn't work. In fact, I couldn't add new songs, so that breaks something else. I think the submit button is working okay since when I add a new song name and click submit it does add it to the list. And my sort and shuffle buttons work. Everything works except the deduplication.
  2. I can't figure out what's wrong with this. The program works, but the deduplication doesn't work. The error I get is : Warning: in_array() [function.in-array]: Wrong datatype for second argument in line 67. I bolded line 67 below, which is an if (in_array ) statement. in_array wants an array for the second argument, which I have, so don't see the problem. Any suggestions? TIA ---------------------------------------------------------------------- <!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 // handle parameters in the URL if (isset($_GET['action'])) { if ((file_exists("SongOrganizer/songs.txt")) && (filesize("SongOrganizer/songs.txt") != 0)) { $SongArray = file("SongOrganizer/songs.txt"); // menu for Deduplication, Sort, Shuffle 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 switch statement // save the song list after it has been modified 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"); } } // read song file data into the $ExistingSongs array 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"); } } // check to see if the song name entered is already in the song list // this is where it has a problem. 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>"; } // add new song to the list file 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"; } } // display the song list, or empty message if song list is empty 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>"; } ?> <!-- Show hyperlinks for the three functions in the switch statement (Sort, Remove Dups, Shuffle) --> <p> <a href="SongOrganizer.php?action=Sort%20Ascending">SortSong List</a><br /> <a href="SongOrganizer.php?action=Remove%20Duplicates">Remove Duplicate Songs</a><br /> <a href="SongOrganizer.php?action=Shuffle">Shuffle Song List</a><br /> </p> <!-- web form for entering new song names into the song list --> <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>
  3. Barand, that works perfectly!! Many thanks.
  4. I can't figure out how to combine readfile() with <pre> tags so that the \n linebreaks in my text file are displayed properly. // works but doesn't print line breaks echo readfile("filename.txt"); // doesn't work -- parse error echo "<pre>readfile("filename.txt")</pre>"; // doesn't work -- parse error echo "<pre>{readfile("filename.txt")}</pre>\n"; Any tips to solve this?
  5. haha!! Works great now, thanks!! So I was just missing two $ symbols before my variables. Not used to PHP. FYI, i'm using Notepad++. Everything is color coded, but errors don't jump out at me like they do in other IDEs I'm used to.
  6. A simple math problem, but I can't get it working. If I enter 50 for $hours and 10 for $hourly I keep getting 400 $weekPay. It refuses to calculate OT pay. I don't understand what's wrong. <?php $name = $_POST['employeeName']; $hours = $_POST['hoursWorked']; $hourly = $_POST['hourlyPay']; echo "<p>Week pay for $name \n</p>"; echo "<p>Hours worked: $hours \n</p>"; echo "<p>Pay rate: $hourly \n</p>"; if ($hours > 40) { $otHours = $hours - 40; echo "<p>OT Hours: $otHours\n</p>"; $weekPay = (($hourly * 40) + (1.5 * hourly) * otHours); } else $weekPay = $hours * $hourly; echo "<p>Week pay: $weekPay </p>"; ?>
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.