0

Okay, so i get this error on line 19 in this pagination script.
The error goes like this:

Notice: Undefined index: page in C:\wamp\www\pagination.php on line 19

This is wrong: $page = $_GET;

But i don't realy understand what is wrong, because the targetpage is set to "pagination.php".

Maybe it's worth metioning that the pagination.php file is included in the index.php file. Maybe i should have just put the code right in the index.php file instead of including it?

<?php

	include 'script_dbconnect_pc.php';
	
	// My table name.
	$tbl_name="images"; 
	
	// How many adjacent pages should be shown on each side?
	$adjacents = 5;
	
	$query = "SELECT COUNT(*) as num FROM $tbl_name";
	$total_pages = mysql_fetch_array(mysql_query($query));
	$total_pages = $total_pages['num'];
	
	/* Setup vars for query. */
	$targetpage = "pagination.php"; 	//Filename (name of this file).
	$limit = 6;							//How many items per page (pictures).
	
	$page = $_GET['page'];
	if($page) 
		$start = ($page - 1) * $limit; 			//First item to display on this page.
	else
		$start = 1;								//If no page var is given, set start to 0.
	
	/* Get data. */
	$sql = "SELECT * FROM $tbl_name ORDER BY $tbl_name.`id` DESC LIMIT $start, $limit";
	$result = mysql_query($sql);
	
	/* Setup page vars for display. */
	if ($page == 0) $page = 1;					//If no page var is given, default to 1.
	$prev = $page - 1;							//Previous page is page - 1.
	$next = $page + 1;							//Next page is page + 1.
	$lastpage = ceil($total_pages/$limit);		//Lastpage is = total pages / items per page, rounded up.
	$lpm1 = $lastpage - 1;						//Last page minus 1.
	
	/* 
		Now we apply our rules and draw the pagination object. 
		We're actually saving the code to a variable in case we want to draw it more than once.
	*/
	$pagination = "";
	if($lastpage > 1)
	{	
		$pagination .= "<div class=\"pagination\">";
		
		//Previous button.
		if ($page > 1) 
			$pagination.= "<a href=\"$targetpage?page=$prev\">« previous</a>";
		else
			$pagination.= "<span class=\"disabled\">« previous</span>";	
		
		//Pages	
		if ($lastpage < 7 + ($adjacents * 2))	//Not enough pages to bother breaking it up.
		{	
			for ($counter = 1; $counter <= $lastpage; $counter++)
			{
				if ($counter == $page)
					$pagination.= "<span class=\"current\">$counter</span>";
				else
					$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";					
			}
		}
		elseif($lastpage > 5 + ($adjacents * 2))	//Enough pages to hide some.
		{
			//Close to beginning; only hide later pages.
			if($page < 1 + ($adjacents * 2))		
			{
				for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
				{
					if ($counter == $page)
						$pagination.= "<span class=\"current\">$counter</span>";
					else
						$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";					
				}
				$pagination.= "...";
				$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
				$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";		
			}
			//In middle; hide some front and some back.
			elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
			{
				$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
				$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
				$pagination.= "...";
				for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
				{
					if ($counter == $page)
						$pagination.= "<span class=\"current\">$counter</span>";
					else
						$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";					
				}
				$pagination.= "...";
				$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
				$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";		
			}
			//Close to end; only hide early pages.
			else
			{
				$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
				$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
				$pagination.= "...";
				for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
				{
					if ($counter == $page)
						$pagination.= "<span class=\"current\">$counter</span>";
					else
						$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";					
				}
			}
		}
		
		//Next button.
		if ($page < $counter - 1) 
			$pagination.= "<a href=\"$targetpage?page=$next\">next »</a>";
		else
			$pagination.= "<span class=\"disabled\">next »</span>";
		$pagination.= "</div>\n";		
	}
?>

	<?php
	while($row = mysql_fetch_array($result)){
		echo "<a href='". $row['path'] . "'><img id='images' src='" . $row['path'] . "' alt='image' /></a>"; 
	}
	?>
    
<?=$pagination?>
3
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by cereal
0

Didn't really look very hard in the script since i haven't slept in 27 hours, but try adding the form directly to the php document.. im sorry im just so tired.. if this doesn't make sense ignore it

0

Didn't really look very hard in the script since i haven't slept in 27 hours, but try adding the form directly to the php document.. im sorry im just so tired.. if this doesn't make sense ignore it

You need to sleep mate, haha.
But it did'nt work, still getting error on the page line.
But thank for trying! :)

0

I'll try when i get up if its not solved by then which it prob will be.. sorry that i couldn't help right now :S

0

Or just replace line 19 and 20 with this:

$page = isset($_GET['page']) ? $_GET['page'] : false;
if($page != false)

If you don't send page var through the link then $page is undefined and gives you an error.

Edited by cereal: n/a

0

Or just replace line 19 and 20 with this:

$page = isset($_GET['page']) ? $_GET['page'] : false;
if($page != false)

If you don't send page var through the link then $page is undefined and gives you an error.

Ahh, okay, i think i'll have to read more about the page var. :)
But that worked, thank you! :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.