Solved

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

Posted on 2012-03-16
11
267 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
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
 
LVL 34

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 34

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 34

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 34

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
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.
The viewer will learn how to count occurrences of each item in an array.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

895 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

14 Experts available now in Live!

Get 1:1 Help Now