?
Solved

Preferred method of stripping out the end part of the URL using a pagination class

Posted on 2012-03-16
11
Medium Priority
?
301 Views
Last Modified: 2012-03-16
Attached is the pagination class script I'm using...

It works, except the URL that is produced is "vacancies_view.html?id=1&stageid=3&type=1&dis=4a&page=2" which is what is expected....

until you want to go back using the pagination navigation buttons eg: First,  << or the page number itself...as you get "vacancies_view.html?id=1&stageid=3&type=1&dis=4a&page=2&page=1"

note the "page=2&page=1" and it'll keep adding the page number if you use the buttons...

I'd like to be able to strip the "page=*" from the end...
$_SERVER['PHP_SELF']  doesn't give me the additional values needed to display the page, it just shows the "vacancies_view.html"
ps-pagination-be.php
0
Comment
Question by:m-higgins
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37729105
Instead of $_SERVER['PHP_SELF'] try $_SERVER['REQUEST_URI']
0
 

Author Comment

by:m-higgins
ID: 37729420
Thanks Ray - this sorted one issue...

but I still need to find a way to strip out the page=x from the URL and i'm sorted!

as I am still getting "page=2&page=1" is I navigate via the pagination output links...it should just change page=2 to page=1 - but I can't seem to get it to work?

If I had hair - I'd be pulling it out....!

I've been googling and see RegEx, preg_replace, str_replace amongst some others as possible solutions...but as you probably can tell - I'm trying to teaching myself PHP...

I have read *not* to use RegEx, as it's a bit of a resource hog...so thought I'd try str_replace..

My thinking is that I'd be better off looking for a pattern for the "page=x or y" at the top in function PS_Pagination_be()

Can you recommend a solution?
0
 
LVL 1

Expert Comment

by:ScottNL1
ID: 37729519
try this

//$url = $_SERVER['REQUEST_URI'];

$url = "http://www.someurl.com/vacancies_view.html?id=1&stageid=3&type=1&dis=4a&page=2&page=1";

$href = explode("&",$url);
$new_href= $href[0];unset($href[0]);
if(sizeof($href)){
  foreach($href as $k =>$v){
     if(stristr($v,"page=")===false){
       $new_href.= "&".$v;
     }
  }

}

echo $new_href;

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Accepted Solution

by:
gr8gonzo earned 2000 total points
ID: 37729535
You can use parse_str() to separate the parameters into an array:

// Parse the query string into an array of parameters
parse_str($_SERVER['QUERY_STRING'],$parameters);

// Change the "page" parameter to page 99
$parameters["page"] = 99;

// Convert the array of parameters back into the query string format
$query_string = http_build_query($parameters);
echo $query_string;
0
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 37729551
You can also use unset() to remove a parameter, so instead of changing the page to 99 (which was just for the sake of an example), you could:

unset($parameters["page"]);

...and then use http_build_query to rebuild the query string without the page parameter.
0
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 37729564
@ScottNL1 - Why go through all that manual work when PHP offers pre-built methods (parse_str and http_build_query) for doing all of it in a cleaner fashion?

http://www.php.net/manual/en/ref.url.php
http://www.php.net/manual/en/function.parse-str.php
0
 

Author Comment

by:m-higgins
ID: 37729591
The URL will be different depending on what item is clicked...so it will have different id's etc...

I'll see if I can get any closer with this though
0
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 37729592
And for the record, since you're learning PHP, don't be afraid of regular expressions. Yes, regexes will use more resources than most standard string functions, but sometimes they are the best answer.

If you're moving to a new house and it takes either 20 trips in a fast sports car or 1 trip in a big truck, it's probably better to take that big truck. The sports car will be overall slower and less efficient if it has to make 20 trips to do the same job. (The exception is if you're driving a Lambourghini, then I can understand taking 20 trips, because that's just an awesome car and any sane person would want to be in it as much as possible.)
0
 
LVL 1

Expert Comment

by:ScottNL1
ID: 37729594
Because most of the time i don't  know what i am talking about.
0
 

Author Closing Comment

by:m-higgins
ID: 37730765
You are the  gr8gonzo!

This helped me along with unsetting the parameter "page"
0
 

Author Comment

by:m-higgins
ID: 37730769
Thank you everyone for your time and your comments!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question