Jump to content

Why isn't my pagination working?


AdamCCFC

Recommended Posts

The page is showing all the results from my database, then when the link to go to the second page is clicked the same results are being displayed on that page. I have basically merged two scripts together. I had the paination working showing the results, but that was before I had the comment system in place. When I put the script of the comment system with the pagination script, it stopped working properly.

 

Any help would be greatly appreciated! Thanks in advanced, Adam!

 

Here's my code

 

			<?php
			include('connect.php');	

			$tableName="dream";		
			$targetpage = "index.php"; 	
			$limit = 5; 

			$query = "SELECT COUNT(*) as num FROM $tableName ORDER BY `date_entered` DESC";
			$total_pages = mysql_fetch_array(mysql_query($query));
			$total_pages = $total_pages[num];

			$stages = 3;
			$page = mysql_escape_string($_GET['page']);
			if($page){
				$start = ($page - 1) * $limit; 
			}else{
				$start = 0;	
				}	

			// Get page data
			$query1 = "SELECT * FROM $tableName LIMIT $start, $limit";
			$result = mysql_query($query1);

			// Initial page num setup
			if ($page == 0){$page = 1;}
			$prev = $page - 1;	
			$next = $page + 1;							
			$lastpage = ceil($total_pages/$limit);		
			$LastPagem1 = $lastpage - 1;					


			$paginate = '';
			if($lastpage > 1)
			{	

				$paginate .= "<div class='paginate'>";
				// Previous
				if ($page > 1){
					$paginate.= "<a href='$targetpage?page=$prev'>Previous</a>";
				}else{
					$paginate.= "<span class='disabled'>Previous</span>";	}



				// Pages	
				if ($lastpage < 7 + ($stages * 2))	// Not enough pages to breaking it up
				{	
					for ($counter = 1; $counter <= $lastpage; $counter++)
					{
						if ($counter == $page){
							$paginate.= "<span class='current'>$counter</span>";
						}else{
							$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}					
					}
				}
				elseif($lastpage > 5 + ($stages * 2))	// Enough pages to hide a few?
				{
					// Beginning only hide later pages
					if($page < 1 + ($stages * 2))		
					{
						for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
						{
							if ($counter == $page){
								$paginate.= "<span class='current'>$counter</span>";
							}else{
								$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}					
						}
						$paginate.= "...";
						$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
						$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";		
					}
					// Middle hide some front and some back
					elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
					{
						$paginate.= "<a href='$targetpage?page=1'>1</a>";
						$paginate.= "<a href='$targetpage?page=2'>2</a>";
						$paginate.= "...";
						for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
						{
							if ($counter == $page){
								$paginate.= "<span class='current'>$counter</span>";
							}else{
								$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}					
						}
						$paginate.= "...";
						$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
						$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";		
					}
					// End only hide early pages
					else
					{
						$paginate.= "<a href='$targetpage?page=1'>1</a>";
						$paginate.= "<a href='$targetpage?page=2'>2</a>";
						$paginate.= "...";
						for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
						{
							if ($counter == $page){
								$paginate.= "<span class='current'>$counter</span>";
							}else{
								$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}					
						}
					}
				}

						// Next
				if ($page < $counter - 1){ 
					$paginate.= "<a href='$targetpage?page=$next'>Next</a>";
				}else{
					$paginate.= "<span class='disabled'>Next</span>";
					}

				$paginate.= "</div>";		


		}

		function displayDreams($all = 0) {
			/* bring in two variables
			 * $db is our database connection
			 * $max_items is the maximum number
			 * of news items we want to display */
			global $db, $max_items;

			/* query for news items */
			if ($all == 0) {
				/* this query will get all news */
				$query = "SELECT dream_id,name,dream,tags FROM dream ORDER BY date_entered DESC";
			}
			$result = mysql_query ($query);
			while ($row = mysql_fetch_assoc ($result)) {

				/* Here we also make sure no
				 * HTML tags, other than the
				 * ones we want are displayed */
				$date = $row['date_entered'];        
				$name = htmlentities ($row['name']);
				$dream = nl2br (strip_tags ($row['dream'], '<a><b><i><u>'));
				$tags = $row['tags'];

				/* display the data */
				echo "<h1>" . $row['name'] . "</h1>";
				echo "<p class='dream'>" . $row['dream'] . "</p>";				  
				echo "<p class='tags'>" . $row['tags'] . "</p>";
				echo "<p class='date'>" . $row['date_entered'] . "</p>";

				/* get number of comments */
				$comment_query = "SELECT count(*) FROM dreams_comments WHERE dream_id={$row['dream_id']}";
				$comment_result = mysql_query ($comment_query);
				$comment_row = mysql_fetch_row($comment_result);

				/* display number of comments with link */
				echo "<p class='comments'><a href=\"{$_SERVER['PHP_SELF']}?action=show&id={$row['dream_id']}\">Comments ($comment_row[0])</a></p>";
				echo "</br>";
			}

			/* if we aren't displaying all dream, 
			 * then give a link to do so */
			if ($all == 0) {
				echo "<a href=\"{$_SERVER['PHP_SELF']}\"?action=all'\">'View all dreams'</a>";
			}
		}

		function displayOneItem($id) {
			global $db;

			/* query for item */
			$query = "SELECT * FROM `dream` WHERE `dream_id` = '$id'";
			$result = mysql_query ($query);

			/* if we get no results back, error out */
			if (mysql_num_rows ($result) == 0) {
				echo "Bad Dream ID\n";
				return;
			}
			$row = mysql_fetch_assoc($result);

			/* easier to read variables and 
			 * striping out tags */
			$name = htmlentities ($row['name']);
			$dream = nl2br (strip_tags ($row['dream'], '<a><b><i><u>'));
			$tags = ($row['tags']);

			/* display the items */
			echo "<h1>" . $row['name'] . "</h1>";
			echo "<p class='dream'>" . $row['dream'] . "</p>";				  
			echo "<p class='tags'>" . $row['tags'] . "</p>";
			echo "<p class='date'>" . $row['date_entered'] . "</p>";

			echo "</br>";

			/* now show the comments */
			echo "<p class id='comments'>displayComments($id)</p>";
		}

		function displayComments($id) {
			/* bring db connection variable into scope */
			global $db;

			/* query for comments */
			$query = "SELECT * FROM dreams_comments WHERE dream_id=$id";
			$result = mysql_query ($query);
			echo "Comments:</br>";

			/* display the all the comments */
			while ($row = mysql_fetch_assoc ($result)) {

				$name = htmlentities ($row['name']);
				echo "<h2>$name</h2>";

				$comment = strip_tags ($row['comment'], '<a><b><i><u>');
				$comment = nl2br ($comment);
				echo "<p class='user_comment'>$comment</p>";

				echo "</br>";
			}

			/* add a form where users can enter new comments */
			echo "Leave a comment!</br>";
			echo "</ br>";
			echo "<form id='addcomment' action=\"" . $_SERVER['PHP_SELF'] . "?action=addcomment&id=$id\" method='POST'>";
			echo "<table border='0'>";
			echo "<tr><td>Name:</td><td><input type'text' width'30' name='name'></td></tr>";
			echo "<tr><td>Comment:</td><td><textarea cols='30' rows='10' name='comment'></textarea></td></tr>";
			echo "<tr><td><input type='submit' name='submit' value='Add Comment'</td></tr>";
			echo "</form>";	
			echo "</table>";
		}

		function addComment($id) {
			global $db;
			/* insert the comment */
			$query = "INSERT INTO dreams_comments VALUES('','$id','$_POST[name]','$_POST[comment]')";
			mysql_query($query);

			echo "Comment entered. Thanks!</br>";
			echo "<a href=\"{$_SERVER['PHP_SELF']}?action=show&id=$id\">Back</a>";
		}

		/* this is where the script decides what do do */


		switch($_GET['action']) {

			case 'show':
				displayOneItem($_GET['id']);
				break;
			case 'all':
				displayDreams(1);
				break;
			case 'addcomment':
				addComment($_GET['id']);
				break;
			default:
				displayDreams();
		}


		 // pagination
		echo $paginate;
		echo $total_pages.' Results';

	?>

Link to comment
https://forums.phpfreaks.com/topic/189775-why-isnt-my-pagination-working/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.