View Full Version : Using $_GET to get php page

03-27-2009, 12:46 AM
Was just wondering if someone could help me with this regex. Or in fact, there may be another thing i can use that i'm not aware of that's simpler than that - i dunno!

anyway, using multiple pages on my website with the same code used for alternating NEXT and PREV between pages etc. I'm looking to use $_GET to scan the url to find out which specific .php page is currently displaying, in order to copy it into the link file.



so i need it to detect "smiles.php" or just "smiles" in order for me to have code for next page as:

echo '<a href="/'.$page_name.'.php?page=2">Next</a>

The only reason i ask is that I want to essentially not have to alter the code for every single different type of page that this PREV/NEXT type of code will be used for on my website. If it can just call the name of the php file, define it as a variable like $page_name, would make life so easy.

Any thoughts?

Cheers! :D

03-27-2009, 12:51 AM
__FILE__ is a constant, but that is the relative file (as in, any included file will be the included file name).
What you want is $_SERVER['SCRIPT_NAME']. $_GET is usable only on querystring values (anything after the ? in the uri). Then, you can use parse_url to separate the parts you're wanting to use.

03-27-2009, 01:32 AM
worked perfectly... but i'd forgotten about the elements AFTER the ? that are specific to different pages (categories/genres etc.)

let's say i make it more difficult for you...

i want to grab everything from the url EXCEPT the "page=1" bit, including everything that comes after the '?', so it's like the whole url except the very last thing which is startrow


can this be done as easily or would this require an actual regex. if so, can you help me? many thanks in advance again. it's driving me nuts trying to figure it out

03-27-2009, 03:14 AM
The easiest way is by manipulating the $_GET variable. If its always the same element name (ie: startrow), you can simply run an unset($_GET['startrow']); and if you really want the querystring back, you can use $querystring = http_build_query($_GET);.
If its the last item in the list and always the last item, you can pop it off the _GET with array_pop($_GET); and then proceeed with the http_build_query or implosion of you're own.

03-29-2009, 03:56 PM
i'm a bit unsure as to what each command does exactly. can someone help me with the syntax on this? the startrow will always be at the end of the url.

Not sure whether to call the url, then seperate the startrow, change the value and implode it back together or not...