I'm trying to implement paginaton on the article archive but I can't seem to get

$_SERVER['PHP_SELF']

to return the page that I need - I sort of know why but can't seem to solve the problem.

The archive page is located at http://localhost/cms/index.php?action=archive and so when I use PHP_SELF it returns the homepage.
I first get the current page with:

if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
					$currentpage = (int) $_GET['currentpage'];
				} else {
					$currentpage = 1;
				}

Then I try to create a link to the next page with

echo " <a href=action='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";

but this just returns the homepage.

Any help or ideas on this issue would be great.
Thanks.

Member Avatar for diafol
echo " <a href=action='?currentpage=$nextpage'>&gt;</a> ";

does that work?

Hey thanks for the reply. No that doesn't seem to work either and I actually can't seem to figure out the url that will give me the next page (I should probably mention that I am new to PHP).
For some reason I can get the example to work perfectly with a standalone php script that lists youtube urls from a mysql database. That's why I think the problem has to do with the page going back to index.php before returning the archive page. I know I'm probably being a little confusing but any hints or ideas would be a great help.
Thanks.

Member Avatar for diafol

Sorry, I can't make head nor tail of what you're trying to do.

Is {$_SERVER}?currentpage=$nextpage'

supposed to be the same as, for example:

index.php?currentpage=2

Well, the page that contains the archive is localhost/cms/?action=archive. Therefore I made the assumption that http://localhost/cms/?action=archive?currentpage=2 would give me the next page of results...but it just returns the homepage.

In the index.php file the archive functions contains

function archive() {

				$order = "publicationDate DESC";
				$sql = "SELECT SQL_CALC_FOUND_ROWS *, UNIX_TIMESTAMP(publicationDate) AS publicationDate FROM articles
					ORDER BY " . mysql_escape_string($order);
				$data = Article::getList( $sql );

				// get the info from the db 
				$results = array();
				$order = "publicationDate DESC";
				$sql = "SELECT SQL_CALC_FOUND_ROWS *, UNIX_TIMESTAMP(publicationDate) AS publicationDate FROM articles
					ORDER BY " . mysql_escape_string($order) . " LIMIT 1";//$offset, $rowsperpage";
				$data = Article::getList($sql);
				$results['articles'] = $data['results'];
				$results['totalRows'] = $data['totalRows'];
				$results['pageTitle'] = "Article Archive | Widget News";
				
				require( TEMPLATE_PATH . "/archive.php" );

				
}

And then in the archive.php file the pagination code is

<?php
				// find out how many rows are in the table 
				$numrows = $data['totalRows'];

				// number of rows to show per page
				$rowsperpage = 1;
				// find out total pages
				$totalpages = ceil($numrows / $rowsperpage);

				// get the current page or set a default
				if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
					// cast var as int
					$currentpage = (int) $_GET['currentpage'];
				} else {
					// default page num
					$currentpage = 1;
				} // end if

				// if current page is greater than total pages...
				if ($currentpage > $totalpages) {
					// set current page to last page
					$currentpage = $totalpages;
				} // end if
				// if current page is less than first page...
				if ($currentpage < 1) {
					// set current page to first page
					$currentpage = 1;
				} // end if
				
				
								/******  build the pagination links ******/
				// range of num links to show
				$range = 3;
				$links = array();
				$fullback = "";
				$back = "";
				// if not on page 1, don't show back links
				if ($currentpage > 1) {
					// show << link to go back to page 1
					echo " <a href='{$_SERVER['PHP_SELF']}?action=archive/currentpage=1'><<</a> ";
					// get previous page num
					$prevpage = $currentpage - 1;
					// show < link to go back to 1 page
					echo " <a href='{$_SERVER['PHP_SELF']}?action=archive/currentpage=$prevpage'><</a> ";
				} // end if 

				
				
				$otherpage = "";
				// loop to show links to range of pages around current page
				for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
					// if it's a valid page number...
					if (($x > 0) && ($x <= $totalpages)) {
						// if we're on current page...
						if ($x == $currentpage) {
							// 'highlight' it but don't make a link
							echo " [<b>$x</b>] ";
						// if not current page...
						} else {
							// make it a link
							echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
						} // end else
					} // end if 
				} // end for
                 
				
				$forwardlink = "";
				$fullforwardlink = "";
				// if not on last page, show forward and last page links        
				if ($currentpage != $totalpages) {
					// get next page
					$nextpage = $currentpage + 1;
					// echo forward link for next page 
					echo " <a href='action=?currentpage=$nextpage'>&gt;ddd</a> ";
					// echo forward link for lastpage
					echo " <a href='{$_SERVER['PHP_SELF']}?action=archive?currentpage=$totalpages'>>></a> ";
					echo $_SERVER['SERVER_NAME'];
				} // end if
				/****** end build pagination links ******/
				
				?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.