mysql - PHP Paging with variables ugly URLS -


ok have large database querying

select * pricepaid postcode '$loc%'          order price desc      limit $start,$perpage  

i know how paging links work out total records found , page through them. using following code on next link :

<a href='http://".$_server['http_host'].$_server['request_uri']."&start=$next'>next</a> 

which works ok keeps ?loc variable start variable duplicates.

this how url looks on first page

 mysite/uk-property-prices.php?loc=l24   

then when click next page link first time url passes start variable , query displays page two

 mysite/uk-property-prices.php?loc=l24&start=1   

but when click next again

mysite/uk-property-prices.php?loc=l24&start=1&start=2 

now know works after while url gets stupidly long

mysite/uk-property-prices.php?loc=l24&start=1&start=2&start=3&start=4 

what doing wrong

$_server['request_uri'] contains query string well. you're doing appending more , more variables each time.

you need parse query string, replace value want replace, , re-build url. php has parsed in $_get (assuming don't need duplicate values). untested, try this:

$newqueryparts = $_get; $newqueryparts['loc'] = 3; $urlparts = parse_url($_server['request_uri']); echo 'http://', $_server['http_host'], $urlparts['path'], '?', http_build_query($newqueryparts); 

don't forget consider protocol well. site might use https in future. also, way interpolating variables directly in query implies might open sql injection attacks. use prepared/parameterized queries avoid problem.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -