SunQuest
           PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old July 12th, 2002, 07:40 AM
Marcus26 Marcus26 is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 14 Marcus26 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Sorting results

I got some help a while ago retrieving data from some databases and then adding the data together.
At the moment I have 2 DB's. One contains people, the other contains their scores from competitions. The script I am writing finds each user and then finds all their scores and adds their scores together.
This works, however the result is sorted by username. I need to be able to take the results and sort by the total score for each person. I've been trying to make an associative array to store
name => score, name2 => score2
etc and then sort it, however it just doesn't work. It looks like it makes and array for each record.
Any ideas?

Reply With Quote
  #2  
Old July 12th, 2002, 08:16 AM
EvilivE EvilivE is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Milwaukee, WI USA
Posts: 291 EvilivE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via Yahoo to EvilivE
RE: Sorting results

Try using sum() when you run your query.

select sum(competition.score) as total from competition, people where competition.id = people.id order by total

OR

If you rather run two queries get use sum() from the the scores but try a GROUP BY or maybe DISTINCT.

That should get you going in right direction.

Reply With Quote
  #3  
Old July 12th, 2002, 08:32 AM
Marcus26 Marcus26 is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 14 Marcus26 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: Sorting results

sorry that doesn't make any sense to me (still learning php) here is my code


$ma = array();
while($row_ID = mysql_fetch_array($result_ID))
{
$score = $total+=$row_ID['score'];
$shooter = $row_Shooters['name'];
$ma [$shooter] = $score;
}
asort($ma);
foreach ($ma as $key => $val)
{
echo "<tr><td>";
echo "".$key."";
echo " ";
echo "".$val."</td>
</tr>";
}


I have it going into the array OK but it just doesn't sort tham at all. Any ideas?

Reply With Quote
  #4  
Old July 12th, 2002, 08:38 AM
D1NGO D1NGO is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Perth, Australia
Posts: 221 D1NGO User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: Sorting results

i think you would sort it when you get it from the database wouldnt you?

ie.
php Code:
Original - php Code
  1.  
  2. $sql = "SELECT SUM(points), username FROM table ORDER BY SUM(points)";


sorry if i misunderstood your post

Reply With Quote
  #5  
Old July 12th, 2002, 08:43 AM
Marcus26 Marcus26 is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 14 Marcus26 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: Sorting results

No here is the issue. Lets say in shooters DB I have Joe
In scores DB I have 5 scores Joe has shot, say
1200
1300
1250
1200

I do a search on all the shooters so it is displayed

Joe 4950

Then the next shooter hets the same, say their's is

Jill 5125

It displays in that order, how do I get the data from the DB's, add them together and then resort?

The site is here
http://comps.dva.asn.au/champsphp/index.php

thanks

Reply With Quote
  #6  
Old July 12th, 2002, 09:04 AM
D1NGO D1NGO is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Perth, Australia
Posts: 221 D1NGO User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: Sorting results

ok, this should work hopefully. Its presuming that there is a linking id in the shooters and scores table.

php Code:
Original - php Code
  1.  
  2. <?php
  3. //define mysql_connection settings
  4. $host ='host';
  5. $username = 'user';
  6. $pass = 'passwrod';
  7. $db = 'database';
  8.  
  9. mysql_connect($host, $username, $pass)
  10.     or die("Couldnt connect to database");
  11.     or die("Couldnt select database");
  12. $result = mysql_query("SELECT name, SUM(score) FROM shooters, scores WHERE shooters.id = scores.id GROUP BY shooters.name") //presuming you have a linking id between the two tables
  13.     or die(mysql_error());
  14. while($row = mysql_fetch_array($result))
  15. {
  16.     echo "{$row['name']} - {$row['SUM(score)']}";
  17. }
  18. ?>


it finds the name from the shooters table, and then adds all the scores with the same id...

Reply With Quote
  #7  
Old July 12th, 2002, 09:40 AM
Marcus26 Marcus26 is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 14 Marcus26 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: Sorting results

Yeah that's right the ID links the 2. That code you sent though just returns errors at line 2 or 3. I think I know what you are trying to do, however don't know enough to see what the result it. Been over it 10 times and can't see the code error. Sometimes the PHP error reporting is very frustrating.
Can you see the error? Thanks

Reply With Quote
  #8  
Old July 12th, 2002, 04:11 PM
xs0 xs0 is offline
Codewalkers Novice (500 - 999 posts)
 
Join Date: Apr 2007
Location: Ljubljana, Slovenia
Posts: 760 xs0 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: Sorting results

I can't see the error either.

If you copy/pasted the code, the problem might be that it uses &nbsp; for spaces, which causes problems with some browsers/OSes.

Otherwise, it would help if you posted what exactly the error is.

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > Sorting results


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway