Jump to content

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


EricgTP313

Recommended Posts

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:
[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>
[/code]
Link to comment
Share on other sites

Change the column type for price so that it will be sorted correctly.
[code]
ALTER TABLE products CHANGE price price DECIMAL
[/code]
If you're storing the "$" symbol in the column you should stop doing so.
Link to comment
Share on other sites

[quote author=shoz link=topic=99897.msg393711#msg393711 date=1152391726]
Change the column type for price so that it will be sorted correctly.
[code]
ALTER TABLE products CHANGE price price DECIMAL
[/code]
If you're storing the "$" symbol in the column you should stop doing so.
[/quote]

awesome, that worked great, thanks!
and no, I'm not storing the $ in the column :)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.