/    Sign up×
Community /Pin to ProfileBookmark

““
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++;
}
}
““

The above is what I got regarding printf-ing the url. Note the htmlentities and urlencode.
Now, let’s convert this to use the “echo” instead replacing the printf. How should it look now ?

to post a comment
PHP

24 Comments(s)

Copy linkTweet thisAlerts:
@NachfolgerAug 26.2020 — Why would you convert it from printf to echo when @NogDog just implemented this design approach? If your concern is that you don't understand printf, read the PHP docs on it. I assure you, it will not take the **average** person longer than 5 minutes to grasp the concept.

Should you decide to convert it back to echo, simply look at the old code that you posted two days ago and base it off of that.

You seem to think that this forum is for doing all of your work for you--**It isn't**.
Copy linkTweet thisAlerts:
@developer_webauthorAug 26.2020 — @Nachfolger#1622585

I understand printf. But it is long version of echo. Simple echo ""; is shorter and simpler.

My old "echo" version did not have htmlentities() and urlencode(). hence pestering this forum for a code sample so I can learn. Once someone like you gives me the correct code, I type and re-type till I memorise the format.

Now NogDog gave the printf version on how to print on screen via htmlentities and urlencode,

I now need to see the echo version how to display on screen/page via htmlentities and urlencode.

Then I will compare the 2 and see which one I can easily remember after memorising. That is all.

I easily memorise and then forget fast. Hence, need to memorise the easier version so that I don't forget that easy.

If you want me to attempt to memorise your code then by all means be my guest and do the printf conversion to echo "" version and then test me and see how well I remember it.
Copy linkTweet thisAlerts:
@NachfolgerAug 26.2020 — @developer_web#1622598 [I have already told you once the method you need to use in order to convert it to echo.](https://www.webdeveloper.com/d/391094-how-to-echo-serverphp-self-on-links/4)

Use your original code from [this thread](https://www.webdeveloper.com/d/391094-how-to-echo-serverphp-self-on-links) and concenate the functions (htmlentities, urlencode) with the echo statements.
Copy linkTweet thisAlerts:
@developer_webauthorAug 31.2020 — @Nachfolger#1622600

I tried. Look:

From this of NogDog's:

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);
}


to this of mine:


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>";
}


I 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#1622812

Change printf to echo and Google "PHP String Concatenation and Interpolation"
Copy linkTweet thisAlerts:
@developer_webauthorSep 02.2020 — @Nachfolger#1622816

I tried concatenation of string. Didn't work for me. So I just made this work. See if it is now injection proof or not ....


$self_page = htmlentities($_SERVER['PHP_SELF']);
$keywords=urlencode($keywords);

if($page>$total_pages) //Display Link to Final Page only.
{
echo "<a href="$self_page?keywords=$keywords&limit=$limit&page=$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="$self_page?keywords=$keywords&limit=$limit&page=$i"><b>$i</b></a>";
}
else
{
echo "<a href="$self_page?keywords=$keywords&limit=$limit&page=$i">$i</a>";
}
$i++;
}
}
Copy linkTweet thisAlerts:
@developer_webauthorSep 02.2020 — @Nachfolger#1622816

This didn't work for me ....


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>' ";
}


Atleast I attempted. Now show us your way so I can see the errors of my way.

Cheers!
Copy linkTweet thisAlerts:
@NachfolgerSep 02.2020 — @developer_web#1622880 Yes, it took you a week to try it, but you did. Thank you.

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

Corrected verson
<CODE lang="PHP">
`PHP<i>
</i>if ($page&gt;$total_pages) {
echo "&lt;a href=" . htmlentities($_SERVER['PHP_SELF']) . "?keywords=" . urlencode($keywords) . "&amp;limit=" . urlencode($limit) . "&amp;page=" . urlencode($total_pages) . "&gt;FINAL PAGE&lt;/a&gt; ";
}<i>
</i>
``


Obviously you did a miserable job at this, so I would encourage you to proceed with practicing.
Copy linkTweet thisAlerts:
@developer_webauthorSep 02.2020 — @Nachfolger#1622882

Thank you very much!

Now I have a few questions.

  • * Why some portions with dbl quotes and some not ? You did not quote the functions.

    Eg.

    * htmlentities($_SERVER['PHP_SELF'])



  • * Why escaping the 2nd dbl quote was not necessary since it is not the closing quote ?

    echo "<a href=" . htmlentities($_SERVER['PHP_SELF'])


  • Those are the only two differences of your's than mine.
    Copy linkTweet thisAlerts:
    @developer_webauthorSep 02.2020 — @Nachfolger#1622882

    Are you saying my code is totally wrong here ?

    https://www.webdeveloper.com/d/391132-printf-echo/7
    Copy linkTweet thisAlerts:
    @NogDogSep 02.2020 — @Nachfolger#1622882

    Will need some literal quotes in there:
    <i>
    </i>echo "&lt;a href='" . htmlentities($_SERVER['PHP_SELF']) . "?keywords=" . urlencode($keywords) . "&amp;limit=" . urlencode($limit) . "&amp;page=" . urlencode($total_pages) . "'&gt;FINAL PAGE&lt;/a&gt; ";

    (Which is one of the reasons I like printf() in such a situation, as it's hard to see those things in a complex concatenated string. YMMV)
    Copy linkTweet thisAlerts:
    @developer_webauthorSep 02.2020 — People,


    $full_name_concat = $first_name . " " . $last_name;
    //output: Jeff Everhart
    $full_name_interpolate = "{$first_name} {$last_name}";
    //output: Jeff Everhart
    $full_name_interpolate_v2 = "$first_name $last_name";


    look at 2nd option. Why use curly races unnecessarily ?

    If 3rd option works then what is the reason for concatenation in the first-place like in option 1 ?

    Got them from here:

    https://jeffreyeverhart.com/2019/11/21/string-interpolation-in-php/

    I would do like this at first:

    $full_name_interpolate_v2 = "$first_name $last_name";

    If that didn't work then like this afterwards:


    $full_name_concat = "$first_name" ." " ."$last_name";
    //output: Jeff Everhart

    If that didn't work then like this afterwards:


    $full_name_concat = $first_name ." " .$last_name;
    //output: Jeff Everhart

    Would never this:


    $full_name_interpolate = "{$first_name} {$last_name}";


    I gave you my rankings now you give me your's. Ok ?
    Copy linkTweet thisAlerts:
    @developer_webauthorSep 02.2020 — @NogDog#1622891

    So was my code wrong then ?


    $self_page = htmlentities($_SERVER['PHP_SELF']);
    $keywords=urlencode($keywords);

    if($page>$total_pages) //Display Link to Final Page only.
    {
    echo "<a href="$self_page?keywords=$keywords&limit=$limit&page=$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="$self_page?keywords=$keywords&limit=$limit&page=$i"><b>$i</b></a>";
    }
    else
    {
    echo "<a href="$self_page?keywords=$keywords&limit=$limit&page=$i">$i</a>";
    }
    $i++;
    }
    }

    What was wrong with it. I got to learn this answer.
    Copy linkTweet thisAlerts:
    @developer_webauthorSep 02.2020 — @NogDog#1622891

    Q1.

    In whatever string a variable was not needed, why didn't you write the strings with a sngl quote instead of dbl ? Something like so ...


    echo "<a href='" . htmlentities($_SERVER['PHP_SELF']) . '?keywords=' . urlencode($keywords) . '&limit=' . urlencode($limit) . '&page=' . urlencode($total_pages) . "'>FINAL PAGE</a> ";

    Any good reason ?

    Q2.

    Would not this work:

    echo "<a href='htmlentities($_SERVER['PHP_SELF']). '?keywords=' .urlencode($keywords) . '&limit=' .urlencode($limit) .'&page=' .urlencode($total_pages) .'>FINAL PAGE</a>' ";

    If not then why not ?

    This following was your latest. I derived the above from it but with sng quotes rather than your dbls.

    echo "<a href='" . htmlentities($_SERVER['PHP_SELF']) . "?keywords=" . urlencode($keywords) . "&limit=" . urlencode($limit) . "&page=" . urlencode($total_pages) . "'>FINAL PAGE</a> ";
    Copy linkTweet thisAlerts:
    @NachfolgerSep 02.2020 — @NogDog#1622891 Thanks for checking that. This is why I shouldn't answer threads in the morning. :
    Copy linkTweet thisAlerts:
    @developer_webauthorSep 02.2020 — @Nachfolger#1622898

    You're welcome.

    Here it is suppertime.
    Copy linkTweet thisAlerts:
    @developer_webauthorSep 02.2020 — @Nachfolger#1622898

    These 2 are not showing errors and so I guess they are right. I gave ranking of preference. I guess your ranking is the same. Right mate ?


    echo "<a href='" .htmlentities($_SERVER['PHP_SELF']) .'?keywords=' .urlencode($keywords) .'&limit=' .urlencode($limit) .'&page=' .urlencode($total_pages) ."'>FINAL PAGE</a>";



    echo '<a href=' ' .htmlentities($_SERVER['PHP_SELF']) .'?keywords=' .urlencode($keywords) .'&limit=' .urlencode($limit) .'&page=' .urlencode($total_pages) .''>FINAL PAGE</a>';


    Confirm and I get on my way closing this thread.

    UPDATE:

    Best to use the first option. Look:

    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++;
    }
    }

    Echoes pagination numbered links like this: 12

    Good.

    The 2nd option, using single quotes messes things up:

    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++;
    }
    }

    Echoes pagination numbered links like this: $i$i

    Not good.

    Sticking to the first option, NachFolger.
    Copy linkTweet thisAlerts:
    @developer_webauthorSep 02.2020 — @Nachfolger

    I still wanna know, was my code in here was wrong or right:

    https://www.webdeveloper.com/d/391132-printf-echo/14

    Read my last few posts too.
    Copy linkTweet thisAlerts:
    @developer_webauthorSep 02.2020 — Funny thing is, I was reading both NachFolger's and NogDog's latest posts and thinking they all are NachFolger's posts! Lol.
    Copy linkTweet thisAlerts:
    @developer_webauthorSep 02.2020 — Hey,

    A mixture of sngl & dl quotes working too. Look:

    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++;
    }
    }
    Copy linkTweet thisAlerts:
    @developer_webauthorSep 02.2020 — @NogDog,

    I am curious, how come you did not secure the $limit & $page ? Only secured $keywords with printf ?

    Your code:

    printf("<a href="%s?keywords=%s&limit=%d&page=%d">FINAL PAGE</a>",htmlentities($_SERVER['PHP_SELF']),urlencode($keywords),$limit,$total_pages);


    NachFolger secured also the $limit & $page with echo. I asked him to show me how to do it with echo over printf.

    Look:

    echo "<a href='" .htmlentities($_SERVER['PHP_SELF']) .'?keywords=' .urlencode($keywords) .'&limit=' .urlencode($limit) .'&page=' .urlencode($total_pages) ."'>FINAL PAGE</a>";


    I converted his "echo" ack to "printf" ut secured the $limit & $page too. Look:

    One example:

    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));


    Two example:

    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));


    What do you think NogDog ? Are my conversions fine or not ?

    Actually, the $limit and the $page are INT Values. Should we really urlencode() them ? or, should we use something else. Bear in mind they are part of url:

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

    Which translates to:

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

    I need your answer. have I done things safe enough (security side) ?
    Copy linkTweet thisAlerts:
    @developer_webauthorSep 02.2020 — @NachFolger,

    Don't forget to answer my last few posts before this thread gets closed.

    Thanks for helping me end this hell pursuit!
    Copy linkTweet thisAlerts:
    @NachfolgerSep 02.2020 — > @developer_web#1622908 NachFolger secured also the $limit & $page with echo. I asked him to show me how to do it with echo over printf.

    Just to clarify, I only copied your code directly and fixed the syntax issues.
    Copy linkTweet thisAlerts:
    @developer_webauthorSep 02.2020 — @NogDog

    I still wanna know, was my code in here was wrong or right:

    https://www.webdeveloper.com/d/391132-printf-echo/14
    ×

    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.26,
    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,
    )...