Jump to content

previous/next buttons 50% working


spiritonline

Recommended Posts

Hi,

 

I'm really new to php so please forgive me my n00bness.

 

My teacher helped me with a bit of php code for a website of mine.

this code would make previous / next buttons function.

 

It works by a scandir that scans the dictionary with all the files in it.

all files are named index00.php index01.php etc.

   

The problem:

now this piece of code works 50% because

 

-going from page 1 to page 2 will work fine

-going back from page 2 to page one won't work.

-Neither will it go from 2 to three, instead it will just stay at it's current page. (page2)

 

Thanks a million in advance !

 

Cheers,

Jason.

 

The code

 

the site can be seen at: http://www.hotels-and-suites.com/demo/index00.php

 

<?

$pages= array(scandir("/mnt/web4/42/79/52006579/htdocs/demo"));
//echo "test" . count($pages)-2;

if (!$_GET["counter"]) {
$counter = $_GET["counter"];
}
else {
$counter = 0;
}
?> 

 

<a href="index0<? echo $counter+1; ?>.php?counter=<? echo $counter; ?>"><img src="../images/next_07.png" border="0" align="right""" /></a><br />
<a href="index0<? echo $counter-1; ?>.php?counter=<? echo $counter; ?>"><img src="../images/previous_05.png" border="0" align="left""" /></a></div

 

 

 

Link to comment
Share on other sites

1.) Are you using different pages for each link? That really defeats the entire object of using pagination.

 

2.) Your logic is wrong where you define counter. You have it as if $_GET['counter'] is false(roughly the same as not set) then set counter to it's value. Try:

 

if (isset($_GET["counter"])) {
   $counter = $_GET["counter"];
}
else {
   $counter = 0;
}

 

3.) You might like to check out this tutorial It's written using a database rather than for files, but the same principles still apply.

Link to comment
Share on other sites

Let me explain this piece of code:

if (!$_GET["counter"]) {
$counter = $_GET["counter"];
}
else {
$counter = 0;
}

 

If $counter is not found in the url, I should try to set $counter to the variable I couldn't find.

Else I should set $counter to 0...

 

Or in short: remove the ! ;)

 

What he said....

Link to comment
Share on other sites

Hi,

 

many thanks for your suggestions ! I will try them out and keep you updated :)

 

once more thanks ! I really appreciate it ;)

 

(PS: yes I use different pages for each time the previous or next button is pressed.)

 

EDIT:

 

Ok what i've done is the following:

 

I replaced

 

if (!$_GET["counter"]) {
$counter = $_GET["counter"];
}
else {
$counter = 0;
}

 

for

 

if (isset($_GET["counter"])) {
&#160; &#160; $counter = $_GET["counter"];
}
else {
&#160; &#160; $counter = 0;
}

 

When I now view the page it will give me the following error:

 

Parse error: syntax error, unexpected '&' in /mnt/web4/42/79/52006579/htdocs/demo/index00.php on line 14

On line 14 is: &#160; &#160; $counter = $_GET["counter"];

 

you could see for yourself at: http://www.hotels-and-suites.com/demo/index00.php

 

I hope some one can help me out :) thanks once more ! :D

 

 

 

Link to comment
Share on other sites

That's really not a particularly good way to go about managing your pages, but never mind.

 

In answer to the question, you need to be a little bit careful. Though it might be tempting to keep track of the numbers at the end from page-to-page, what happens if you make a mistake with your naming? How are you going to work out the highest number to go to? I think what i'd attempt to do is read the directory and look for all files that are in the form index*.php. You can then work out where in the array the current page is and whether or not to show next/previous links.

 

The only caveat with this is that it's only going to work if you've named all your files using two characters for the number. Otherwise they wont sort in a natural order. If you haven't then you'll need to use the natsort function to get a natural ordering. And even in this case, you'll need to be a little careful - it'll sort index02 before index1 so you'll need to rid yourself of the preceding zeros.

 

<?php
//create an array of all index pages
$pages = array();
foreach(glob('index*.php') as $page){
    $pages[] = $page;
}
//get the current page, but we only want the file name without any slashes
$currpage = $_SERVER['SCRIPT_NAME'];
$parts = explode('/',$currpage);
$currpage = $parts[count($parts)-1];

//find the index of the page
$index = array_search($currpage,$pages);

if($index > 0){//previous exists
    $prevpage = $pages[$index-1];
    //show previous link
    echo "Previous page:".$prevpage;
}
if($index < count($pages) -2){//next exists
    $nextpage = $pages[$index+1];
    //show next link
    echo "Next page:".$nextpage;
}
?>

Link to comment
Share on other sites

Hi there,

 

thanks a ton for your replies ! I'm sorry i didn't respond earlier but I saw your post only just now :(

 

In reply to what you've written, I've tried to put in your script but I still end up with the same errors.

( I also tried re-naming the files from 00,01, 02 etc to 0,1,2 etc)

Would you have any clues ? Once more thanks a billion !

 

(ps: you can reffrence to hotels-and-suites.com source)

 

thanks ! :)

Link to comment
Share on other sites

Sure, here's the whole page:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- TemplateBeginEditable name="doctitle" -->
<title>offerte voor: hotel naam hier</title>

<?php

$pages= array(scandir("/mnt/web4/42/79/52006579/htdocs/demo"));
//echo "test" . count($pages)-2;

if (@$_GET["counter"]) {
$counter = $_GET["counter"];
}
else {
$counter = 0;
}

//create an array of all index pages
$pages = array();
foreach(glob('index*.php') as $page){
    $pages[] = $page;
}
//get the current page, but we only want the file name without any slashes
$currpage = $_SERVER['SCRIPT_NAME'];
$parts = explode('/',$currpage);
$currpage = $parts[count($parts)-1];

//find the index of the page
$index = array_search($currpage,$pages);

if($index > 0){//previous exists
    $prevpage = $pages[$index-1];
    //show previous link
    echo "Previous page:".$prevpage;
}
if($index < count($pages) -2){//next exists
    $nextpage = $pages[$index+1];
    //show next link
    echo "Next page:".$nextpage;
}
?>

<!-- TemplateEndEditable -->
<!-- TemplateBeginEditable name="head" -->
<!-- TemplateEndEditable -->
<style type="text/css">
<!--
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
background-image: url(../images/background-gradient_02.png);
background-repeat: repeat-x;
}
body,td,th {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
color: #333;
}
#menu {
margin: auto;
height: 81px;
width: auto;
background-image: url(../images/header_02.png);
padding-top: 30px;
}
#wrapper {
width: 1024px;
margin: auto;
}
#footer {
background-image: url(../images/footer_03.png);
height: 93px;
}
#header {
background-image: url(../images/header_01.png);
height: 125px;
}
#content {
background-image: url(../images/images/content00_02.png);
height: 582px;
}
#tekst {
height: 60px;
width: 200px;
margin-top: 103px;
margin-right: auto;
margin-bottom: auto;
margin-left: auto;
text-align: center;
font-size: 16px;
}
a:link {
text-decoration: none;
}
a:visited {
text-decoration: none;
}
a:hover {
text-decoration: none;
}
a:active {
text-decoration: none;
}
-->
</style>
</head>

<body>
<div id="menu">
<a href="index0<? echo $counter+1; ?>.php?counter=<? echo $counter; ?>"><img src="../images/next_07.png" border="0" align="right""" /></a><br />
<a href="index0<? echo $counter-1; ?>.php?counter=<? echo $counter; ?>"><img src="../images/previous_05.png" border="0" align="left""" /></a><br />
</div>
<div id="wrapper">
  <div id="header"></div>
  <div id="content">      
    <div id="tekst"><br />
      Naam hotel hier<br />
    </div>
  </div>
  <div id="footer"></div>
</div>
</body>
</html>

Link to comment
Share on other sites

Or just the PHP code:

 

<?php

$pages= array(scandir("/mnt/web4/42/79/52006579/htdocs/demo"));
//echo "test" . count($pages)-2;

if (@$_GET["counter"]) {
$counter = $_GET["counter"];
}
else {
$counter = 0;
}

//create an array of all index pages
$pages = array();
foreach(glob('index*.php') as $page){
    $pages[] = $page;
}
//get the current page, but we only want the file name without any slashes
$currpage = $_SERVER['SCRIPT_NAME'];
$parts = explode('/',$currpage);
$currpage = $parts[count($parts)-1];

//find the index of the page
$index = array_search($currpage,$pages);

if($index > 0){//previous exists
    $prevpage = $pages[$index-1];
    //show previous link
    echo "Previous page:".$prevpage;
}
if($index < count($pages) -2){//next exists
    $nextpage = $pages[$index+1];
    //show next link
    echo "Next page:".$nextpage;
}
?>

 

<a href="index0<? echo $counter+1; ?>.php?counter=<? echo $counter; ?>"><img src="../images/next_07.png" border="0" align="right""" /></a><br />
<a href="index0<? echo $counter-1; ?>.php?counter=<? echo $counter; ?>"><img src="../images/previous_05.png" border="0" align="left""" /></a><br />

 

(PS: how come I can't edit my posts anymore after a while ?)

Link to comment
Share on other sites

I think you missed the point of the code i posted slightly. You're use of variables passed in the URL are redundant, seeing as you're actually redirecting to separate pages. The next/previous text that my code outputs are the pages you need to link to. If you look at your page, you'll see text at the top "Next page:index01.php " - that's what you need to link to.

 

(PS: how come I can't edit my posts anymore after a while ?)

 

There's a time limit on the edit in order to prevent an otherwise inevitable complete communication breakdown.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.