General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old July 17th, 2003, 12:16 PM
zigote zigote is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Location: Atlanta GA
Posts: 73 zigote User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 55 m 11 sec
Reputation Power: 7
Sorting By Arrays?

I'm total new at Arrays, I have did my all night search, and nothing seemed to answer my question.

So, Here it goes.
How would I change ORDER BY in a query from a simple URL, for an option to sort by ?


PHP Code:
 $sort_by = array ( 
"1" => "id"
"2" => "date"
"3" => "title" 
); 


// dB Connection
$connection mysql_connect($hostTypeect...
// Query for info
$query "SELECT * FROM $tableName WHERE id = '$id' ORDER BY $sortBy "


My guess of how to call this from the URL?
I knew it would not work, but I tried this.
<a href="sample.php?$sortBy=1" title="Sort By: ID">ID</a>
<a href="sample.php?$sortBy=2" title="Sort By: Date">Date</a>
<a href="sample.php?$sortBy=3" title="Sort By: Title">Title</a>

As, You can see, I'm totally lost.

Reply With Quote
  #2  
Old July 17th, 2003, 12:40 PM
digitallysmooth digitallysmooth is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 788 digitallysmooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 21 sec
Reputation Power: 7
I just reorganized your code a little and cleaned up some things:
This should work. Make sure you have error_reporting set to E_ALL so you can catch any syntax errors.

Sort Links:
Code:
<a href="sample.php?s=1">ID</a> 
<a href="sample.php?s=2">Date</a> 
<a href="sample.php?s=3">Title</a>


Sort Code:
Code:
$defaultSortMethod = 'id';
$sort_by = array ( "1" => "id", "2" => "date", "3" => "title" ); 

$sortBy = isset($sort_by[$_GET['s']]) ? $sort_by[$_GET['s']] : $defaultSortMethod;

// dB Connection
$connection = mysql_connect($hostType, ect...
// Query for info
$query = "SELECT * FROM $tableName WHERE id = '$id' ORDER BY $sortBy ";
__________________
__________________________________________________ _
Wil Moore III, MCP | Integrations Specialist | Senior Consultant
Are You Listed...? | DigitallySmooth Inc.

Reply With Quote
  #3  
Old July 17th, 2003, 01:09 PM
zigote zigote is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Location: Atlanta GA
Posts: 73 zigote User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 55 m 11 sec
Reputation Power: 7
Well that was simple enough, Thank you so much.

Works Perfect!
Now I see what is going on.
Thanks Again laidBak!

Reply With Quote
  #4  
Old July 17th, 2003, 01:43 PM
digitallysmooth digitallysmooth is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 788 digitallysmooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 21 sec
Reputation Power: 7
No problem... just here to help.

Also note that you could have used a switch/case statement to accomplish the same thing.

Also, you might want to read up on the
$variable = true/false ? true : false
statements in the php manual

This is called the ternary conditional operator
REF: http://www.php.net/manual/en/language.expressions.php

Reply With Quote
  #5  
Old July 17th, 2003, 04:22 PM
zigote zigote is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Location: Atlanta GA
Posts: 73 zigote User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 55 m 11 sec
Reputation Power: 7
Very interesting, I will look more into that.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > Sorting By Arrays?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT