Jump to content


Photo

Sorting by numbers..."123" is less than "99"


  • Please log in to reply
4 replies to this topic

#1 EricgTP313

EricgTP313
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 08 July 2006 - 08:30 PM

Hello,
I've written a very basic shopping cart script in php/mysql for a friend, and I want to be able to sort by price ascending and descending, but if there is a price that is, say $123, it is being sorted as less than, say, $99, because the 1 obviously IS less than the 9.

How would I go about making the script know that a 1 in a 3 digit string actually is greater than a 9 in a 2 digit string, and so on?
Heres my code:

<?php
$sort = $_GET['sort'];
$by = $_GET['by'];

if(!$sort){
$sort = title;
}
if(!$by){
$by = ASC;
}

$query2 = mysql_query("SELECT * FROM products WHERE type = 'sfx' ORDER BY $sort $by") or die(mysql_error());
?><div align="right">
<form name="form1" id="form1">Sort Items By: 
  <select name="menu1" onchange="MM_jumpMenu('parent',this,0)">
  	<option value="#">[Select One]</option>
    <option value="index.php?p=sfx&sort=price&by=DESC">Price (High - Low)</option>
	<option value="index.php?p=sfx&sort=price&by=ASC">Price (Low - High)</option>
	<option value="index.php?p=sfx&sort=title&by=ASC">Title (A - Z)</option>
	<option value="index.php?p=sfx&sort=title&by=DESC">Title (Z - A)</option>
	<option value="index.php?p=sfx&sort=type&by=ASC">Type (A - Z)</option>
	<option value="index.php?p=sfx&sort=type&by=DESC">Type (Z - A)</option>
  </select>
</form>
</div>


#2 Gast

Gast
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts
  • LocationSouth East England

Posted 08 July 2006 - 08:33 PM

Use this instead after echoing the results:

<?php
sort($array, SORT_NUMERIC);
?>

OS and Internet Specs:
Windows XP (with SP2), 1680x1050, PHP 4, MySQL 3, Internet Explorer 6 &7 /FireFox 2.0/Opera 9.2

Tutorials:
Simple Introduction to AJAX and XMLHttpRequest
Creating Live Data with AJAX Making a Google Suggest like application

#3 Kurt

Kurt
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts
  • LocationNew York

Posted 08 July 2006 - 08:38 PM

Also, shouldn't the values of the variables $sort and $by be in quotes? Unless you defined title and ASC as constants...

And your query should work fine.

#4 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 08 July 2006 - 08:48 PM

Change the column type for price so that it will be sorted correctly.
ALTER TABLE products CHANGE price price DECIMAL
If you're storing the "$" symbol in the column you should stop doing so.

#5 EricgTP313

EricgTP313
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 08 July 2006 - 09:44 PM

Change the column type for price so that it will be sorted correctly.

ALTER TABLE products CHANGE price price DECIMAL
If you're storing the "$" symbol in the column you should stop doing so.


awesome, that worked great, thanks!
and no, I'm not storing the $ in the column :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users