brali99 Posted May 4, 2012 Share Posted May 4, 2012 I am new to PHP so I'm sorry if anything I ask isn't clear. I'm actually a writer, but my boss randomly decided I need to learn PHP so here I am My first project is to take information from a simple HTML form that asks for users to fill out three text boxes, select if they are user 1 or user 2, and upload a file. I then have to store the information in a text file and display that information in the browser directly below the HTML form. The information must be ordered alphabetically by the first letter of whatever is entered in the first text box. Each entry must be on its own line. For example: Person 1 enters: Sally Mae Johnson User 1 Flowers.jpg Person 2 comes along later and enters: George Michael Johnson User 2 books.jpg Right now it displays in the order entered like this: Sally Mae Johnson User 1 Flowers.jpg George Michael Johnson User 2 books.jpg I need it to display in alphabetical order by the first letter of the first name like this: George Michael Johnson user 2 books.jpg Sally Mae Johnson User 1 Flowers.jpg I'm so close, but I just can't figure out how to finish it up. If anyone can help they'd be a life saver! Here's what I wrote so far: <body> <?php $fone = @$_POST["one"]; $ftwo = @$_POST["two"]; $fthree = @$_POST["three"]; $fselect = @$_POST["select"]; if ($_FILES) { $name = $_FILES['upload']['name']; (move_uploaded_file($_FILES['upload']['tmp_name'], "uploads/$name")); } //write to the file $values = "$fone\t"; $values .= "$ftwo\t"; $values .= "$fthree\t"; $values .= "$fselect\t"; $values .= "<img src='uploads/$name'><br />\n"; //open and write to the file $fp = @fopen("store.txt", "a") or die("Couldn't open the file!"); $numBytes = @fwrite($fp, $values) or die ("Couldn't write values to file!"); @fclose($fp); ?> <form action="test_it2.php" enctype="multipart/form-data" method="post"> Box 1: <input type="text" name="one" size="15" /> Box 2: <input type="text" name="two" size="15" /> Box 3: <input type="text" name="three" size="15" /> Select One: <select name="select"><option value="empty">Please Select</option><option value="user1">User 1</option> <option value="user2">User 2</option> <p>Upload a File:</p> <p><input type="file" name="upload" /> <input type="hidden" name="MAX_FILE_SIZE" value="30000" /> <input type="submit" name="submit" value="submit" /> <input type="hidden" name="submitted" value="submitted" /> </p> </form> <?php print "<P>Here are the users:<br />"; $file = "store.txt"; if (file_exists($file)) { $file1 = fopen("store.txt" , "r"); while (!feof($file1)) { $display = fgets($file1, filesize("store.txt")); echo $display . " "; } fclose($file1); } else { echo "<P>Error occured! Please try again!</p>"; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/262089-alphabetizing-text-file-information/ Share on other sites More sharing options...
dmikester1 Posted May 4, 2012 Share Posted May 4, 2012 I assume eventually there will be much more than 2 lines in the text file? Instead of writing to a text file, it would be much more robust and easier to sort by adding the entries to a mysql database. Quote Link to comment https://forums.phpfreaks.com/topic/262089-alphabetizing-text-file-information/#findComment-1343158 Share on other sites More sharing options...
xyph Posted May 4, 2012 Share Posted May 4, 2012 http://php.net/manual/en/function.file.php http://php.net/manual/en/function.sort.php <?php $lines = file('store.txt'); sort($lines); foreach( $lines as $line ) { echo "$line<br>\n"; } ?> Let me know if you need further explanation. Quote Link to comment https://forums.phpfreaks.com/topic/262089-alphabetizing-text-file-information/#findComment-1343168 Share on other sites More sharing options...
brali99 Posted May 7, 2012 Author Share Posted May 7, 2012 I assume eventually there will be much more than 2 lines in the text file? Instead of writing to a text file, it would be much more robust and easier to sort by adding the entries to a mysql database. Yes it will be more than two lines. I have no idea what they are using for; I haven't been given any more details than what I gave in my original post. I thought a database would be easier too but the boss/clients say it must be a text file. Quote Link to comment https://forums.phpfreaks.com/topic/262089-alphabetizing-text-file-information/#findComment-1343582 Share on other sites More sharing options...
brali99 Posted May 7, 2012 Author Share Posted May 7, 2012 Xyph, your code returned an error for me. It says "sort() expects parameter 1 to be array, string given" and "Invalid argument supplied for foreach()" What am I doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/262089-alphabetizing-text-file-information/#findComment-1343585 Share on other sites More sharing options...
cyberRobot Posted May 7, 2012 Share Posted May 7, 2012 Xyph, your code returned an error for me. It says "sort() expects parameter 1 to be array, string given" and "Invalid argument supplied for foreach()" What am I doing wrong? That sounds like the text file wasn't imported. Did you double check that file() is pointing to the correct location? You could try adding the following line of code after the call to file(): <?php //... $lines = file('store.txt'); var_dump($lines); ...// ?> If it displays anything, you have the data from the text file. Otherwise, the content wasn't imported correctly. You should also check that the same variable is being used for the file(), sort(), and foreach() lines of code. Quote Link to comment https://forums.phpfreaks.com/topic/262089-alphabetizing-text-file-information/#findComment-1343690 Share on other sites More sharing options...
xyph Posted May 7, 2012 Share Posted May 7, 2012 Xyph, your code returned an error for me. It says "sort() expects parameter 1 to be array, string given" and "Invalid argument supplied for foreach()" What am I doing wrong? Okay, here's the spoon-fed version <?php $filename = 'yourfile.txt'; if( !file_exists($filename) || !is_readable($filename) ) { echo 'File does not exist or can not be read: '.$filename; } else { $data = file($filename); if( $data === FALSE ) { echo 'There was an error using file() on '.$filename; } elseif( empty($data) ) { echo $filename.' is empty'; } else { foreach( $data as $line ) { echo $line.'<br>'; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/262089-alphabetizing-text-file-information/#findComment-1343734 Share on other sites More sharing options...
brali99 Posted May 7, 2012 Author Share Posted May 7, 2012 Xyph, your code does not alphabetize. It simply lists the entries in the order they were entered. Quote Link to comment https://forums.phpfreaks.com/topic/262089-alphabetizing-text-file-information/#findComment-1343795 Share on other sites More sharing options...
xyph Posted May 7, 2012 Share Posted May 7, 2012 Add the sort() in there. Quote Link to comment https://forums.phpfreaks.com/topic/262089-alphabetizing-text-file-information/#findComment-1343845 Share on other sites More sharing options...
brali99 Posted May 7, 2012 Author Share Posted May 7, 2012 Oh, gosh! Duh!! How could I forget to add sort()? Thank you so much, xyph and everyone else who helped. I have a few other bugs to work out but it is now alphabetizing! Quote Link to comment https://forums.phpfreaks.com/topic/262089-alphabetizing-text-file-information/#findComment-1343847 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.