Solved

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

Posted on 2012-03-16
11
251 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
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Instead of $_SERVER['PHP_SELF'] try $_SERVER['REQUEST_URI']
0
 

Author Comment

by:m-higgins
Comment Utility
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
Comment Utility
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
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 500 total points
Comment Utility
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 34

Expert Comment

by:gr8gonzo
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 34

Expert Comment

by:gr8gonzo
Comment Utility
@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
Comment Utility
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 34

Expert Comment

by:gr8gonzo
Comment Utility
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
Comment Utility
Because most of the time i don't  know what i am talking about.
0
 

Author Closing Comment

by:m-higgins
Comment Utility
You are the  gr8gonzo!

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

Author Comment

by:m-higgins
Comment Utility
Thank you everyone for your time and your comments!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now