Sorting a list

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
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Sorting a list

Post by Chris Corbyn »

Hi,

I have a page on my site which is entirely a long list of <a href=> for a selection of midi files.

I'm constantly adding to the list and it is not practical to keep finding the correct place in the list to maintain an alphabetical order.

I know you can sort arrays but is there any way I can sort my list?

I figured I could create an array which contains all of my links and then sort the array but I wasn't sure how to implement it so that each value in the array is a link or how to echo the sorted list back out onto the page in the form of links.

Sorry it probably seems like an obvious answer but I'm new to PHP.

Thanks
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

if you save the list in a mysql db then you can just call it alphabetically (regardless how you enter it into the db)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Thanks.

I'll have go but I know nothing at all about mysql... I can't even work out how to connect to the server my file are on LOL.

Erm... if anyone else has any ideas how to just contain all the links inside a variable, then sort the variable and echo it back out onto the page please let me know.

I admit mySql would be better (especially since I could include a search page) but I just don't know where to start. I'm not running the server.. it's my university's server and I've checked with phpinfo() and they are using mysql. It gives this info about it:
MySQL Support enabled
Active Persistent Links 0
Active Links 0
Client API version 4.0.12
MYSQL_MODULE_TYPE external
MYSQL_SOCKET /tmp/mysql.sock
MYSQL_INCLUDE -I/usr/local/mysql/mysql-standard-4.0.12-sun-solaris2.8-sparc/include
MYSQL_LIBS -L/usr/local/mysql/mysql-standard-4.0.12-sun-solaris2.8-sparc/lib -lmysqlclient
Does this give any clues how to create a db and connect to it etc? What do I need to do.. I've heard that you use the command line? I downloaded phpMyAdmin and put it in my web space but when I open it I get an error
phpMyAdmin tried to connect to the MySQL server, and the server rejected the connection. You should check the host, username and password in config.inc.php and make sure that they correspond to the information given by the administrator of the MySQL server.

Error

MySQL said:


#2002 - Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

[Documentation]
And when I try going to config.inc.php I just get a blank page.

Thanks
aleigh
Forum Commoner
Posts: 26
Joined: Thu Mar 25, 2004 11:06 am
Location: Midwestern United States
Contact:

Re: Sorting a list

Post by aleigh »

d11wtq wrote:Hi,

I have a page on my site which is entirely a long list of <a href=> for a selection of midi files.

I'm constantly adding to the list and it is not practical to keep finding the correct place in the list to maintain an alphabetical order.

I know you can sort arrays but is there any way I can sort my list?

I figured I could create an array which contains all of my links and then sort the array but I wasn't sure how to implement it so that each value in the array is a link or how to echo the sorted list back out onto the page in the form of links.

Sorry it probably seems like an obvious answer but I'm new to PHP.

Thanks
I would look at putting the URLs into an array like you mentioned, and then you want to use something like sort() to sort it.

If you have song title and filename, though, I would do it like this;

$files['Some Song']='somesong.mid';
$files['Other Song']='othersing.mid';
$files['A Song']='zsong.mod';

ksort($files);

foreach($files as $n => $v) {
print '<A HREF="'.$v.'">'.$n.'</A><P>';
}

If you want to sort on the file-names rather than the titles use asort() instead.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Excellent... That works just the way I want it to. I'll use that for now but I guess it would make sense to learn mysql.

If anyone else wants offer advice on where to start with mysql feel free to post.

Thanks for indicating how to sort my array aleigh!
Post Reply