Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extracting 1 $_GET value and leave the rest

Status
Not open for further replies.

tektipsismyfavorite

Technical User
May 4, 2006
57
US
I'm having the hardest time trying to do a "page view style switch" where the user can view a page 1 of 2 ways. Basically, if there is no "view" variable set in the URL, then the view is "normal", but if $_GET['view] == "list", then it's list view. The problem is switching them back and forth from normal to list. If I try extracting "view=list" from $_SERVER['QUERY_STRING'], it leaves the trailing question mark on the URL if there's nothing else left in the server string.

Basically what I'm trying to do is:
if(URL has "view=list" in it){
if(QUERYSTRING has other stuff in it){
file.php?(otherstuff -[minus] 'view=list');
} else{
file.php;
}
} else {
if(QUERYSTRING has other stuff in it){
file.php?otherstuff&view=list;
} else{
file.php?view=list
}
}

I'm just not sure how to keep my URL clean from extra '&'s and extra '?'s.
 
build your query string fresh each time

Code:
if (isset($_GET['view']) && $_GET['view'] == "list")
  $dolistview = true;
  unset ($_GET['view']);
else:
  $dolistview=false;
endif;
foreach ($_GET as $key=>$val):
 $qrystring="$key=$val&";
endforeach;
$qrystring=rtrim($val,"&")
at the end of this you have a variable($qrystring) that can be appended to a filename as you wish eg
Code:
echo "file.php?".$qrystring;
and you have a variable ($dolistview) that will allow you to control what display code you need to use.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top