Solved

Escaping strings pain in php

Posted on 2008-10-03
11
297 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses four methods for overlaying images in a container on a web page
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

803 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