Jump to content

Alphabetizing Text File Information


brali99

Recommended Posts

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>

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>';
	}
}
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.