Solved

paging with php

Posted on 2011-03-22
7
394 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Triple line in an alert message 4 19
xml with php question 5 29
Ajax on ASP 2 31
Check input text, Number 6 26
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

679 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