Showing posts with label url. Show all posts
Showing posts with label url. Show all posts

Wednesday, August 1, 2012

Remove parameter from URL PHP

If you are looking to remove a specific parameter from the URL here is how to do it:

Assume you have URL
http://www.b4blinky.com/index.php?option=com_content&view=article&id=1
And you want to remove the id


$currURL = $_SERVER[QUERY_STRING];
//this will get you option=com_content&view=article&id=1
parse_str($currURL,$params); //Puts the url parameters in key=>value pair
//This will give you option=>com_content, view=>article, id=>1
unset($params[$fieldName]);  //Removes the parameter on which 'x' is clicked
//This will remove the $fieldName from the array e.g. 'id'
$newURL = http_build_query($params); //Rebuilds the query with the remaining parameters
//This will rebuild array excluding id
$newURL = 'index.php?'.$newURL;
//and finally your newURL like
index.php?option=com_content&view=article

That you can modify if relative doesn't work for you.

Hope it helps. Happy coding :)

Thursday, April 12, 2012

How to get a URL variable in Joomla

If you want to get a variable from URL the command is simply

$id = JRequest::getVar('id');
It will give you the value of 'id' if its in your url like:

http://www.b4blinky.com/index.php?id=1

However if you don't have an 'id' var in URL it will return you NULL, so if you want to have a default value of let's say zero you can use this


$id = JRequest::getVar('id','0');

Especially useful when you want to have some default views etc.