ORDER BY via URL variable - why is this failing?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

ORDER BY via URL variable - why is this failing?

Post by simonmlewis »

Code: Select all

http://test.local/seller/807&simon=y
My code isn't collating this $simon variable.

Code: Select all

$simon= isset($_GET['simon']) ? $_GET['simon'] : null;
echo "$simon";

echo "
<a href='/seller/$id&simon=y'>test</a>";
Are you now allowed to put a & after a number in the URL? I'm trying to run an "order by" based on a $sorder variable. But it won't work. so I tried it with $simon to see if that is passed through, but it isn't. I don't understand why. It's not echoing "$simon".
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: ORDER BY via URL variable - why is this failing?

Post by Celauran »

simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: ORDER BY via URL variable - why is this failing?

Post by simonmlewis »

No that doesn't work either.
Changing the url to $id?simon=y, makes no difference.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: ORDER BY via URL variable - why is this failing?

Post by Celauran »

Sounds like the issue is with rewrite rules, then. What are those?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: ORDER BY via URL variable - why is this failing?

Post by simonmlewis »

This is the one that rewrites the seller products page:

Code: Select all

RewriteRule ^seller/([0-9]+) /index.php?page=productsseller&id=$1&menu=home [L]
And this is a category page - the 'sort' works on this one:

Code: Select all

RewriteRule ^categ/([0-9]+)/([^/]+) /index.php?page=categ&c=$1&cname=$2&menu=home [L]
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: ORDER BY via URL variable - why is this failing?

Post by Celauran »

Missing QSA?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: ORDER BY via URL variable - why is this failing?

Post by simonmlewis »

I've not seen that before.

If I do this:

Code: Select all

RewriteRule ^seller/([0-9]*)? /index.php?page=productsseller&id=$1&menu=home [QSA]
The page is not found.
But this

Code: Select all

RewriteRule ^seller/([0-9]+) /index.php?page=productsseller&id=$1&menu=home [QSA]
Page is found, but the issue I have raised, remains.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: ORDER BY via URL variable - why is this failing?

Post by simonmlewis »

Incidentally, if I do this:

Code: Select all

RewriteRule ^categ/([0-9]+)/([^/]+) /index.php?page=categ&c=$1&cname=$2&menu=home [L]
And put ... &order=title, on the end of the rewritten URL, it works!!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: ORDER BY via URL variable - why is this failing?

Post by Celauran »

And if you do this?

Code: Select all

RewriteRule ^seller/([0-9]*)? /index.php?page=productsseller&id=$1&menu=home [QSA, L]
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: ORDER BY via URL variable - why is this failing?

Post by simonmlewis »

Server error!

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there was an error in a CGI script.

If you think this is a server error, please contact the webmaster.
Error 500
site.local
Apache/2.4.4 (Win32) OpenSSL/1.0.1e PHP/5.5.1

Code: Select all

DirectoryIndex index.php index.html index.htm 
order allow,deny
allow from all 

Options +FollowSymLinks
Options +Indexes
RewriteEngine On

RewriteRule ^messages/([0-9]+) /index.php?page=messages&threadid=$1&menu=home [L]
RewriteRule ^seller/([0-9]*)? /index.php?page=productsseller&id=$1&menu=home [QSA, L]
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: ORDER BY via URL variable - why is this failing?

Post by Celauran »

Sorry, there shouldn't be a space between QSA and L.

Code: Select all

RewriteRule ^seller/([0-9]+)/? /index.php?page=productsseller&id=$1&menu=home [QSA,L]
works fine for me
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: ORDER BY via URL variable - why is this failing?

Post by simonmlewis »

Well I'll be damned, it works.
What is the difference between using & with [L] and using it in that other method?
Is it a newer method?

Odd that the other way works on my category page, but not this page.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: ORDER BY via URL variable - why is this failing?

Post by Celauran »

They're different rules. QSA is query string appended, ensuring things don't get "lost" in the rewrite. L is last, meaning stop processing rules.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: ORDER BY via URL variable - why is this failing?

Post by simonmlewis »

So how is my category script working, if that uses the same "order" values in the URL?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply