How to put text file into alphabetical order?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
tom456
Forum Newbie
Posts: 22
Joined: Fri Aug 15, 2008 2:38 am

How to put text file into alphabetical order?

Post by tom456 »

when im inserting some text into following text field and press "submit" button to save it, then it saves the written data by back-to-back order..

here's the script:

<?
if($_POST['Submit']){
$open = fopen("textfile.txt","a+");
$text = $_POST['update'];
fwrite($open, $text);
fclose($open);
echo "File updated.<br />";
echo "File:<br />";
$file = file("textfile.txt");
foreach($file as $text) {
echo $text."<br />";
}
}else{
$file = file("textfile.txt");
echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
echo "<textarea Name=\"update\" cols=\"50\" rows=\"10\">";
foreach($file as $text) {
echo $text;
}
echo "</textarea>";
echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n
</form>";
}
?>

i read some topics too but i was unable to find something useful..
baileylo
Forum Newbie
Posts: 13
Joined: Sun Sep 30, 2007 12:48 am

Re: How to put text file into alphabetical order?

Post by baileylo »

Code: Select all

<?
if($_POST['Submit']){
$open = fopen("textfile.txt","a+");
$text = $_POST['update'];
fwrite($open, $text);
fclose($open);
echo "File updated.<br />";
echo "File:<br />";
$file = file("textfile.txt");
foreach($file as $text) {
echo $text."<br />";
}
}else{
$file = file("textfile.txt");
sort($file);
echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
echo "<textarea Name=\"update\" cols=\"50\" rows=\"10\">";
foreach($file as $text) {
echo $text;
}
echo "</textarea>";
echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n
</form>";
}
?>
So i'm sorting the textfile before you output it to the textarea.

Which sort of does the trick otherwise i think it'll be a pain in the button....are you modifying the textarea?
User avatar
tom456
Forum Newbie
Posts: 22
Joined: Fri Aug 15, 2008 2:38 am

Re: How to put text file into alphabetical order?

Post by tom456 »

baileylo wrote:

Code: Select all

<?
if($_POST['Submit']){
$open = fopen("textfile.txt","a+");
$text = $_POST['update'];
fwrite($open, $text);
fclose($open);
echo "File updated.<br />";
echo "File:<br />";
$file = file("textfile.txt");
foreach($file as $text) {
echo $text."<br />";
}
}else{
$file = file("textfile.txt");
sort($file);
echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
echo "<textarea Name=\"update\" cols=\"50\" rows=\"10\">";
foreach($file as $text) {
echo $text;
}
echo "</textarea>";
echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n
</form>";
}
?>
So i'm sorting the textfile before you output it to the textarea.

Which sort of does the trick otherwise i think it'll be a pain in the button....are you modifying the textarea?
i dont know
User avatar
tom456
Forum Newbie
Posts: 22
Joined: Fri Aug 15, 2008 2:38 am

Re: How to put text file into alphabetical order?

Post by tom456 »

and the text file is here: http://hot.ee/antsman333/list.txt
Post Reply