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
Old 06-30-2008, 03:46 PM Sort by.....
Skilled Talker

Posts: 60
Trades: 0
I want to have an order by date, name, email etc on a site ,but I don't even know where to start. When somebody goes to the results page I want them to be able to click on say name and then the results are displayed in order of names.
At the moment it's set up to automatically display by date added using:
Code:
$sql_order = " ORDER BY date DESC";
but I don't know how to change this so the user can select how they're ordered.

I've tried:
Code:
$sql_order = " ORDER BY '. $_GET['orderby'] . ' DESC";
Code:
$result = mysql_query ($sql_select . $sql_from . $sql_where . $sql_order . $sql_limit);
Code:
Order by: <a href='?orderby=name'>name</a> <a href='?orderby=email'>email</a>
but get an error saying:
Quote:
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/west/public_html/search.php on line 74
line 74 is:
Code:
$sql_order = " ORDER BY '. $_GET['orderby'] . ' DESC";
I'd be grateful if anybody could give me any pointers
Thanks
thehappyappy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-30-2008, 04:05 PM Re: Sort by.....
nyef's Avatar
Ultra Talker

Posts: 265
Name: Lucas
Trades: 0
Code:
$sql_order = " ORDER BY '. $_GET['orderby'] . ' DESC";
You started with a double quote ( " ), but then used single-quotes in the middle. It's confused because " ORDER BY ' isn't a valid string. You need to use all double quotes or all single quotes throughout.
__________________
~nyef

Please login or register to view this content. Registration is FREE
nyef is offline
Reply With Quote
View Public Profile Visit nyef's homepage!
 
Old 06-30-2008, 04:23 PM Re: Sort by.....
Skilled Talker

Posts: 60
Trades: 0
Thanks, I stopped getting that error and the page now loads. Although no results are shown at all. All it says is:
Quote:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/west/public_html/search.php on line 121

Warning: implode() [function.implode]: Argument to implode must be an array. in /home/west/public_html/search.php on line 121

Warning: mysql_error(): supplied argument is not a valid MySQL-Link resource in /home/west/public_html/search.php on line 128
Line 121 is:
PHP Code:
$results implode(mysql_fetch_assoc(mysql_query ("SELECT COUNT(*) " $sql_from $sql_where $sql_order))); 
and that is used to display 5 results at a time and line 128 is: the error that's shown if there was a problem with the query
PHP Code:
    echo("<p>Doh! Error performing query: " mysql_error($result) . "</p>"); 
The same thing happened whether I used single or double quotes although when you link on one of the 'order by' options the error disappears, and they work

Last edited by thehappyappy; 06-30-2008 at 04:31 PM..
thehappyappy is offline
Reply With Quote
View Public Profile
 
Old 06-30-2008, 04:34 PM Re: Sort by.....
Skilled Talker

Posts: 60
Trades: 0
Okay I moved
Code:
$sql_order = ' ORDER BY '. $_GET['orderby'] .' DESC';
to lower down the error's gone, but it then it stops working completely

Last edited by thehappyappy; 06-30-2008 at 04:42 PM..
thehappyappy is offline
Reply With Quote
View Public Profile
 
Old 07-01-2008, 05:00 PM Re: Sort by.....
coldwind's Avatar
Novice Talker

Posts: 12
Name: Konstantin Leboev
Trades: 0
Quote:
Originally Posted by thehappyappy View Post
Okay I moved
Code:
$sql_order = ' ORDER BY '. $_GET['orderby'] .' DESC';
to lower down the error's gone, but it then it stops working completely
There is SQL-injection: don't trust $_GET['orderby'].
(more about SQL-injections: http://www.webmaster-talk.com/php-fo...injection.html)

Can you show more php code?

Last edited by coldwind; 07-01-2008 at 05:12 PM..
coldwind is offline
Reply With Quote
View Public Profile
 
Old 07-02-2008, 05:01 AM Re: Sort by.....
Skilled Talker

Posts: 60
Trades: 0
I changed
Code:
$sql_order = ' ORDER BY '. $_GET['orderby'] .' DESC';
to
Code:
$orderby = $_GET['orderby'] ? $_GET['orderby'] : 'date';

$orderdir = ( isset ( $_GET['orderdir'] ) &&  $_GET['orderdir'] == 'ASC' ) ? 'ASC' : 'DESC'; 

$sql_order = " ORDER BY `" . $orderby . "` " . $orderdir;
and it now works fine. Thanks
thehappyappy is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Sort by.....
 

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.30744 seconds with 12 queries