Solved

Escaping strings pain in php

Posted on 2008-10-03
11
293 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses four methods for overlaying images in a container on a web page
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…
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…

743 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

11 Experts available now in Live!

Get 1:1 Help Now