Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

paging with php

Posted on 2011-03-22
7
Medium Priority
?
398 Views
Last Modified: 2012-05-11
Please help for php secure paging
0
Comment
Question by:visionplus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
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 viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

704 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