Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 306
  • Last Modified:

Escaping strings pain in php

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
Simon336697
Asked:
Simon336697
  • 4
  • 2
  • 2
  • +3
5 Solutions
 
hieloCommented:
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
 
darron_chapmanCommented:
<?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
 
fragopiCommented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Hube02Commented:
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
 
Hube02Commented:
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
 
Simon336697Author Commented:
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
 
Simon336697Author Commented:
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
 
hieloCommented:
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
 
zivperryCommented:
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
 
Simon336697Author Commented:
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
 
Simon336697Author Commented:
Thanks so much to all you champions.
0

Featured Post

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.

  • 4
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now