how to use this ?page=2

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Tassadduq
Forum Commoner
Posts: 60
Joined: Wed Dec 03, 2008 2:53 pm

how to use this ?page=2

Post by Tassadduq »

i have a picture gallery of ten pages.the names of the pages are as index.php , index2.php , index3.php so on....
every page has a Next and Previous button at the bottom.
when user click on next page it displays the next page like index2.php , index3.php
but
i have seen in some website that the picture gallery which contains more than one pages.
and when user click on next page button the url in the address bar shows like that

http://www.example.com/gallery/index.php?page=2
http://www.example.com/gallery/index.php?page=3
http://www.example.com/gallery/index.php?page=4
and so on....

i also want to use this function.
anyone know how to use this function?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: how to use this ?page=2

Post by califdon »

That's not a function, it's just a way to think about your project. Instead of having a separate script file for every page, assuming that the pages are quite similar, except for specific content, the idea is to just use one script that checks the URL (the $_GET server array variable) and then inserts appropriate content, depending on that variable.

The general approach is this:

Code: Select all

<html>
<head>
...
</head>
<body>
<?php
   if(!isset($_GET['page'])) {
      $page=1;
   } else {
      $page=$_GET['page'];
   }
...
   switch ($page) {
      case 1:
         echo "This is the content for page one...";
         ...
         break;
      case 2:
         echo "This is the content for page two...";
         ...
         break;
      case 3:
         echo "This is the content for page three...";
         ...
         break;
      case 4:
         echo "This is the content for page four...";
         ...
         break;
      ...
      default:
         echo "Invalid page number!";
   }
...
   $max = 10 // or whatever the highest valid page number may be
   $prev = $page <= 1 ? 1 : $page - 1;
   $next = $page>= $max ? $max : $page + 1;
   echo "<br /> <a href='".$_SERVER['PHP_SELF']."?page=$prev'> < Previous</a> -- ";
   echo "<a href='".$_SERVER['PHP_SELF']."?page=$next>Next > </a><br />";
...
?> 
With some words of caution about outputting anything before issuing a header(), another approach is to redirect the browser to a different script file, depending on the URL, but that's another story.
Last edited by califdon on Mon Dec 22, 2008 3:41 pm, edited 1 time in total.
Reason: Added the bit about redirection
User avatar
The_Anomaly
Forum Contributor
Posts: 196
Joined: Fri Aug 08, 2008 4:56 pm
Location: Tirana, Albania

Re: how to use this ?page=2

Post by The_Anomaly »

What califdon said is right, just my way of doing it is described here.
User avatar
Tassadduq
Forum Commoner
Posts: 60
Joined: Wed Dec 03, 2008 2:53 pm

Re: how to use this ?page=2

Post by Tassadduq »

hi califdon.
i simply copy your code and paste it in a php page and test it but it generates an error.

Parse error: syntax error, unexpected T_VARIABLE in C:\wamp\www\test.php on line 35

i think there is some missing in line no 35 here is code that i have pasted in the page.

Code: Select all

<html>
 <head>
 ...
 </head>
 <body>
 <?php
    if(!isset($_GET['page'])) {
       $page=1;
    } else {
       $page=$_GET['page'];
    }
    switch ($page) {
       case 1:
          echo "This is the content for page one...";
 
          break;
       case 2:
          echo "This is the content for page two...";
 
          break;
       case 3:
          echo "This is the content for page three...";
 
          break;
       case 4:
          echo "This is the content for page four...";
 
          break;
 
       default:
          echo "Invalid page number!";
    }
 
    $max = 10 // or whatever the highest valid page number may be
    $prev = $page <= 1 ? 1 : $page - 1;
    $next = $page>= $max ? $max : $page + 1;
    echo "<br /> <a href='".$_SERVER['PHP_SELF']."?page=$prev'> < Previous</a> -- ";
    echo "<a href='".$_SERVER['PHP_SELF']."?page=$next>Next > </a><br />";
 ?>
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: how to use this ?page=2

Post by Reviresco »

Add a semicolon:

Code: Select all

$max = 10; // or whatever the highest valid page number may be
User avatar
Tassadduq
Forum Commoner
Posts: 60
Joined: Wed Dec 03, 2008 2:53 pm

Re: how to use this ?page=2

Post by Tassadduq »

i have tried it it works but there is little bit problem.
i want to echo the index2.php whole page.
so it mean i will paste the whole code of the index2.php in the echo cores??
i have tried to display the image and it works very fine.
i have displayed the picture in page 2
the code is

Code: Select all

case 2:
          echo "[color=#FF0000]<img src='slides/1.jpg'>[/color]";
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: how to use this ?page=2

Post by califdon »

Reviresco wrote:Add a semicolon:

Code: Select all

$max = 10; // or whatever the highest valid page number may be
OOPS! Thanks, Reviresco. Sorry about that.
Tassadduq wrote:i have tried it it works but there is little bit problem.
i want to echo the index2.php whole page.
so it mean i will paste the whole code of the index2.php in the echo cores??
i have tried to display the image and it works very fine.
i have displayed the picture in page 2
the code is

Code: Select all

case 2:
          echo "[color=#FF0000]<img src='slides/1.jpg'>[/color]";
Basically, whatever you want to happen when "page 2" is selected, that's what you put in the appropriate Case block. If a lot of your page is the same for all pages, then you just do that part once, but wherever something needs to change for different pages, you put that in the Case block. There must be a thousand ways to do it, so I'm not going to write your code for you, but to give you the idea:

Code: Select all

<html>
<head>
...
</head>
<body>
  <div style='text-align:center; font-size:20pt; color:green; border:2pt solid green;'>
  Welcome to my Picture Gallery!
  </div>
...
<?php
   if(!isset($_GET['page'])) {
      $page=1;
   } else {
      $page=$_GET['page'];
   }
...
   switch ($page) {
      case 1:
         echo "This is the content for page one...";
         ...
         break;
      case 2:
         echo "This is the content for page two...";
         ...
         break;
      case 3:
         echo "This is the content for page three...";
         ...
         break;
      case 4:
         echo "This is the content for page four...";
         ...
         break;
      ...
      default:
         echo "Invalid page number!";
   }
...
   $max = 10 // or whatever the highest valid page number may be
   $prev = $page <= 1 ? 1 : $page - 1;
   $next = $page>= $max ? $max : $page + 1;
   echo "<br /> <a href='".$_SERVER['PHP_SELF']."?page=$prev'> < Previous</a> -- ";
   echo "<a href='".$_SERVER['PHP_SELF']."?page=$next>Next > </a><br />";
...
?>
  <div>Thanks for visiting my galleries! Come back again soon.</div>
</body>
</html>
So you don't have to put everything in the PHP block, just the parts that change. Hope that helps you.
User avatar
Tassadduq
Forum Commoner
Posts: 60
Joined: Wed Dec 03, 2008 2:53 pm

Re: how to use this ?page=2

Post by Tassadduq »

thank you soooooooo much califdon.
i have done it.
again bundle of thanks.
Post Reply