Welcome Guest, Not a member yet? Register   Sign In
change GET query string
#1

Hi,

I search page which gets filter using GET method. in some cases I can get  ?brand=&model=2   this kind of query where brand is empty. 
When I get this kind of get string I want remove 'brand'  from get field so it will be ?model=2 . 
In other words when user make query url ?brand=&model=2 as a response url I want to be it ?model=2

I dont know how to do it.

Thanks in advance
Reply
#2

(01-24-2017, 02:18 AM)neuron Wrote: Hi,

I search page which gets filter using GET method. in some cases I can get  ?brand=&model=2   this kind of query where brand is empty. 
When I get this kind of get string I want remove 'brand'  from get field so it will be ?model=2 . 
In other words when user make query url ?brand=&model=2 as a response url I want to be it ?model=2

I dont know how to do it.

Thanks in advance

if you get string url you have to extract it

ex
PHP Code:
$str=explode('&',$str); 



if get end url you should below

PHP Code:
end(explode('='$str)); 
Reply
#3

(This post was last modified: 01-24-2017, 03:29 AM by neuron.)

I did this part already, this is not what I am asking how to change url  www.example.com?brand=&model=a  to 
www.example.com?model=a  (this url shoould be shown in browser url address field)
Reply
#4

I'm not sure if this is what you want, but you can use codeigniter ability to parse uri
uri_to_assoc();
http://whatever.com/brand/foo/model/bar
(
        'brand' => 'foo',
        'model' => 'bar',
)

assoc_to_uri()
(
        'brand' => 'foo',
        'model' => 'bar',
)
http://whatever.com/brand/foo/model/bar
assoc_to_uri()
(
        'model' => 'bar',
)
http://whatever.com/model/bar

But if you really need exactly the query string, there could be 'http_build_query()' usefull for you.
When you build response, just create array of parameters and uset all 'empty' keys. Then 'http_build_query()' should create query without 'brand' if it is empty.
Reply
#5

PHP Code:
I'm not sure if this is what you want, but you can use codeigniter ability to parse uri
uri_to_assoc();
http://whatever.com/brand/foo/model/bar
(
        '
brand' => 'foo',
        '
model' => 'bar',
)

assoc_to_uri()
(
        '
brand' => 'foo',
        '
model' => 'bar',
)
http://whatever.com/brand/foo/model/bar
assoc_to_uri()
(
        '
model' => 'bar',
)
http://whatever.com/model/bar 
this is not what I need

Quote:[quote pid='340365' dateline='1485334057']
But if you really need exactly the query string, there could be 'http_build_query()' usefull for you. 
When you build response, just create array of parameters and uset all 'empty' keys. Then 'http_build_query()' should create query without 'brand' if it is empty.
this way I can build the url I want but it does not change the url that is shown in url field of browser
[/quote]
Reply
#6

It is possible that I'm stupidDodgy but I really do not understand what do you need.

"I search page which gets filter using GET method. in some cases I can get  ?brand=&model=2   this kind of query where brand is empty. When I get this kind of get string I want remove 'brand'  from get field so it will be ?model=2"

"this way I can build the url I want but it does not change the url that is shown in url field of browser"

Try do describe situation when such ulr string is created in your code. Is it when you send filled form with method GET? Or is it somehow generated by script? Why is empty parameter making trouble to you?
Reply
#7

When user submits filter form the url will be example.com/search?brand=&model=foo

and then I filter products according to these values in GET field, when filtering I discard that 'brand' because it doesnt have a value.

then I load same view again with filtered data. 

But in browser stays same example.com/search?brand=&model=foo 

I can delete brand from url  using javascript before submitting filter form. But I want to know if there is way to delete brand from url in php
I mean when send example.com/search?brand=&model=foo request the responded url will be example.com/search?model=foo

if I build url string using http_build_query() It wont change the url field in browser it example.com/search?brand=&model=foo
Reply
#8

If I understood you good, you have need to delete 'brand' segment because it's empty and it brings you to troubles BECAUSE it is empty.

But there must be a place where you handle with _GET array and there is nothing easier than unset all keys, where value is empty, before next process of these input data.

If you make this, you can simply ignore what is left in browser url address field and you don't need to modify it.
Reply
#9

Surely it all depends how you build the url to avoid empty parameters.
If it is a form why not use POST instead and avoid that ugly url.
And you deal with the POST data in your controller
A good decision is based on knowledge and not on numbers. - Plato

Reply
#10

With the same reason as Google, GET is faster than POST
and there is nothing to hide.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB