Solved

Escaping strings pain in php

Posted on 2008-10-03
11
295 Views
Last Modified: 2010-04-21
Hi guys...
I wish there was an easier way to do this in php.
Guys im having issues with the following trying to get the right syntax..

I having issues in particular with the line:

<a href="<?php echo $_SERVER['PHP_SELF']?pageno=1; ?>">

Escaping strings..

Parse error: syntax error, unexpected '=

I wish this wasnt so tedious in php.......is there an easier way to do this
if ($pageno == 1) {

	echo " FIRST PREV ";

} else { ?>

	<a href="<?php echo $_SERVER['PHP_SELF']?pageno=1; ?>">FIRST</a> <?php

	$prevpage = $pageno-1; ?>

	<a href="<?php echo $_SERVER['PHP_SELF']}?pageno=$prevpage; ?>">PREV</a>"

}

Open in new window

0
Comment
Question by:Simon336697
  • 4
  • 2
  • 2
  • +3
11 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 22639029
if ($pageno == 1) {
      echo " FIRST PREV ";
} else {
      printf("FIRST", "$_SERVER['PHP_SELF']?pageno=1");
      $prevpage = $pageno-1;
      printf("PREV", "$_SERVER['PHP_SELF']}?pageno=$prevpage");
}
0
 
LVL 19

Assisted Solution

by:darron_chapman
darron_chapman earned 50 total points
ID: 22639046
<?PHP if ($pageno == 1) {
        echo " FIRST PREV ";
} else { ?>
        <a href="<?php echo "{$_SERVER['PHP_SELF']}?pageno=1"; ?>">FIRST</a> <?php
        $prevpage = $pageno-1; ?>
        <a href="<?php echo "{$_SERVER['PHP_SELF']}?pageno=$prevpage"; ?>">PREV</a>
<?PHP }
?>
0
 
LVL 1

Assisted Solution

by:fragopi
fragopi earned 100 total points
ID: 22639120
Hi Simon336697,

I am not sure if you are looking for another implementation of PHP pagination, but as far as the sintax issue, here is your working code with a few changes. Note that most of the issues are related to string concatenation:

<?php      
      if ($pageno == 1) {
        echo " FIRST PREV ";
} else { ?>
        <a href="<?php echo $_SERVER['PHP_SELF']."?pageno=1"; ?>">FIRST</a>
 <?php $prevpage = $pageno-1; ?>
        <a href="<?php echo $_SERVER['PHP_SELF']."?pageno=".$prevpage; ?>">PREV</a>
<?php }  ?>

Just make sure the beggining and end <?php ?> tags applies to where you past this in your code. Another alternative, similar to yours and  'Genius' post:

<?php
   if ($pageno == 1) {
         echo "FIRST PREV";
      } else {
         echo " <a href='{$_SERVER['PHP_SELF']}?pageno=1'>FIRST</a> ";
         $prevpage = $pageno-1;
         echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'> PREV</a> ";
      }
?>

 
0
 
LVL 18

Expert Comment

by:Hube02
ID: 22639394
From what I can see you simply need to move the query string you are attaching outside of the php tags or you need to concatenate them:
// move the query string

if ($pageno == 1) {

        echo " FIRST PREV ";

} else { ?>

        <a href="<?php echo $_SERVER['PHP_SELF'] ?>?pageno=1;">FIRST</a> <?php

        $prevpage = $pageno-1; ?>

        <a href="<?php echo $_SERVER['PHP_SELF']; ?>"?pageno=$prevpage>PREV</a>"

}
 
 

// Conatenation

if ($pageno == 1) {

        echo " FIRST PREV ";

} else { ?>

        <a href="<?php echo $_SERVER['PHP_SELF'].'?pageno=1'; ?>">FIRST</a> <?php

        $prevpage = $pageno-1; ?>

        <a href="<?php echo $_SERVER['PHP_SELF'].'?pageno=$prevpage'; ?>">PREV</a>"

}

Open in new window

0
 
LVL 18

Assisted Solution

by:Hube02
Hube02 earned 130 total points
ID: 22639430
It would be nice if we could edit posts here. I just noticed some other syntax errors that have to do with quote placement.
// move the query string

if ($pageno == 1) {

        echo " FIRST PREV ";

} else { ?>

        <a href="<?php echo $_SERVER['PHP_SELF'] ?>?pageno=1;">FIRST</a> <?php

        $prevpage = $pageno-1; ?>

        <a href="<?php echo $_SERVER['PHP_SELF']; ?>?pageno=$prevpage>PREV</a>

}

 

 

// Conatenation

if ($pageno == 1) {

        echo " FIRST PREV ";

} else { ?>

        <a href="<?php echo $_SERVER['PHP_SELF'].'?pageno=1'; ?>">FIRST</a> <?php

        $prevpage = $pageno-1; ?>

        <a href="<?php echo $_SERVER['PHP_SELF'].'?pageno=$prevpage'; ?>">PREV</a>

}

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:Simon336697
ID: 22641569
Hi guys, thanks so much for all your help...working through all your suggestions now...sorry about the delay.

Hielo..... i tried yours mate.. and got the following.....

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line xxx

where xxx is the following..

printf("FIRST", "$_SERVER['PHP_SELF']?pageno=1");

===================================== this is the code i used...
if ($pageno == 1) {
      echo " FIRST PREV ";
} else {
      printf("FIRST", "$_SERVER['PHP_SELF']?pageno=1");
      $prevpage = $pageno-1;
      printf("PREV", "$_SERVER['PHP_SELF']}?pageno=$prevpage");
}
======================================================== so i tried the following..

if ($pageno == 1) {
      echo " FIRST PREV ";
} else {
      printf("FIRST", "{$_SERVER['PHP_SELF']}?pageno=1");
      $prevpage = $pageno-1;
      printf("PREV", "{$_SERVER['PHP_SELF']}?pageno=$prevpage");
}

===============================================
The above does not error, but on view source there are no hyperlinks...for example..

The view source is just the following..

FIRST PREV
0
 
LVL 1

Author Comment

by:Simon336697
ID: 22641577
Hi daron, mate i tried yours...

================================================
<?PHP if ($pageno == 1) {
        echo " FIRST PREV ";
} else { ?>
        <a href="<?php echo "{$_SERVER['PHP_SELF']}?pageno=1"; ?>">FIRST</a> <?php
        $prevpage = $pageno-1; ?>
        <a href="<?php echo "{$_SERVER['PHP_SELF']}?pageno=$prevpage"; ?>">PREV</a>
<?PHP }
?>
==========================================================

This doesnt error either but gives me no links either (like hielos)....
This is meant to do pagination, so I should be able to click on FIRST PREV etc.
0
 
LVL 82

Accepted Solution

by:
hielo earned 150 total points
ID: 22641704
my apologies. I didn't realize that EE changed what I posted originally. This was the real original post:
if ($pageno == 1) {

      echo " FIRST PREV ";

} else {

      printf("<a href='%s'>FIRST</a>", "{$_SERVER['PHP_SELF']}?pageno=1");

      $prevpage = $pageno-1;

      printf("<a href='%s'>PREV</a>", "{$_SERVER['PHP_SELF']}?pageno={$prevpage}");

}

Open in new window

0
 
LVL 1

Assisted Solution

by:zivperry
zivperry earned 70 total points
ID: 22642323
And the elegant way...:)

<?php

if ($pageno == 1) {

     echo " FIRST PREV ";

} else { 

     $prevpage = $pageno-1;

     $url = rawurlencode( $_SERVER['PHP_SELF'] );

     

     echo "<a href=\"{$url}?pageno=1\">FIRST</a>",

          "<a href=\"{$url}?pageno={$prevpage}\">PREV</a>";

}

Open in new window

0
 
LVL 1

Author Comment

by:Simon336697
ID: 22642617
Hi darron_chapman, fragopi, Hube02, hielo and zivperry.
Guys thanks so much for your kind help really appreciated :>)

Guys with your help it is sorted....hielo your latest post i went with......ziv, fragopi and hube thanks so much for yours as well im sure they work too.

Ill split the points as best i can to all you kind people :>)


0
 
LVL 1

Author Closing Comment

by:Simon336697
ID: 31502960
Thanks so much to all you champions.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now