-[solved]-Pagination problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Axllrod
Forum Newbie
Posts: 12
Joined: Tue Jun 12, 2012 4:15 pm
Location: Belgium

-[solved]-Pagination problem

Post by Axllrod »

I'm struggling with a pagination php. I'm a newbie at php so i looked for a 'template' on pagination. The pagination works, but the entries are not loaded. I already checked the loop that takes the entries from the database. That works so it has to be the pagination bit that doesn't work.
The code i have:

Code: Select all

                <?php	
					
						/*
						Place code to connect to your DB here.
						*/

						//include('config.php');	// include your code to connect to DB.
					
						mysql_select_db('petition') or die(mysql_error());
					
						$tbl_name='comments';		//your table name
						// How many adjacent pages should be shown on each side?
						$adjacents = 3;
						
						/* 
						   First get total number of rows in data table. 
						   If you have a WHERE clause in your query, make sure you mirror it here.
						*/
						$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 = "petition.php"; 	//your file name  (the name of this file)
						$limit = 1; 								//how many items to show per page
						$page = $_GET['page'];
						if($page) 
							$start = ($page - 1) * $limit; 			//first item to display on this page
						else
							$start = 0;								//if no page var is given, set start to 0
						
						/* Get data. */
						$sql = "SELECT text FROM $tbl_name 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($result = mysql_fetch_array($query)){ ?>
										<table class="marginbottom">
										<tr><td class="nameborder"> <?php echo $result['name'] ?></td><td><?php echo $result['text'] ?></td></tr>
										<tr><td class="centeralign"><?php echo $result['date'] ?></td><td class="numberleft">#<?php echo $result['id'] ?></td></tr>
										</table>
										<?php } ?>
					
					<?=$pagination?>
Thanks in advance!
Last edited by Axllrod on Fri Jul 13, 2012 9:58 am, edited 1 time in total.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Pagination problem

Post by social_experiment »

Code: Select all

<?php
  $total_pages = $total_pages[num];
  // should be
  $total_pages = $total_pages['num'];
?>
Also

Code: Select all

<?php
$sql = "SELECT text FROM $tbl_name LIMIT $start, $limit";
?>
You select 'text' in the above query but you wish to display id, name and record columns; if it's not selected in an sql query it cannot be displayed
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Axllrod
Forum Newbie
Posts: 12
Joined: Tue Jun 12, 2012 4:15 pm
Location: Belgium

Re: Pagination problem

Post by Axllrod »

I have made the adjustments as you said but still no change.
I changed the sql variable to the following. So the way i see it is that everything is selected.

Code: Select all

<?php
$sql = "SELECT * FROM $tbl_name LIMIT $start, $limit";
?>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Pagination problem

Post by social_experiment »


Notice: Use of undefined constant num - assumed 'num' in.../file01.php on line 22

Warning: mysql_fetch_array() expects parameter 1 to be resource, string given in .../file01.php on line 127

I got these two messages when running the script; it's wise to turn on error reporting when developing a script because it will help you catch problems like these.

The first message is related to the script below;

Code: Select all

<?php
$query = "SELECT COUNT(*) as num FROM $tbl_name";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];
// try the snippet below;
$query = "SELECT COUNT(*) FROM $tbl_name";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[0];
?>
The second is because you use a value called $query but never assigned it any value

Code: Select all

 // $query has no value here, hence no data and the warning above
  while($result = mysql_fetch_array($query)){ ?>
The code below resolves this issue (just replace my column names with the ones you want to display)

Code: Select all

<?php
while($ary = mysql_fetch_array($result)){ ?>
   <table class="marginbottom">
   <tr><td class="nameborder"> <?php echo $ary['bike_name'] ?></td><td><?php echo $ary['bike_name'] ?></td></tr>
   <tr><td class="centeralign"><?php echo $result['date'] ?></td><td class="numberleft">#<?php echo $ary['id'] ?></td></tr>
   </table>
<?php } ?>
?>
$result in this case contains the resource for the mysql_fetch_array() function; you did indeed create the value here...

Code: Select all

<?php
 $sql = "SELECT bike_name, id FROM $tbl_name LIMIT $start, $limit";
 $result = mysql_query($sql);
?>
but never used it correctly.

Code: Select all

<?php
$sql = "SELECT * FROM $tbl_name LIMIT $start, $limit";
?>
^ this isn't a good practise; just select what you need to display.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Axllrod
Forum Newbie
Posts: 12
Joined: Tue Jun 12, 2012 4:15 pm
Location: Belgium

Re: Pagination problem

Post by Axllrod »

Works like a charm! Thank you so much! I should have seen that the loop didn't get any data though.
Indeed, it is useful to work with error messages. The thing is, i'm using MAMP and it isn't standard enabled. But I will look into that for future projects.

Again, thanks for the quick help!
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Pagination problem

Post by social_experiment »

Take a look at this url; it shows how you can enable error reporting in your php.ini file (use a search engine to find it's location in your MAMP installation) or how to enable it during runtime.
http://nucleussystems.com/blog/enable-error-reporting
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Axllrod
Forum Newbie
Posts: 12
Joined: Tue Jun 12, 2012 4:15 pm
Location: Belgium

Re: Pagination problem

Post by Axllrod »

Thx for sharing that link. MAMP shows the error messages now ^^
Post Reply