Link to home
Start Free TrialLog in
Avatar of nriddock
nriddockFlag for United States of America

asked on

["PHP_SELF"] syntax question?

I understand how the following code works...in that it displays the current filename:

filename.php from the URL as seen below:

ie: http://server.com/<b>filename.php</b>?var1=21&var2=Y&var3=90210


<?php
$currentPage = $HTTP_SERVER_VARS["PHP_SELF"];
?>


<a href="<?php echo printf($currentPage); ?>

what i'm looking to do is display the actual URL with all it's arguments and switches using a ["PHP_SELF"] type variable
ie: http://server.com/<b>filename.php?var1=21&var2=Y&var3=90210</b>

does this make sense? any suggestions?
Avatar of lozloz
lozloz

hi,

you want to use REQUEST_URI (so $HTTP_SERVER_VARS["REQUEST_URI"] before php 4.1.0 and $_SERVER["REQUEST_URI"]) after

loz
Avatar of nriddock

ASKER

that works great...one last question...as ive implemented this:


$currentPage = $_SERVER['REQUEST_URI'];

<a href="<?php echo printf($currentPage); ?>?maxRows_Recordset1=25">


it works as desired on the initial click...each subsequent click it appends the url and several clicks later the url looks like this:

http://www.server.com/filename.php?maxRows_Recordset1=25?maxRows_Recordset1=50?maxRows_Recordset1=25?maxRows_Recordset1=100

this is kinda defeats the purpose of the flexibility. do you have any suggestions how to "reset" the url string or limit the number of variables appended to the information returned by $currentPage = $_SERVER['REQUEST_URI'];

thanks
i had a feeling you were going to use it for something like this - personally i wouldn't use request_uri for this, i'd have $_SERVER["PHP_SELF"] as before and then loop through the arguments of $_GET to form your link

$link = $_SERVER["PHP_SELF"] . "?";
foreach($_GET as $key => $value) {
  $link .= "$key=$value&";
}
$link = substr($link, 0, -1);

then if you want you can expand and check for a variable that already exists:

$link = $_SERVER["PHP_SELF"] . "?";
foreach($_GET as $key => $value) {
  if($key != "maxRows_Recordset1") {
    $link .= "$key=$value&";
  }
}
$link = substr($link, 0, -1);

loz
so do i call /use the above code as follows?

<?php
$link = $_SERVER["PHP_SELF"] . "?";
foreach($_GET as $key => $value) {
  if($key != "maxRows_Recordset1") {
    $link .= "$key=$value&";
  }
}
$link = substr($link, 0, -1);
?>


<a href="<?php echo printf($link); ?>


??? Thanks
Almost, you should use the urlencode function, when writing up QUERY_STRINGS
should it look like this?

<a href="<?php echo printf($link), htmlentities(urlencode($userinput)), '" ; ?></a>

thanks for your help/patience.

-neils
no, it should be

<?php

$link = $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . "?";

$_GET['maxRows_Recordset1'] = 25;

foreach($_GET as $key => $val){
    $link .= "$key=" . urlencode($val);
}
$link = substr($link, 0, -1);

<a href="<?php echo $link; ?>">Click Here</a>



you forgot the &:

<?php

$link = $_SERVER['PHP_SELF'] . "?";

$_GET['maxRows_Recordset1'] = 25;

foreach($_GET as $key => $val){
    $link .= "$key=" . urlencode($val) . "&";
}
$link = substr($link, 0, -1);

<a href="<?php echo $link; ?>">Click Here</a>

cheers,

loz
just about there....it works great, with one last item.

i have the option of viewing  25 | 50 | 100 | 250 records per page. i notice the line:

$_GET['maxRows_Recordset1'] = 25;

how would i accomodate for the other record viewing options ie: 25, 50, 100, 250 ?

thanks
$_GET['maxRows_Recordset1'] = 25; shouldn't be in there - if you want to display that many rows from your mysql database, you need to limit according to the number in the url variable, e.g. $query = "SELECT * from table LIMIT 0," . $_GET["maxRows_Recordset1"];

loz
In that case If you have more than one link to do, I'd encapsulate it into a function.

function make_link($recordset_value);

  $link = $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . "?";

  $_GET['maxRows_Recordset1'] = $recordset_value;

  foreach($_GET as $key => $val){
      $link .= "$key=" . urlencode($val) . '&';
  }
  $link = substr($link, 0, -1);
 
  return $link;
}

For the link it would be then

<a href="<?php echo make_link(25); ?>">click here</a>

aolXFT

it's giving me a :
Parse error: parse error, expecting `'{'' in /var/www/html/php/local/filename.php on line 132

here is the code I've entered :
<?php
function make_link($recordset_value);{ ---------------------this is line 132------------------
  $link = $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . "?";
  $_GET['maxRows_Recordset1'] = $recordset_value;
  foreach($_GET as $key => $val){
      $link .= "$key=" . urlencode($val) . '&';
  }
  $link = substr($link, 0, -1);
  return $link;
}
?>
do you see where I would be missing a {
ASKER CERTIFIED SOLUTION
Avatar of lozloz
lozloz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ah sorry used a ; instead of a {

change it to function

function make_link($recordset_value){
everything is working great...

lozloz ill award you 500 pts here and
aolXFT ill post 500 pts for you in: Home|All Topics|Web Development|Web Languages|PHP
The post will be labled ptys for aolXFT


thanks for you help