Page 1 of 1
how to use this ?page=2
Posted: Mon Dec 22, 2008 2:45 pm
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?
Re: how to use this ?page=2
Posted: Mon Dec 22, 2008 3:38 pm
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.
Re: how to use this ?page=2
Posted: Mon Dec 22, 2008 3:58 pm
by The_Anomaly
What califdon said is right, just my way of doing it is described
here.
Re: how to use this ?page=2
Posted: Mon Dec 22, 2008 4:37 pm
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 />";
?>
Re: how to use this ?page=2
Posted: Mon Dec 22, 2008 5:26 pm
by Reviresco
Add a semicolon:
Code: Select all
$max = 10; // or whatever the highest valid page number may be
Re: how to use this ?page=2
Posted: Mon Dec 22, 2008 5:48 pm
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]";
Re: how to use this ?page=2
Posted: Mon Dec 22, 2008 7:08 pm
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.
Re: how to use this ?page=2
Posted: Mon Dec 22, 2008 7:13 pm
by Tassadduq
thank you soooooooo much califdon.
i have done it.
again bundle of thanks.