Mysql sorting problem....

This is a discussion on "Mysql sorting problem...." within the Databases section. This forum, and the thread "Mysql sorting problem.... are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > Databases

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Jul 11th, 2005, 20:49
New Member
Join Date: Jun 2005
Location: Southampton
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Mysql sorting problem....

hello,

Having abit of trouble with a site im working on. Basically the site displays a load of products and i want to allow the customers to be able to view the products either alphabetically, by lowest price or by highest price. This is easy to do with the menu page that displays all the items but i want a previous and next option on each seperate product page.

The problem is that when there are two items with the same price, it gets caught in a loop when im arranging them by either higher or lowest price.

heres my current code:

Code: Select all
	switch ($sortby)
	{
	case "hprice":						
	 $sqlprev = "select * from deyron where dis != 1 and ppm91 > '$netinfo[ppm91]' and name != '$name'  order by ppm91 asc";
	 $sqlnext = "select * from deyron where dis != 1 and ppm91 < '$netinfo[ppm91]' and name != '$name'  order by ppm91 desc";
	break;
	
	case "lprice":
	 $sqlprev = "select * from deyron where dis != 1 and ppm91 < '$netinfo[ppm91]' and name != '$name' order by ppm91 desc";
	 $sqlnext = "select * from deyron where dis != 1 and ppm91 > '$netinfo[ppm91]' and name != '$name' order by ppm91 asc";
	 break;
	
         default:
	 $sqlprev = "select * from deyron where dis != 1 and name < '$netinfo[name]' and name != '$name'  order by name desc";
	 $sqlnext = "select * from deyron where dis != 1 and name > '$netinfo[name]' and name != '$name'  order by name asc";
	}
which will produce a query like this:
Code: Select all
select * from deyron where dis != 1 and ppm91 > '3.15' and name != 'Venus' order by ppm91 asc
= display the item 'melody'

and the next item like this:
Code: Select all
select * from deyron where dis != 1 and ppm91 > '3.15' and name != 'Melody' order by ppm91 asc
= display the item 'venus'

and then on the next page:
Code: Select all
select * from deyron where dis != 1 and ppm91 > '3.15' and name != 'Venus' order by ppm91 asc
= display the item 'melody'


now if there are 2 or more items that are 3.15 it will alternate between each of them on each page as the next highest / lowest item.

I thought of sorting the results alphabetically and then if the price is the same, search for the alphabetically higher/lower next item (depending on the direction) with the same price, if not, dont worry about sorting by name cos that will screw things up. Im not sure how to stick an IF statement in the middle of the query and if this can be done.

maybe i can stick some php in the middle of 2 queries but i wonder if there is an easier way?

cheers all.
Reply With Quote

  #2 (permalink)  
Old Jul 23rd, 2005, 14:02
Reputable Member
Join Date: Jul 2005
Location: Melksham, Wilts, UK
Posts: 293
Thanks: 0
Thanked 0 Times in 0 Posts
If you given several column names in your order clause, then MySQL will sort by the first column named ... but if that column contains two identical value when it's comparing two records, then it will move on and sort by the second named column, and so on. Thus

Code: Select all
order by ppm91 desc name desc
Did you know that you can leave out the "asc" for ascending, by the way
Reply With Quote
  #3 (permalink)  
Old Jul 26th, 2005, 09:08
New Member
Join Date: Jun 2005
Location: Southampton
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Ok Cheers, Il give it a try!
Reply With Quote
Reply

Tags
mysql, sorting, problem

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
PHP problem in Apache/PHP/MySQL JohnI PHP Forum 6 Aug 7th, 2008 10:26
Asp.net insert to mysql problem boyindie ASP.NET Forum 1 Aug 24th, 2007 02:00
mySQL - PHP (problem) - XML - Flash photofx PHP Forum 3 Jul 27th, 2007 17:43
PHP-MySQL problem robertboyle PHP Forum 4 Jun 16th, 2006 13:02
Mysql syntax problem... ktsirig Databases 1 Jan 6th, 2006 15:51


All times are GMT. The time now is 09:58.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs 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 43