["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?
nriddockAsked:
Who is Participating?
 
lozlozConnect With a Mentor Commented:
it should be

function make_link($recordset_value) {

loz
0
 
lozlozCommented:
hi,

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

loz
0
 
nriddockAuthor Commented:
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
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
lozlozCommented:
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
0
 
nriddockAuthor Commented:
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
0
 
aolXFTCommented:
Almost, you should use the urlencode function, when writing up QUERY_STRINGS
0
 
nriddockAuthor Commented:
should it look like this?

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

thanks for your help/patience.

-neils
0
 
aolXFTCommented:
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>



0
 
lozlozCommented:
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
0
 
nriddockAuthor Commented:
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
0
 
lozlozCommented:
$_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
0
 
aolXFTCommented:
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>

0
 
nriddockAuthor Commented:
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 {
0
 
aolXFTCommented:
Ah sorry used a ; instead of a {

change it to function

function make_link($recordset_value){
0
 
nriddockAuthor Commented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.