Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
php mysql sort question
Old 12-23-2009, 09:26 AM php mysql sort question
Sydpix's Avatar
Drinker I Smoke

Posts: 424
Name: Denny
Location: In a can...
Trades: 0
Is it possible to sort two rows with an if statement.

Sort by weight unless length is greater?

$query=" SELECT * FROM anglers ORDER BY Weight,Fish_Length +0 DESC ";

I need to sort Weight DESC but if the weights are the same, I need Fish_Length to take over.

Make sence?
__________________
.
Village Idiot

Sydpix is offline
Reply With Quote
View Public Profile Visit Sydpix's homepage!
 
 
Register now for full access!
Old 12-23-2009, 09:36 AM Re: php mysql sort question
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
I think this would work

"SELECT * FROM anglers ORDER BY Weight DESC, Fish_Length DESC"
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 12-23-2009, 09:45 AM Re: php mysql sort question
Sydpix's Avatar
Drinker I Smoke

Posts: 424
Name: Denny
Location: In a can...
Trades: 0
Yup... before I returned here I tried SELECT * FROM anglers ORDER BY Weight+0 DESC, Fish_Length+0 DESC "; and it worked fine.

Thanks!
__________________
.
Village Idiot

Sydpix is offline
Reply With Quote
View Public Profile Visit Sydpix's homepage!
 
Old 12-23-2009, 11:25 AM Re: php mysql sort question
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
I curious though, as to why you add a "+0" to the Weight and Fish_length columns(?). If it's some attempt to cast string values into numeric ones for correct ordering, it would be better to simply set those columns to a numeric type, rather than for example varchar.
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 12-24-2009, 03:01 AM Re: php mysql sort question
Sydpix's Avatar
Drinker I Smoke

Posts: 424
Name: Denny
Location: In a can...
Trades: 0
Well... I was having issues with decimals like 9.100 displaying before 10.100 when ORDER DESC. So +0 (I was told) corrects the issue because apparently it was reading it as a string therefore 1 is less than 9.
__________________
.
Village Idiot

Sydpix is offline
Reply With Quote
View Public Profile Visit Sydpix's homepage!
 
Old 12-24-2009, 10:19 AM Re: php mysql sort question
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Preserve us from programmers who think they are DBAs!!!!!


Just use a numeric data type for numeric data it's far easier in the long run!!
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-27-2009, 06:45 PM Re: php mysql sort question
Sydpix's Avatar
Drinker I Smoke

Posts: 424
Name: Denny
Location: In a can...
Trades: 0
Gotchya! Sheesh, so many opinions in the world of programming. An intermediate could get REALLY confused. LOL!

While we're on the topic, let me go off topic by asking the best way to insert and/or update a date record with dropdown selectors options?
__________________
.
Village Idiot

Sydpix is offline
Reply With Quote
View Public Profile Visit Sydpix's homepage!
 
Old 12-28-2009, 05:33 AM Re: php mysql sort question
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
MySQL handles the data types 'date' and 'timestamp', you should use them. For the dropdown selectors, you could have i.e. one for day, one for month, and one for year, then you put them together. When you insert a date to the database you should suply it as a string in the form of "YYYY-MM-DD", or in the case of timestamp "YYYY-MM-DD HH:MM".

Since they are represented as string you could of course use a varchar, but with the date and timestamp types MySQL will also sort them correctly when using 'ORDER BY'.
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.

Last edited by lizciz; 12-28-2009 at 05:35 AM..
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 12-28-2009, 06:53 AM Re: php mysql sort question
Sydpix's Avatar
Drinker I Smoke

Posts: 424
Name: Denny
Location: In a can...
Trades: 0
Would have to create separate fields for 00/00/0000? And would the field/fields be set to "date"?

I don't know why dates are throwing me off. I guess because it's three parts of data being stored to display only piece of information.

How would I combine the three? Use a UNION? Javascript?

Code:
print "Catch_Date: <select name=month >
<option>January
<option>February
<option>March
<option>April
<option>May
<option>June
<option>July
<option>August
<option>September
<option>October
<option>November
<option>December
</select>\n";
print "<select name=day>
<option>1<option>2<option>3
<option>4<option>5<option>6
<option>7<option>8<option>9
<option>10<option>11<option>12
<option>13<option>14<option>15
<option>16<option>17<option>18
<option>19<option>20<option>21
<option>22<option>23<option>24
<option>25<option>26<option>27
<option>28<option>29<option>30
<option>31
</select>\n";

print "<select name=day><option>2010<option>2011<option>2012
<option>2013<option>2014<option>2015
</select>< /br>\n";



The INSERT


$result=mysql_query("INSERT INTO anglers (month, day , year,) VALUES ('$_POST[month]','$_POST[day]','$_POST[year]')")or die("Insert Error: ".mysql_error());
mysql_close($link);
__________________
.
Village Idiot

Sydpix is offline
Reply With Quote
View Public Profile Visit Sydpix's homepage!
 
Old 12-28-2009, 09:58 AM Re: php mysql sort question
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
In the database there should be only one date field. How you choose to represent it on your site is up to you. Following your code example above, you will eventually end up with the day, month and year in variables, lets call them $day, $month and $year.

From there, just put them together as a string, as in
PHP Code:
$date $day "-" $month "-" $year;

//Then run your qurey,
$query "INSERT INTO table (datefield) VALUES('$date')"
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Reply     « Reply to php mysql sort question
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.32719 seconds with 12 queries