timcadieux Posted May 6, 2013 Share Posted May 6, 2013 so I built a simple grid that loads data based on params sent via the querystring. I'm using the same backend for both a Simple Search and Advanced Search. What I happend is a Search for Last Name and Firstname, this appears like ?lname=foo&fname=morefoo However, when my grid appears, I dsiplay ALL data related to that user on a row, ie Country, State, etc. What I want to be able to do have the CountryName hyperlinked with the params from the existing querystring and add the new params so that the querystring looks like ?lname=foo&fname=morefoo&countryId=65&stateId=129 My problem is that the Advanced search allows a user to query on these fields via dropdown, but the are Optional. Therefore I can often end up with ?lname=foo&fname=morefoo&countryId=&stateId= I need to be able to use a function that will allow me to replace an existing parm if it exists and/or append if it does not. Does that make sense. Something that would eventually look like the below echo '<td><a href=view-paginated.php?' . merge_querystring('countryID' , $row[13] ) . ' class=nodecoration>' . $row[13] . '</a></td>'; any help would be appeciated Link to comment https://forums.phpfreaks.com/topic/277717-need-some-help-using-the-querystring-to-allow-users-to-further-filter-data-in-a-grid/ Share on other sites More sharing options...
ignace Posted May 6, 2013 Share Posted May 6, 2013 function addOrReplaceParam($params, $value = null) { if ($value !== null) { $params = array($params => $value); } return http_build_query(array_merge($_GET, $params)); }You can use it like: <a href="foo.php?<?= addOrReplaceParam('foo', 'bar') ?>">foobar</a>Or like <a href="foo.php?<?= addOrReplaceParam(array('foo' => 'bar', 'bat' => 'baz')) ?>">more foobar</a> Link to comment https://forums.phpfreaks.com/topic/277717-need-some-help-using-the-querystring-to-allow-users-to-further-filter-data-in-a-grid/#findComment-1428663 Share on other sites More sharing options...
timcadieux Posted May 7, 2013 Author Share Posted May 7, 2013 Perfect, thank you! Link to comment https://forums.phpfreaks.com/topic/277717-need-some-help-using-the-querystring-to-allow-users-to-further-filter-data-in-a-grid/#findComment-1428873 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.