Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

paging with php

Posted on 2011-03-22
7
Medium Priority
?
400 Views
Last Modified: 2012-05-11
Please help for php secure paging
0
Comment
Question by:visionplus
5 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Find out what you should include to make the best professional email signature for your organization.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
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 …
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…
Suggested Courses
Course of the Month6 days, 15 hours left to enroll

783 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