Solved

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

Posted on 2012-03-16
11
291 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 110

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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
LVL 35

Accepted Solution

by:
gr8gonzo earned 500 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses four methods for overlaying images in a container on a web page
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

735 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