Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

listing in alphabetical order..

Status
Not open for further replies.

JohnnyT

Programmer
Jul 18, 2001
167
GB
I have a string such as this :

$postings = "1 ADSR|
1 Armoured Field Ambulance|
94 Locating Regiment|
97 Gurkha Infantry Brigade|
9th (The Queens Royal) Lancers|
10 Fd Wksp REME|
AAC Arborfield|
ACIO|
";

This string is held in its own file called postings.php and 'required' by the scripts in which it is used (it is also a lot longer than this but the same format)

It forms a drop down box in a form that allows ex-forces members to select the postings that they were sent to.

They can also email me with postings that aren't on the list. So.. here's the problem I have...

I need to write a script that will allow me to open the file (ie. postings.php), add a 'posting', re-sort them into alphabetical order and then re-write the file.

Any got any ideas ??

I know I can open the file with :
if (file_exists("postings.php") {
$file_contents = fopen("path/to/file.txt", "r+");
}
else {
echo "files gone awol !";
}

... but as for adding the new postings and then alphabetically sorting them and writing them back to the file.. well, to say I was a bit stuck would be an understatement.

A big thanks for any help you can give me.

Cheers

JT I don't make mistakes, I'm merely beta-testing life.
 
Ever thought about using a database? It's perfect for this purpose... //Daniel
 
daniel

Thanks for your reply mate. I thought about using a database but if poss. I would prefer to do it with a file (it would be a LOT of work to convert it to a database now)

Is that the only way ? Or is it possible to do it as it is ?

Cheers

JT I don't make mistakes, I'm merely beta-testing life.
 
It is possible, but a database would be easier and much more efficient. Anyway, I would recommend that you store everything in a text file instead of a PHP file. You could then use the file function to read the file into an array, add an element to it and then use sort to sort the entries. //Daniel
 
Thanks daniel, I think I'll look into that. I'm fairly new to php so still scratching around trying to find out what it can do (and of course, always learning).

I'll try sticking them in a text file, and I'll get into the php manual to find out how to parse it into an array, add one and then sort 'em.

Thanks for your help mate.

(Stand by.... I might be back ;-) )

Cheers

JT I don't make mistakes, I'm merely beta-testing life.
 
Right,

I've now got this
Code:
$file = "./".$force."_files/postings.txt";
$fp = fopen($file,'r'); 
$postings = fread($fp,filesize($file)); 
fclose($fp);

But I need to sort the $postings into alphabetical order..

I've tried sort($postings) but I get an error saying "wrong datatype in sort call", any ideas ?

Cheers

JT I don't make mistakes, I'm merely beta-testing life.
 
Here is how I would do it:
Code:
$file = "./".$force."_files/postings.txt";
$lines = file($file);
/* This adds a record */
$lines[] = "New entry\r\n";
/* Write the new contents to a file */
$fp = fopen($file, 'w');
foreach ($lines as $line)
{
     fwrite($fp, $line);
}
fclose($fp);
/* Get the whole contents to a string variable */
$contents = implode("", $lines);
//Daniel
 
Daniel

Thanks for that mate. I managed to sort it out in the end. For anyone else who's stuck, here's how I did it...

Code:
$file = "./".$force."_files/corps.txt";
$fp = fopen($file,'r'); 
$corps_list = fread($fp,filesize($file)); 
fclose($fp); 

$corps_sort = explode ("|", $corps_list);

function cmp ($a, $b) { 
$tmp[0]=strtoupper($a); 
$tmp[1]=strtoupper($b); 
sort($tmp); 
return (strcmp(strtoupper($tmp[1]) , strtoupper($b))) ? 1 : -1; 
} 
usort($corps_sort, "cmp");


$corps_list = implode ("|", $corps_sort);

Thanks (as always) for all your help

Cheers

JT I don't make mistakes, I'm merely beta-testing life.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top