Highest Values

This is a discussion on "Highest Values" within the PHP Forum section. This forum, and the thread "Highest Values 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




Reply
 
LinkBack Thread Tools
  #1  
Old May 3rd, 2007, 22:00
Up'n'Coming Member
Join Date: Jan 2006
Location: NI
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Highest Values

Hi can anyone tell me how to display the top 3 fields from a MySQL database in order of highest number

I have a voting system like so:

id---total_votes---total_value
1 -------- 3 -------- 23
2 -------- 2 -------- 42
3 -------- 6 -------- 10
4 -------- 2 -------- 9
5 -------- 1 -------- 11


I want it to display the top 3 total_value fields which in this case would be:

ID 2, 3 and 5


Can anyone help?


Thanks
Robert
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old May 4th, 2007, 08:28
Reputable Member
Join Date: Jul 2005
Location: Melksham, Wilts, UK
Posts: 293
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Highest Values

The main MySQL command you need is

select total_value from whatevertablescalled order by total_value desc limit 3

I see you've posted this in the PHP forum - are you happy with the fucntion calls to conenct to the database and bring the result set back, or did you want guidance on that area as well?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old May 4th, 2007, 08:49
Up'n'Coming Member
Join Date: Jan 2006
Location: NI
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Highest Values

Hi thanks for your help.

No its ok i know how to connect to the database,


Can u give me the exact code to do this i have to get it done as soon as possible. Ive tried the code below but it just prints out the query in match.

Code: Select all
													
<?php
$server = "localhost";

$database = "kidzglob_polls";

$db_user = "";

$db_pass = "";

$table = "";

// connect to the mysql server 
$link = mysql_connect($server, $db_user, $db_pass) 
or die ("Could not connect to mysql because ".mysql_error()); 

// select the database
mysql_select_db($database) 
or die ("Could not select database because ".mysql_error()); 

$match = "select total_value from $table order by total_value desc limit 3";

 
echo ("$match");

?>

Much appreciated thanks
Robert

Last edited by robukni; May 4th, 2007 at 09:01.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old May 4th, 2007, 09:22
Reputable Member
Join Date: Jul 2005
Location: Melksham, Wilts, UK
Posts: 293
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Highest Values

Replace echo($match) with ...

$rs = mysql_query($match);
while ($row = mysql_fetch_assoc($rs)) {
echo ($row["total_value"]);
}


You need to run the query against the database, read it back row by row, and print out the result.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old May 4th, 2007, 09:27
Up'n'Coming Member
Join Date: Jan 2006
Location: NI
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Highest Values

Hi ok i have tried that and the code now looks like so:

Code: Select all
<?php
$server = "";

$database = "";


$db_user = "";


$db_pass = "";



$table = "";


// connect to the mysql server 
$link = mysql_connect($server, $db_user, $db_pass) 
or die ("Could not connect to mysql because ".mysql_error()); 

// select the database
mysql_select_db($database) 
or die ("Could not select database because ".mysql_error()); 

$rs = mysql_query($match);
while ($row = mysql_fetch_assoc($rs)) {
echo ($row["total_value"]);
} 


?>
It gives the following error:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/kidzglob/public_html/coverart/covers_content.php on line 126

Thanks
Robert
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old May 4th, 2007, 09:32
Up'n'Coming Member
Join Date: Jan 2006
Location: NI
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Highest Values

Its ok got it working

Thanks
Robert
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old May 4th, 2007, 11:33
Reputable Member
Join Date: Jul 2005
Location: Melksham, Wilts, UK
Posts: 293
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Highest Values

By adding back in the $match = line??
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
help

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
What can I use to make something update automatically (people with the highest scores imagehorizons PHP Forum 1 May 18th, 2008 16:16
Help please : Can't get the highest number to be recognised madmaxusm JavaScript Forum 0 May 28th, 2007 09:16
Setting Form Values to Previously Entered Values masonbarge PHP Forum 6 Oct 17th, 2006 17:36
determine which variable has the highest value... tehrobot Flash & Multimedia Forum 2 Oct 16th, 2006 02:33
Assign string values to integer values of a session variable Andy K Classic ASP 1 Jul 13th, 2005 08:29


All times are GMT. The time now is 04:26.


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