ddcphpf Posted May 7, 2014 Share Posted May 7, 2014 Hi, Please excuse my lack of pre-existing knowledge on the matter; this is some prepatory work I am doing before I speak with my PHP developer (I just know I'll get farther if I go in with some background knowledge) Here's the scenario: Using our website's search engine you can build all kinds of different URLs (we have many parameters) In some cases you can create the same set of parameters but the order of them can vary, producing the same content but with different URLs (not desired) What I'd like to do is recommend that we assign a placement hierarchy to the parameters so that we can be assured that if we're showing a particular set of results, the URL will always be the same, i.e. they will reorder themselves or "get in line" Is this totally out in left field? Or is it doable? I would also really appreciate any hints or examples that I can share with my developer to lead him in the right direction Thanks very much Quote Link to comment https://forums.phpfreaks.com/topic/288319-reordering-url-parameters-to-keep-them-consistent-using-php/ Share on other sites More sharing options...
BuildMyWeb Posted May 7, 2014 Share Posted May 7, 2014 i dont see why you cant do something like order them alphabetically. throw them into an array first and then sort: <?php sort( $array ); ?> http://uk.php.net/manual/en/function.sort.php once you have your items sorted, loop through the array to create your string. if you want to maintain the key/value relationship such as in an associative array, use asort() instead. Quote Link to comment https://forums.phpfreaks.com/topic/288319-reordering-url-parameters-to-keep-them-consistent-using-php/#findComment-1478623 Share on other sites More sharing options...
QuickOldCar Posted May 7, 2014 Share Posted May 7, 2014 name your parameters alphabetical or numerical order as to the order in want to display them ?1something=1&3something=3&2something=2 ?asomething=1&csomething=3&bsomething=2 then use something like this <?php if (isset($_REQUEST)) { ksort($_REQUEST); $build = array(); foreach ($_REQUEST as $key => $value) { $build[] = $key . "=" . $value; } $new_query = implode("&", $build); echo $new_url = filter_var("http://" . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'], FILTER_SANITIZE_STRING) . "?" . $new_query; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/288319-reordering-url-parameters-to-keep-them-consistent-using-php/#findComment-1478634 Share on other sites More sharing options...
mac_gyver Posted May 7, 2014 Share Posted May 7, 2014 after you sort your built up array of url parameters by their keys, php has a function that builds query strings from an array, it even handles urlencoding for you - http://us1.php.net/http_build_query Quote Link to comment https://forums.phpfreaks.com/topic/288319-reordering-url-parameters-to-keep-them-consistent-using-php/#findComment-1478639 Share on other sites More sharing options...
QuickOldCar Posted May 7, 2014 Share Posted May 7, 2014 (edited) Is other ways to go about this, like checking the key values and sorting in your own orders by key numbers, but have to check if they even exist, associate which ones to what order, can get messy. another option is to just send all the possible GET parameters back even if they are empty in the order you want can check if the $_REQUEST for each even exists and trim them as well,do any checking before place into query $new_url = filter_var("http://" . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'], FILTER_SANITIZE_STRING) . "?article=" . $_REQUEST['article']."&id=".$_REQUEST['id']."&page=".$_REQUEST['page']; Edited May 7, 2014 by QuickOldCar Quote Link to comment https://forums.phpfreaks.com/topic/288319-reordering-url-parameters-to-keep-them-consistent-using-php/#findComment-1478640 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.