Solved

Escaping strings pain in php

Posted on 2008-10-03
11
299 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

751 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