Solved

paging with php

Posted on 2011-03-22
7
389 Views
Last Modified: 2012-05-11
Please help for php secure paging
0
Comment
Question by:visionplus
7 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35188693
"Paging" is usually done via the MySQL SELECT....LIMIT clause, but if you can describe you are trying to do we can help better - you've given us nine words including the title and two of those are PHP.
0
 
LVL 2

Accepted Solution

by:
pixalax earned 500 total points
ID: 35482358
It is quite easy.

Let's say you have a table in your database called topics.

1. You have to gather page number from address bar (URL) (example: http://www.yourdomain.com/topics.php?page=10).

PHP part;
<?php
// You are gathering ?page= value from address bar.
$pageNumber = intval($_GET['page']); // intval() = we only accept numbers. If there is no ?page= value, then $pageNumber will be equal to 0.

$recordsPerPage = 20; // 20 records / topics per page.

// Find the offset
// Assuming 20 items per page:
// page 1 has an offset of 0 (1-1) * 20
// page 2 has an offset of 20 (2-1) * 20
// in other words, page2 starts with item 21

$offset = ($pageNumber -  1) * $recordsPerPage;
// Since our $pageNumber could be 0, lets fix $offset to be sure it won't be less than 0
($offset > 0) ? $offset : $offset = 0;

// Calculate the limit;
$limit = $recordsPerPage * $pageNumber;
// Since our $pageNumber could be 0, lets fix $limit to be sure it won't be 0
($limit == 0) ? $limit : $limit = $pageNumber;

/*
Now we have ;
1. How many records we want to display per page ($recordsPerPage )
2. How many records we will skip ($offset)
3. How many records we will be limiting ($limit)
4. Our page number.
*/

$paginationSQL = "SELECT (*) FROM topics LIMIT $limit, $offset"; // RUN THIS SQL query in whatever you are using (mysql, mysqli, PDO) and you are done.
?>

Open in new window

0
 
LVL 5

Expert Comment

by:eNarc
ID: 35687664
this is what I use for my paging, very simple, very easy.
<?php
$Q2="select count(*) as numrows from items where sub = '$_GET[ite]';";
$D4=mysql_fetch_array(mysql_query($Q2));


if($D4['numrows'] > $_SESSION['rowlimit']){ 
if($_SESSION[rowlimit]!=0){
?>    
<div id="header">
	<H4 class="footer">
    	<SPAN class="block">
		</SPAN>
 <center>		
		<SPAN style="text-align:right; float:right;" onclick="window.location='#top'">
			<img src="include/database/48px-Go-down.svg2.png" title="View Top" width="16" />
		</SPAN><?php
			  
			$ff=ceil($D4['numrows']/$_SESSION['rowlimit']);
              ?>
<select style="width:auto;font-size:14px" onChange="window.location=this.value;" name="drop_down">    
<?php			  
	for ($i=1; $i<=$ff; $i++){
		$rowscount=$i * $_SESSION['rowlimit'];
		$rowscounts=$rowscount - $_SESSION['rowlimit'];
		$ffd= 'Row '.$rowscounts." to " .$rowscount.' - '.$i.'/'.$ff.' Pages'; ;
		#echo "<option value=\"?category=".$_GET['category']."&row=$rowscounts\">$ffd</option>"; 
		//$optionlayout.="<option value=\"?category=".$_GET['category']."&row=$rowscounts\">$ffd</option>\n";
	
	if($_GET['row']==$rowscounts){
		$nowrow=$ffd;
		$optionlayout.="<option value=\"?category=".$_GET['category']."&row=$rowscounts\" selected>$ffd</option>\n";
	}else{
		$optionlayout.="<option value=\"?category=".$_GET['category']."&row=$rowscounts\">$ffd</option>\n";
	}
	
	}

	
	echo $optionlayout;
?> 
  </select></center>

 	</H4>
</div>
 <?php
	}
}
?>

Open in new window

0
 
LVL 2

Expert Comment

by:pixalax
ID: 35889231
@Dhaest;
My solution is working. I directly copied and pasted from my own code which I am using. I just showed the way.
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 35964414
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now