Solved

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

Posted on 2012-03-16
11
296 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This article will show, step by step, how to integrate R code into a R Sweave document
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

705 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