note 25378 deleted from function.parse-url by didou

From: Date: Fri, 30 Jan 2004 14:26:07 +0000
Subject: note 25378 deleted from function.parse-url by didou
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-64435@lists.php.net to get a copy of this message
Note Submitter: xmontero@dsitelecom.com ---- Hi. If you ever need to call a php script itself with modified parameters in the query, it may be useful this piece of code. Just imagine you write a "viewer" with 1000 results and you want to show "page=1" then provide links to "page=2", "page=3" and so on. If you request the URL and simply append the page at the end, you might result in duplicated values in the URI, like "table=customers&page=1&page=2". I use the modifies version of cox gluer, posted by anonimous an dmodified it a little bit: // The next function takes a query like "page=2" or "page=2&name=xavi" and uses it to override the self parameters function everything_self_uri( $new_query ) { global $REQUEST_URI; // Get the current URI used for the script $parsed=parse_url( $REQUEST_URI ); // Split into parts if( isset($parsed['scheme']) ) // Begin to rebuild it { $sep = (strtolower($parsed['scheme']) == 'mailto' ? ':' : '://'); $uri = $parsed['scheme'] . $sep; } else { $uri = ''; } if( isset($parsed['pass']) ) // Continue to rebuild it. { $uri .= "$parsed[user]:$parsed[pass]@"; } elseif( isset($parsed['user']) ) { $uri .= "$parsed[user]@"; } if ( isset( $parsed['host'] ) ) { $uri .= $parsed['host']; } // Continue to rebuild it. if ( isset( $parsed['port'] ) ) { $uri .= ":$parsed[port]"; } // Continue to rebuild it. if ( isset( $parsed['path'] ) ) { $uri .= $parsed['path']; } // Continue to rebuild it. if ( isset( $parsed['query'] ) ) { $uri .= "?" . everything_mix_query( $parsed[query], $new_query ); } // Continue to rebuild it. if ( isset( $parsed['fragment'] ) ) { $uri .= "#$parsed[fragment]"; } // End to rebuild it. return $uri; } // The next function is the mixer itself function everything_mix_query( $old_query, $new_query ) { // This function takes two queries and returns one single query. // The queries are for URLs. An example of query is: "name=michael&destination=London" // This function puts all the params of every query and if one param is repeated in the first and the second, the "new_query" remains, while // the "old_query" looses its value. $old_query_array = explode( "&", $old_query ); $new_query_array = explode( "&", $new_query ); $result_query_array = array(); // Empty a new array $old_count = count( $old_query_array ); // Count the old parameters for( $i = 0; $i < $old_count; $i++ ) // Add the old parameters { $current_item = $old_query_array[ $i ]; // Ex: Current item = "name=xavi" $pair = explode( "=", $current_item ); // Ex: Pair = {"name", "xavi"} $key = $pair[ 0 ]; // Ex: Key = "name" $result_query_array[ $key ] = $old_query_array[ $i ]; // Ex: $result[ "name" ] = "name=xavi" } $new_count = count( $new_query_array ); // Count the new parameters for( $i = 0; $i < $new_count; $i++ ) // Add the new parameters { $current_item = $new_query_array[ $i ]; // Ex: Current item = "name=smith" $pair = explode( "=", $current_item ); // Ex: Pair = {"name", "smith"} $key = $pair[ 0 ]; // Ex: Key = "name" $result_query_array[ $key ] = $new_query_array[ $i ]; // Ex: $result[ "name" ] = "name=smith" } $result_query = implode( "&", $result_query_array ); return $result_query; } // You can now call the whole thing like this: $URI = everything_self_uri( "page=3" ); echo( '<a href="' . $URI . '">Click here</a>' ); Hope to help. If you can copy/paste this code, I'm then happy ;-) See you. Xavier Montero.

« previous php.notes (#64435) next »