Forum Moderators: coopster

Message Too Old, No Replies

"Next page" / "Previous page"

Can it be done automatically without a database?

         

anax

12:13 am on Jan 4, 2006 (gmt 0)

10+ Year Member



I'm teaching myself some very elementary PHP - just simple includes with some "thispage" and "you are here" variables to keep a navigation menu consistent across pages. It's a simple static site with no databases involved.

One thing I'd really like to be able to automate is the Next-page/Previous-page links on every page. The HTML files themselves have unpredictable names (2005-03-green-widgets, 2005-07-blue-widgets, etc.).

Is there some way of using PHP (without a database) to read or point to the next/previous file in a directory, regardless of the file's name? I'm a complete beginner, so any information would be welcome.

mm1220

12:34 am on Jan 4, 2006 (gmt 0)

10+ Year Member



sessions baby, sessions!

twist

12:40 am on Jan 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It could be done. You could use PHP to read the file names in the directory and then sort them in whatever manner you see fit.

PHP and databases go hand in hand. Just take the plunge and create a database. If your nervous about doing it directly just use a program like phpmyadmin. It will help you create and fill a database and anytime you manipulate the database using phpmyadmin it shows you how they did it. Great way to learn php/mysql.

anax

3:08 am on Jan 4, 2006 (gmt 0)

10+ Year Member



You could use PHP to read the file names in the directory

What's the general syntax for this? That's really all I need: read the list and tell me which one is next and which is previous. If I know the name of the command/function to look up, I might be able to see if it would work for me.

Many thanks.

twist

3:23 am on Jan 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have never done this before so I don't really know a lot about it. This might be a good place to start though,

[us3.php.net...]

anax

8:56 am on Jan 5, 2006 (gmt 0)

10+ Year Member



Many thanks for that reference. It's still pretty incomprehensible to me, but it does look like if there's an answer, it's in the general vicinity of that page somewhere.

twist

11:50 pm on Jan 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I hope this isn't a homework assignment, because I don't think we are supposed to help people do their homework, but either way,

Create a folder called "test"

Stick the following four files inside,

a.php
b.php
c.php
d.php

Now put the following code on each of the four files above after you add the correct path information,


<?php
// Variables
$current_page = basename( $_SERVER['PHP_SELF'] );
$previous_page = '';
$next_page = '';
$begin = 1;

// Scan files in the current directory and store in array
$directory = opendir( "/path/test/" );
while( false!== ( $filename = readdir( $directory ) ) ) {
if( preg_match( '/php/', $filename ) ) {
$files[] = $filename;
}
}

// Count array and minus one because array starts at zero
$count = count( $files );
$end = $count - 1;

// If current page is beginning of array
if( $current_page == $files[0] ) {
$previous_page = $files[ $count - 1 ];
$next_page = $files[1];
}
// Else if current page is end of array
else if( $current_page == $files[ $end ] ) {
$previous_page = $files[ $end - 1 ];
$next_page = $files[0];
}
// Else current page is in middle of array
else {
while( $begin < $end ) {
if( $current_page == $files[ $begin ] ) {
$previous_page = $files[ $begin - 1 ];
$current_page = $files[ $begin ];
$next_page = $files[ $begin + 1 ];
}
$begin++;
}
}

echo'
<html><body>
<a href="'. $previous_page .'">Go to previous page</a><br />
Current page - '. $current_page . '<br />
<a href="'. $next_page .'">Go to next page</a>
</body></html>
';
?>

anax

11:39 pm on Jan 6, 2006 (gmt 0)

10+ Year Member



Wow, thanks *very* much. Never could have gotten anywhere near that in a million years.

No, not a homework assignment. :) I'd probably be better of if it were, since that would mean I had someone teaching me this stuff in a systematic manner.

I've tried it with the test files as you set it up, and it works perfectly. Now I'm trying to include the php directly in my existing HTML files. It returns the $current_page variable correctly, but the previous and next links just show the base domain URL (http://mysite.net) with no specific page identified. Now working on understanding what causes that problem.

anax

12:25 am on Jan 7, 2006 (gmt 0)

10+ Year Member



OK, I'm getting there. :) My problem originates in this line I think:

if( preg_match( '/php/', $filename ) ) {

Am I correct that the purpose of this line is to check whether each item returned from the directory list is in fact a file name rather than, say, the name of a directory or a cgi script or something else? And that this check is based on the assumption that the string "php" occurs in and only in filenames (such as a.php, b.php, etc.)?

So, if my files end in .html rather than .php, I could substitute the following for the code above:

if( preg_match( '/html/', $filename ) ) {

I'm testing that now and it seems to work in one setting. I have another setting in which I've eliminated file extensions from my URLs and it isn't working there. Perhaps in that case I need to find a different kind of test for file-ness?

twist

2:43 am on Jan 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if( preg_match( '/html/', $filename ) ) {

I'm testing that now and it seems to work in one setting. I have another setting in which I've eliminated file extensions from my URLs and it isn't working there. Perhaps in that case I need to find a different kind of test for file-ness?

The preg_match function uses regular expressions [etext.lib.virginia.edu]. I was getting a list of filenames and only grabbing the ones with the letters "php" in them because it lists the directory "." and ".." as part of the files. You could simply grab all files that aren't "." or ".." by ignoring them like so,

if ( $file!= ".." && $file!= "." ) {

The above will grab all files in the directory excluding "." and ".."

If you want to search for multiple extensions you would use,

if( preg_match( '/(html¦php¦asp)/', $filename ) ) {

* change broken pipes "¦" to solid bars

anax

4:39 am on Jan 7, 2006 (gmt 0)

10+ Year Member



You know, if you want to be a minor celebrity you could write up a generalized version of these instructions to be published on "A List Apart" - I got started by cutting and pasting from their article on "keeping navigation current" [oops, WebmasterWorld won't let me link to it - you can find it with a search]. This has been a very highly cited article that got a lot of attention. A similar "next/previous" article would probably be very popular.

I've now got your method working perfectly on one group of pages - many thanks indeed!

I'm still chopping away at another group of pages where I haven't been able to get it to work yet. In these pages I'm not using file extensions in the URLs (though they are still present on the actual files on the server). So the URLs for my page look like

[mysite.com...] (rather than)
[mysite.com...]

From my simple-minded tests, it looks like the problem now is in the line:

$current_page = basename( $_SERVER['PHP_SELF'] );

I'm trying to read up now on how that line specifies the file name. (But may be way off, who knows.)

twist

8:11 am on Jan 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You know, if you want to be a minor celebrity you could write up a generalized version of these instructions to be published on "A List Apart" - I got started by cutting and pasting from their article on "keeping navigation current" [oops, WebmasterWorld won't let me link to it - you can find it with a search]. This has been a very highly cited article that got a lot of attention. A similar "next/previous" article would probably be very popular.

The code I wrote is actually pretty sloppy. I just simplified the stuff they were doing in the examples on php.net to pertain to your situation.

http://mysite.com/testing/2004-blue-widget (rather than)
[mysite.com...]

If i'm not mistaken $_SERVER['PHP_SELF'] will grab "2004-blue-widget" and not the files real name in the directory.

Where as, readdir() is going to grab the actual filename off the server, i'm guessing.

The easiest way to figure it out is to use some echo/print statements to output what it's actually reading to screen. If readdir() is grabbing the extensions then just remove the extensions before putting them into the array.