Sorting Database Results

Reply

Join Date: Jan 2007
Posts: 2
Reputation: c0rkscrew is an unknown quantity at this point 
Solved Threads: 0
c0rkscrew c0rkscrew is offline Offline
Newbie Poster

Sorting Database Results

 
0
  #1
Jan 14th, 2007
Okay, I'm working on a small project for my daughter's class at school. They have a fundraiser running right now and they want to post their fund raiser data on the their school website. What they want is this, they want a page listing each student and the amount that they've managed to raise so far along with a ranking.

Something like this:

Teresa H. $85 #1
Jana L. $84 #2

etc...etc...

This i've already accomplished. But they also want the ability to list the students alphabetically, which I can do also, but they each name to be clickable with a link to a page about that student containing information about their particular fundraising efforts. I guess a blog of sorts. And I have this written as well. The only thing I can't figure out is how to add each individual ranking to the individual pages.

To get the ranking above, I just select all the data from the db and use mysql_numrows($result); with a loop to get the "ranking" but I can't seem to figure this out when I have to use a select statement that requires that the userid be a match. Example:

  1. $query = mysql_query("SELECT * FROM students where uid =$uid")
  2. or die(mysql_error());

Now how do I re-write my select statement to get my overall ranking for each student on their individual page?

Here's the basic db structure also:

uid, student_name, total_raised, homeroom

"total_raised" is the field that I'm using to get my Ranking.

Any help would be great b/c I'm stumped on this.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 138
Reputation: php_daemon is an unknown quantity at this point 
Solved Threads: 2
php_daemon php_daemon is offline Offline
Junior Poster

Re: Sorting Database Results

 
0
  #2
Jan 14th, 2007
You cannot determine the position of a record by selecting it alone.

The best idea, I think, is to save the ranking to the database as well. For example, upon the payment you can recalculate them and save to he database.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 44
Reputation: MCP is an unknown quantity at this point 
Solved Threads: 3
MCP MCP is offline Offline
Light Poster

Re: Sorting Database Results

 
0
  #3
Jan 14th, 2007
How about something like:
  1. select count(*)
  2. from students
  3. where total_raised > (select total_raised from students where uid=$uid)
This should tell you how many students have total_raised greater than the student you're looking at on your individual page... which should be equal to the ranking.

It should also handles ties just fine.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC