Solved

paging with php

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Find out what you should include to make the best professional email signature for your organization.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

856 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