own URL[solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

own URL[solved]

Post by vigge89 »

I want to get the filename and querystring of the script running (to generate links and things like that), so i tried using this code:

Code: Select all

<?php
### set path info
$fself		= $_SERVER['PHP_SELF'];
$qstring	= $_SERVER['QUERY_STRING'];
if (empty ($qstring)) $fself = $self."?";
if (!empty ($qstring)) $fself = $self."?".$qstring."&";
?>
i then generate links with this for example:

Code: Select all

<?php
echo "<a href='{$fself}n={$rndnumber}'>link</a>";
?>
however, the querystring doesn't get deleted, so if i click the link 2 times or more, or if i go to another page, the querystring is still there. after some clicks, it could look like this:

Code: Select all

index.php?p=news&amp;n=2&amp;n=3&amp;n=5&amp;n=4&amp;n=1&amp;n=3&amp;n=2
so i'm wondering if there's someway else to do this?
i can't come up with anything...
Last edited by vigge89 on Sun May 23, 2004 2:43 pm, edited 1 time in total.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Just loop through $_GET adding the key and value seperated by "=" and "&".
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

you mean something like this?

Code: Select all

<?php

### set path info
$self      = $_SERVER['PHP_SELF'];

$qstring = "";
foreach ($_GET as $key => $value) {
    $qstring .= "$key=$value";
}

$fself = $self."?".$qstring;

?>
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Yeah, and if you still get duplicate values then just run $_GET though array_unique();
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

ok, i'll try it out after i'm finished with some C++ studying :wink:
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

aww, i have this:

Code: Select all

<?php
### set path info
$_GET = array_unique ($_GET);
$self = $_SERVER['PHP_SELF'];

$qstring = "";
foreach ($_GET as $key => $value) {
	$qstring .= "$key=$value&";
}

$fself = $self."?".$qstring;
?>
but there's still one duplicate :?

is there any better way of doing this?
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

got it too work with this:

Code: Select all

<?php
$self = $_SERVER['PHP_SELF'];

$qstring = "p=".$_GET['p'];
if ($qstring != "p=") $qstring .= "&";

$fself = $self."?".$qstring;
?>
Post Reply