[SOLVED] PHP Sorting

This is a discussion on "[SOLVED] PHP Sorting" within the PHP Forum section. This forum, and the thread "[SOLVED] PHP Sorting are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Closed Thread
 
LinkBack Thread Tools
  #1  
Old Oct 18th, 2007, 01:41
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] PHP Sorting

Hello once again,

I have the following code to sort a bunch of arrays:
PHP: Select all

<?php
$narray
[0]="2 Alfred";
$narray[1]="7 Robert";
$narray[2]="3 Deepak";
$narray[3]="5 Teresa";
$narray[4]="1 Joshua";
$narray[5]="8 Chandni";
$narray[6]="4 Sadiq";
$narray[7]="6 Vladimir";
sort($narray);
for(
$i=0$i<8$i++)
{
print 
$narray[$i] . "<br />";
}
?> 




(This is an example)
It is working without problems, but now I want to apply some php "magic"... I would like to place three links above this code that read:
1) Sort numerically
2) Sort alphabetically
3) Sort normally (the way it is in the script)

Now, when clicked, the links will simply add "?sort=value" to the URL. The "value" is one of the three above.
On my actual page (since the above names are an example) there is a list of 'downloadable' files that are, by default, listed in the order that they were posted (numerically by date - ex: July 10, August 23, October 17). Then, the second link is to sort them by their title (alphabetically by title - ex: File A, File B, File C). Finally, the last link will sort them by the name of the person that posted them (alphabetically by name - ex: Amanda, Mark, Ryan, Wendy). I am sure that I need more than the above code, and probably separate arrays for all three points, but I am not sure of how to do this since I am still new to php .

Any help would be much appreciated.

Thanks,
SWagner

PS: If you do not quite get what I am asking, don't hesitate to ask.
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!

  #2  
Old Oct 18th, 2007, 05:34
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Sorting

Is the list going to static, or dynamically read out of a database?
How long is the list going to be?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #3  
Old Oct 18th, 2007, 11:19
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Sorting

Well, at the moment, I have the list as static html. The problem is, it is not much of a list in terms of using <ul> or <ol>. I just have several divs, one after the other. The divs all have a unique id, so it might still work. But if you think it is better, I can make put each div into a <li> although I don't think that will correspond well with my stylesheet...

The list is going to have a maximum number of 100 items.
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #4  
Old Oct 18th, 2007, 11:41
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Sorting

I would use a MySql database for this. Put your arrays into a database with the following fields (number,name,timestamp). Then you can just change the query to sort the items:
PHP: Select all

 // sort normal
$query=mysql_query("SELECT * FROM tablename ORDER BY timestamp (ASC or DESC)");

// sort numerically
$query=mysql_query("SELECT * FROM tablename ORDER BY number (ASC or DESC)");

// sort alphabetically
$query=mysql_query("SELECT * FROM tablename ORDER BY name (ASC or DESC)"); 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #5  
Old Oct 18th, 2007, 12:02
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Sorting

Sorry about this: How do you create a MySQL Database. I have never done something like this...
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #6  
Old Oct 18th, 2007, 12:32
Marc's Avatar
Staff Manager

SuperMember
Join Date: Apr 2007
Location: Scotland, UK
Posts: 1,794
Thanks: 0
Thanked 17 Times in 17 Posts
Re: PHP Sorting

Have you got hosting/xampp?
__________________
Marc
Staff Manager - Webforumz.com


Want to be a moderator? PM me.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #7  
Old Oct 18th, 2007, 12:41
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Sorting

I am not sure what hosting/xampp is, but if you're asking if my server has MySQL, then yes.
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #8  
Old Oct 18th, 2007, 13:58
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: PHP Sorting

Your server as in you have physical access to it or as in you have to upload to it?

If you are using the command prompt mysql.
Type the following in:
Code: Select all
create database name;
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)

Last edited by alexgeek; Oct 18th, 2007 at 14:00.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #9  
Old Oct 18th, 2007, 23:22
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Sorting

OK, hold on...
Let me tell you what the whole story is here: Within my website, there is a directory (secure/) which is password protected by .htaccess. In this directory, there is the page that contains the "list" of files that people have uploaded (index.php). The way people upload files is through another page in the secure/ directory (upload.php). This page uses php to transfer the uploaded files into a new folder inside secure/ (files/). OK so far? The files are called and output by index.php in the order that is determined by the "?sort=" value in the URL. Now, to be honest, I have no idea how to do any of this... So, I am going to need a lot of help .

Thanks,
SWagner
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #10  
Old Oct 19th, 2007, 05:27
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Sorting

When a file is uploaded and you move it to the secure folder, then you insert a new row into your database with the number and name of the file and the timestamp when it was uploaded. Then in index.php your code will look something like so:

PHP: Select all

 $sort=(isset($_GET['sort'])?$_GET['sort']:'timestamp');
$query=mysql_query("SELECT * FROM tablename ORDER BY ".$sort." ASC");
while(
$row=mysql_fetch_object($query)) {
    
// output file
    
echo('<p>'.$row->number.': <a href="'.$row->name.'">'.$row->name.'</a>');

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #11  
Old Oct 20th, 2007, 00:46
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Sorting

I don't want to annoy anyone here, but could someone give me the basic rundown for a MySQL Database, how it works, and how to create one? Honestly, I am really stumped ...
All I need is two codes: one to upload the files to a database through a form, and one to call the files from that database and display them in a list.
Like I said, I have no idea whatsoever how to do this (don't get frustrated with me!).

Thanks,
SWagner
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Closed Thread

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help sorting an array djeyewater PHP Forum 6 Apr 18th, 2008 15:16
[SOLVED] Array sorting welshstew Classic ASP 6 Nov 28th, 2007 16:45
Sorting a PHP array fallen_angel PHP Forum 3 Apr 20th, 2007 22:18
Need help sorting highscore - rsort() mofle PHP Forum 2 Apr 8th, 2007 09:41
Mysql sorting problem.... mills Databases 2 Jul 26th, 2005 09:08


All times are GMT. The time now is 23:59.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42