Rifts Posted December 4, 2010 Share Posted December 4, 2010 hey all how would I do this.. I'm guessing I need to go four different SELECT queries I have prices in my DB I need to select the highest price one echo it select the second highest price echo it select third highest echo it then select everything else and echo them I have this so far but i dont know how to get the second, third, ext highest. $result = mysql_query("SELECT * FROM stuff ORDER BY price DESC LIMIT 1"); while($row = mysql_fetch_array($result)) { echo $row['image'] . " " . $row['link']; echo "<br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/220666-selecting-second-third-highest/ Share on other sites More sharing options...
Zurev Posted December 4, 2010 Share Posted December 4, 2010 Right now you are ordering them by highest price first, followed by 2nd, third, fourth, etc. So what you're doing will accomplish your goal just fine, except you're adding LIMIT 1 so it's only selecting 1 row, happening to be the highest, just do limit 10 or so, and it will order them by highest price first. Quote Link to comment https://forums.phpfreaks.com/topic/220666-selecting-second-third-highest/#findComment-1142949 Share on other sites More sharing options...
Rifts Posted December 4, 2010 Author Share Posted December 4, 2010 No I can't do that this way I need them to have individual vars to I can use them later Quote Link to comment https://forums.phpfreaks.com/topic/220666-selecting-second-third-highest/#findComment-1142985 Share on other sites More sharing options...
jcbones Posted December 4, 2010 Share Posted December 4, 2010 Yes, you can. $result = mysql_query("SELECT * FROM stuff ORDER BY price DESC LIMIT 1"); while($row = mysql_fetch_array($result)) { echo $row['image'] . " " . $row['link']; echo "<br />"; $prices[] = $row['price']; } rsort($prices,SORT_NUMERIC); echo 'Highest price: $' . $prices[0] . '<br />'; echo 'Third Highest Price: $' . $prices[2] . '<br />'; Quote Link to comment https://forums.phpfreaks.com/topic/220666-selecting-second-third-highest/#findComment-1142987 Share on other sites More sharing options...
Rifts Posted December 4, 2010 Author Share Posted December 4, 2010 I love you Quote Link to comment https://forums.phpfreaks.com/topic/220666-selecting-second-third-highest/#findComment-1142996 Share on other sites More sharing options...
Rifts Posted December 4, 2010 Author Share Posted December 4, 2010 nvm worked it out thanks Quote Link to comment https://forums.phpfreaks.com/topic/220666-selecting-second-third-highest/#findComment-1142999 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.