/    Sign up×
Community /Pin to ProfileBookmark

How To echo $_SERVER[‘PHP_SELF’] on Links ?

Folks,

This pagination section works fine.
Note the numbered-page links.

““
if($page>$total_pages) //If Page Number is greater than Total Pages, show only a link to FINAL PAGE.
{
echo “<a href=”pagination.php?keywords=$keywords&limit=$limit&page=$total_pages”>”; echo “<b> Final Page </b>”;?></a><?php
}
else
{
$i = 1;
while($i != $total_pages)
{
if($i!=$page)
{
echo “<a href=”pagination.php?keywords=$keywords&limit=$limit&page=$i”>”; echo ” $i “;?></a><?php
}
else //Bold the Current Page Number.
{
echo “<a href=”pagination.php?keywords=$keywords&limit=$limit&page=$i”>”; echo “<b> $i </b>”;?></a><?php
}
$i++;
}
}
““

Now, I want to switch the file name to: $_SERVER[PHP_SELF’] as file names would change and I building a php template.
This following code attempt showing error:

**Parse error: syntax error, unexpected ” (T_ENCAPSED_AND_WHITESPACE), expecting ‘-‘ or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:xampphtdocstestpagination.php on line 250**

““
if($page>$total_pages) //Display Link to Final Page only.
{
echo “<a href=”$_SERVER[‘PHP_SELF’];?keywords=$keywords&limit=$limit&page=$total_pages”>$total_pages</a>”; THIS IS LINE 50 WHERE ERROR OCCURS
}
else
{
$i=1;
while($i<=$total_pages)//Display the Pagination Section.
{
if($i==$page) //Bold the ‘current page’ Link.
{
echo “<a href=”$_SERVER[‘PHP_SELF’];?keywords=$keywords&limit=$limit&page=$i”><b>$i</b></a>”;
}
else
{
echo “<a href=”$_SERVER[‘PHP_SELF’];?keywords=$keywords&limit=$limit&page=$i”>$i</a>”;
}
$i++;
}
}
““

to post a comment
PHP

22 Comments(s)

Copy linkTweet thisAlerts:
@developer_webauthorAug 24.2020 — Nope. This ain't working either:


if($page>$total_pages) //Display Link to Final Page only.
{
echo "<a href=" . "$_SERVER['PHP_SELF']" . "?keywords=$keywords&limit=$limit&page=$total_pages" . ">$total_pages</a>"; //Error on this line
}


Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:xampphtdocstestpagination.php on line 250
Copy linkTweet thisAlerts:
@developer_webauthorAug 24.2020 — Neither works this ....

if($page>$total_pages) //Display Link to Final Page only.
{
echo "<a href="$_SERVER["PHP_SELF"];?keywords=$keywords&limit=$limit&page=$total_pages">FINAL PAGE</a>";
}

NOTE: Note the dl quotes: ["PHP_SELF"].
Copy linkTweet thisAlerts:
@developer_webauthorAug 24.2020 — This fail too:

if($page>$total_pages) //Display Link to Final Page only.
{
echo "<a href="$_SERVER["PHP_SELF"];?keywords=$keywords&limit=$limit&page=$total_pages">FINAL PAGE</a>";
}

Parse error: syntax error, unexpected '$_SERVER' (T_VARIABLE), expecting ',' or ';' in C:xampphtdocstestpagination.php on line 250
Copy linkTweet thisAlerts:
@NachfolgerAug 24.2020 — > @developer_web#1622456 Parse error: syntax error, unexpected '$_SERVER' (T_VARIABLE), expecting ',' or ';' in C:xampphtdocstestpagination.php on line 250

That's because this isn't valid syntax. Look into string concatenation.
Copy linkTweet thisAlerts:
@NogDogAug 24.2020 — I find [sprintf()](https://php.net/sprintf) or [printf()](https://php.net/printf) can make this sort of thing easier to read/maintain, but that's just a stylistic preference. It also makes it cleaner if you want to apply functions to some of the values, e.g.:
<i>
</i>if($page &gt; $total_pages) //Display Link to Final Page only.
{
printf(
'&lt;a href="%s;?keywords=%s&amp;limit=%d&amp;page=%d"&gt;FINAL PAGE&lt;/a&gt;",
htmlentities($_SERVER['PHP_SELF']),
urlencode($keywords),
$limit,
$totoal_pages
);
}
Copy linkTweet thisAlerts:
@developer_webauthorAug 24.2020 — @NogDog#1622459

Your code shows this error:

**Parse error: syntax error, unexpected 'PHP_SELF' (T_STRING), expecting ',' or ')' in C:xampphtdocstestpagination.php on line 255**

I fixed it to this ..


if($page > $total_pages) //Display Link to Final Page only.
{
printf(
'<a href="%s;?keywords=%s&limit=%d&page=%d">FINAL PAGE</a>',
htmlentities($_SERVER['PHP_SELF']),
urlencode($keywords),
$limit,
$total_pages
);
}


Is it ok ?
Copy linkTweet thisAlerts:
@developer_webauthorAug 24.2020 — @NogDog#1622459

I tried fixing your code liike this following. I get no error.

However, pagination links do not get displayed like this:

12

(Meaning, links to page1 & page2).

Instead the link anchor texts display like this:

$i$i

Why is not the $i value getting displayed on screen ?


if($page>$total_pages) //Display Link to Final Page only.
{
printf('<a href="%s;?keywords=%s&limit=%d&page=%d">FINAL PAGE</a>',htmlentities($_SERVER['PHP_SELF']),urlencode($keywords),$limit,$total_pages);
}
else
{
$i=1;
while($i<=$total_pages)//Display the Pagination Section.
{
if($i==$page) //Bold the 'current page' Link.
{
printf('<a href="%s;?keywords=%s&limit=%d&page=%d"><b>$i</b></a>',htmlentities($_SERVER['PHP_SELF']),urlencode($keywords),$limit,$i);
}
else
{
printf('<a href="%s;?keywords=%s&limit=%d&page=%d">$i</a>',htmlentities($_SERVER['PHP_SELF']),urlencode($keywords),$limit,$i);
}
$i++;
}
}

UPDATE few mins later:

I am not used to printf and so I tried fixing your code like this just now ut no luck. Same result on screen.

if($page>$total_pages) //Display Link to Final Page only.
{
printf('<a href="%s?keywords=%s&limit=%d&page=%d">FINAL PAGE</a>',htmlentities($_SERVER['PHP_SELF']),urlencode($keywords),urlencode($limit),urlencode($total_pages));
}
else
{
$i=1;
while($i<=$total_pages)//Display the Pagination Section.
{
if($i==$page) //Bold the 'current page' Link.
{
printf('<a href="%s?keywords=%s&limit=%d&page=%d"><b>$i</b></a>',htmlentities($_SERVER['PHP_SELF']),urlencode($keywords),urlencode($limit),urlencode($i));
}
else
{
printf('<a href="%s?keywords=%s&limit=%d&page=%d">$i</a>',htmlentities($_SERVER['PHP_SELF']),urlencode($keywords),urlencode($limit),urlencode($i));
}
$i++;
}
}
Copy linkTweet thisAlerts:
@NogDogAug 24.2020 — > @developer_web#1622461 Your code shows this error:

Should be a single quote at the end of the preceding line.
Copy linkTweet thisAlerts:
@NogDogAug 24.2020 — > @developer_web#1622463 printf('&lt;a href="%s?keywords=%s&amp;limit=%d&amp;page=%d"&gt;&lt;b&gt;$i&lt;/b&gt;&lt;/a&gt;',htmlentities($_SERVER['PHP_SELF']),urlencode($keywords),urlencode($limit),urlencode($i));

The initial $i in the string to be output should be changed to a %d place-holder (which indicates you want integer Digits there (which also removes the need to urlencode it)).
Copy linkTweet thisAlerts:
@developer_webauthorAug 24.2020 — @NogDog#1622468

No luck. Still displaying like this:

$i$i


if($page>$total_pages) //Display Link to Final Page only.
{
printf('<a href="%s?keywords=%s&limit=%d&page=%d">FINAL PAGE</a>',htmlentities($_SERVER['PHP_SELF']),urlencode($keywords),$limit,$total_pages);
}
else
{
$i=1;
while($i<=$total_pages)//Display the Pagination Section.
{
if($i==$page) //Bold the 'current page' Link.
{
printf('<a href="%s?keywords=%s&limit=%d&page=%d"><b>$i</b></a>',htmlentities($_SERVER['PHP_SELF']),urlencode($keywords),$limit,$i);
}
else
{
printf('<a href="%s?keywords=%s&limit=%d&page=%d">$i</a>',htmlentities($_SERVER['PHP_SELF']),urlencode($keywords),$limit,$i);
}
$i++;
}
}


What is wrong NogDog ?

They link to correct urls, alright.

http://localhost/test/pagination.php?keywords=heman&limit=1&page=1

http://localhost/test/pagination.php?keywords=heman&limit=1&page=2
Copy linkTweet thisAlerts:
@developer_webauthorAug 24.2020 — NogDog,

I fixed it. Can you spot where your mistake was originally ?

The fix:

if($page>$total_pages) //Display Link to Final Page only.
{
printf("<a href="%s?keywords=%s&limit=%d&page=%d">FINAL PAGE</a>",htmlentities($_SERVER['PHP_SELF']),urlencode($keywords),$limit,$total_pages);
}
else
{
$i=1;
while($i<=$total_pages)//Display the Pagination Section.
{
if($i==$page) //Bold the 'current page' Link.
{
printf("<a href="%s?keywords=%s&limit=%d&page=%d"><b>$i</b></a>",htmlentities($_SERVER['PHP_SELF']),urlencode($keywords),$limit,$i);
}
else
{
printf("<a href="%s?keywords=%s&limit=%d&page=%d">$i</a>",htmlentities($_SERVER['PHP_SELF']),urlencode($keywords),$limit,$i);
}
$i++;
}
}
Copy linkTweet thisAlerts:
@NogDogAug 24.2020 — > @developer_web#1622471 printf("&lt;a href="%s?keywords=%s&amp;limit=%d&amp;page=%d"&gt;&lt;b&gt;$i&lt;/b&gt;&lt;/a&gt;",htmlentities($_SERVER['PHP_SELF']),urlencode($keywords),$limit,$i);

Not really a "fix", in that what you've done is use double-quotes around the string so that the $i gets interpolated directly within it, rather than using a %d place-holder to ensure it's an integer value and then injecting the value via the $i argument at the end of the function call. 🤷‍♂️
Copy linkTweet thisAlerts:
@developer_webauthorAug 24.2020 — @NogDog#1622478

Ok then. You expert. I leave it to you. I not know how to fix it to your asic standard as I still new to htmlspecialchars, htmlentities and urlencode.

I will wait for your fix.

Thanks.
Copy linkTweet thisAlerts:
@developer_webauthorAug 28.2020 — NogDog,

I guess you missed my last post. ;)
Copy linkTweet thisAlerts:
@developer_webauthorAug 31.2020 — @Nachfolger#1622458

I give-up. I tried this:

if($page>$total_pages) //Display Link to Final Page only.
{
printf("<a href="htmlentities($_SERVER['PHP_SELF'])?keywords=urlencode($keywords)&limit=urlencode($limit)&page=urlencode($total_pages)">FINAL PAGE</a>";
}


Get error:

@Nachfolger#1622458

I give-up. I tried this:

if($page>$total_pages) //Display Link to Final Page only.
{
echo"<a href="htmlentities($_SERVER['PHP_SELF'])?keywords=urlencode($keywords)&limit=urlencode($limit)&page=urlencode($total_pages)">FINAL PAGE</a>";
}


From NogDog's this:

if($page>$total_pages) //Display Link to Final Page only.
{
printf("<a href="%s?keywords=%s&limit=%d&page=%d">FINAL PAGE</a>",htmlentities($_SERVER['PHP_SELF']),urlencode($keywords),$limit,$total_pages);
}


Get error:

**Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:xampphtdocstestpagination.php on line 152**
Copy linkTweet thisAlerts:
@NachfolgerAug 31.2020 — @developer_web#1622830 If you would read my posts, you would know what you need to do to figure out the issue. You do not read anything I write to you, so why in the world are you asking for my help? I'm not going to waste time repeating myself. Find my posts, read them, and fix it yourself.
Copy linkTweet thisAlerts:
@developer_webauthorSep 01.2020 — @Nachfolger#1622835

I do read your posts. Which one in particular you refering to ? Let me see.
Copy linkTweet thisAlerts:
@developer_webauthorSep 07.2020 — @Nachfolger#1622835

Here is what I completed nearly half a week ago:


Echo Pagination Links - With Single Quotes:

if($page>$total_pages) //Display Link to Final Page only.
{
echo '<a href='' .htmlentities($_SERVER['PHP_SELF']) .'?keywords=' .urlencode($keywords) .'&limit=' .urlencode($limit) .'&page=' .urlencode($total_pages) ."'>FINAL PAGE</a>";
}
else
{
$i=1;
while($i<=$total_pages)//Display the Pagination Section.
{
if($i==$page) //Bold the 'current page' Link.
{
echo '<a href='' .htmlentities($_SERVER['PHP_SELF']) .'?keywords=' .urlencode($keywords) .'&limit=' .urlencode($limit) .'&page=' .urlencode($i) ."'><b>$i</b></a>";
}
else
{
echo '<a href='' .htmlentities($_SERVER['PHP_SELF']) .'?keywords=' .urlencode($keywords) .'&limit=' .urlencode($limit) .'&page=' .urlencode($i) ."'>$i</a>";
}
$i++;
}
}


Echo Pagination Links - With Double Quotes:

if($page>$total_pages) //Display FINAL PAGE link.
{
echo "<a href='" .htmlentities($_SERVER['PHP_SELF']) .'?keywords=' .urlencode($keywords) .'&limit=' .urlencode($limit) .'&page=' .urlencode($total_pages) ."'><b>Final Page</b></a>";
}
else
{
$i = 1;
while($i<=$total_pages)
{
if($i==$page) //Bold the 'Current page' Number.
{
echo "<a href='" .htmlentities($_SERVER['PHP_SELF']) .'?keywords=' .urlencode($keywords) .'&limit=' .urlencode($limit) .'&page=' .urlencode($i) ."'><b>$i</b></a>";
}
else
{
echo "<a href='" .htmlentities($_SERVER['PHP_SELF']) .'?keywords=' .urlencode($keywords) .'&limit=' .urlencode($limit) .'&page=' .urlencode($i) ."'>$i</a>";
}
$i++;
}
}


Printf Pagination Links - With Escaping:

if($page>$total_pages) //Display FINAL PAGE link.
{
printf("<a href="%s?keywords=%s&limit=%d&page=%d"><b>Final Page</b></a>",htmlentities($_SERVER['PHP_SELF']),urlencode($keywords),urlencode($limit),urlencode($total_pages));
}
else
{
$i = 1;
while($i<=$total_pages)
{
if($i==$page) //Bold the 'Current page' Number.
{
printf("<a href="%s?keywords=%s&limit=%d&page=%d"><b>$i</b></a>",htmlentities($_SERVER['PHP_SELF']),urlencode($keywords),urlencode($limit),urlencode($i));
}
else
{
printf("<a href="%s?keywords=%s&limit=%d&page=%d">$i</a>",htmlentities($_SERVER['PHP_SELF']),urlencode($keywords),urlencode($limit),urlencode($i));
}
$i++;
}
}


Printf Pagination Links - Without Escaping:

if($page>$total_pages) //Display FINAL PAGE link.
{
printf("<a href='%s?keywords=%s&limit=%d&page=%d'><b>Final Page</b></a>",htmlentities($_SERVER['PHP_SELF']),urlencode($keywords),urlencode($limit),urlencode($total_pages));
}
else
{
$i = 1;
while($i<=$total_pages)
{
if($i==$page) //Bold the 'Current page' Number.
{
printf("<a href='%s?keywords=%s&limit=%d&page=%d'><b>$i</b></a>",htmlentities($_SERVER['PHP_SELF']),urlencode($keywords),urlencode($limit),urlencode($i));
}
else
{
printf("<a href='%s?keywords=%s&limit=%d&page=%d'>$i</a>",htmlentities($_SERVER['PHP_SELF']),urlencode($keywords),urlencode($limit),urlencode($i));
}
$i++;
}
}
Copy linkTweet thisAlerts:
@developer_webauthorSep 07.2020 — @NogDog,

Close this thread as PROBLEM: SOLVED if you deem the codes in my above post are correct.

Ok ?

Thanks!
Copy linkTweet thisAlerts:
@ChodorSep 10.2020 — I have no idea if you found your answer or not.

I was a lurker until you had to bring me in this thing.

The solution is printf("<a href=?mega=" . $value . "&file=" . $file_path . ">whatever</a>");

That's how you handle string concatination, getting a string from a variable requires concatination.

Have a great one.
Copy linkTweet thisAlerts:
@developer_webauthorOct 04.2020 — @Chodor#1623180

Mmm. So, this not correct ?

if($page>$total_pages) //Display FINAL PAGE link.
{
echo "Page: <a href='" .htmlentities($_SERVER['PHP_SELF']) .'?col=' .urlencode($col) .'&keywords=' .urlencode($keywords) .'&limit=' .urlencode($limit) .'&page=' .urlencode($total_pages) ."'><b>Final Page</b></a>";
}
else
{
echo "Page: ";

$i = 1;
while($i<=$total_pages)
{
if($i==$page) //Bold the 'Current page' Number.
{
echo "<a href='" .htmlentities($_SERVER['PHP_SELF']) .'?col=' .urlencode($col) .'&keywords=' .urlencode($keywords) .'&limit=' .urlencode($limit) .'&page=' .urlencode($i) ."'><b>$i </b></a>";
}
else
{
echo "<a href='" .htmlentities($_SERVER['PHP_SELF']) .'?col=' .urlencode($col) .'&keywords=' .urlencode($keywords) .'&limit=' .urlencode($limit) .'&page=' .urlencode($i) ."'>$i </a>";
}
$i++;
}
}
Copy linkTweet thisAlerts:
@developer_webauthorDec 03.2020 — @criterion9

Care to chime in ?
×

Success!

Help @developer_web spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...