Jump to content

I want pagination in php


ankit.pandeyc012

Recommended Posts

<?php
require_once('upper.php');

// Connects to your Database
require_once('database.php');
//This checks to see if there is a page number. If not, it will set it to page 1
if (!(isset($pagenum)))
{
$pagenum = 1;
}

//Here we count the number of results
//Edit $data to be your query

$query = "SELECT * FROM events";
$result = mysqli_query($dbc,$query) or die('Not');
$rows = mysqli_num_rows($result);

//This is the number of results displayed per page
$page_rows = 2;

//This tells us the page number of our last page
$last = ceil($rows/$page_rows);

//this makes sure the page number isn't below one, or more than our maximum pages
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}

//This sets range that we will display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

//This is your query again, the same one... the only difference is we add $max into it
$result = mysqli_query($dbc,"SELECT * FROM events $max") or die('Not');

//This is where you display your query results
while($row = mysqli_fetch_array( $result))
{
$Title=$row['Title'];
$City=$row['City'];
$Content=$row['Content'];
//$Photo=$row['Photo'];
$Date=$row['Date'];
$EventId=$row['EventId'];
$Photo=$row['Photo'];
//echo $Photo;

echo	"<div><table border='0' cellspacing='0' cellpadding='0' width='389'>
		<tr>
			<td><img src='images/events_2.png' width='389' height='10'></td>
		</tr>

		<tr>
			<td background='images/events_2_bg.png'>
				<table border='0' cellspacing='0' width='359'>
					<tr>

						<td>
							<tr>
								<table width='100%' border='0' cellspacing='0' >
									<tr>
										<td rowspan='3'><img src='$Photo' width='80' height='60'></td>			
										<td align='left' valign='top' width='180'>$City</td>
										<td align='left' valign='top'>$Date</td>											
									</tr>
									<tr>											
									    <td colspan='3' align='left' valign='top'>$Title</td>						

									</tr>
									<tr>
										<td colspan='2'><a href='KnowMore.php?EventId=".$row['EventId']."'>Know more </a> / <a href='EventParticipator.php?EventId=".$row['EventId']."'>click here to participate</a></td>
									</tr>

								</table>
							</tr>

					</tr>	
				</table>					
			</td>
		</tr>

		<tr>
			<td><img src='images/events_2_bottom.png' width='389' height='10' ></td>
		</tr>

	</table></div>";

}


echo "<p>";

// This shows the user what page they are on, and the total number of pages
echo " --Page $pagenum of $last-- <p>";

// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum == 1)
{
}
else
{
echo " <a href='{$_SERVER['pagination.php']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['page_test.php']}?pagenum=$previous'> <-Previous</a> ";
}
echo $PHP_SELF;


//just a spacer
echo " ---- ";

//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['page_test.php']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['page_test.php']}?pagenum=$last'>Last ->></a> ";
}
require_once('lower.php');
?>

 

 

Hi friends............

I want to paginate my page but it not works properly.

It displays first 2 rows but on clicking "Next" nothing happens.

It displays error also "Notice: Undefined variable: PHP_SELF in C:\wamp\www\NGOProject\Elite Brigade\pagination.php on line 112" and "Notice: Undefined index: page_test.php in C:\wamp\www\NGOProject\Elite Brigade\pagination.php on line 124".........

I don't know what to do??????

Plz help me anyone???????

Link to comment
Share on other sites

Work on your semantic errors before the logic errors (if any).

 

The first error tell you that you don't have a variable called "PHP_SELF" defined. You probably want $_SERVER['PHP_SELF'].

 

The second error tells you that $_SERVER['page_test.php'] does not exist, although $_SERVER definitely does.

 

To see the valid keys for the $_SERVER predefined variable, read through the documentation here: http://www.php.net/manual/en/reserved.variables.server.php

Link to comment
Share on other sites

the logic seems correct. check the links (view-source), make sure that they are properly formatted and links you back to the same script.

 

and please don't say it doesn't work without giving any description of your errors. what logic errors are you getting? page not showing? links not showing? when do they happen? after clicking which link?

Link to comment
Share on other sites

read my full message:

 

the logic seems correct. check the links (view-source), make sure that they are properly formatted and links you back to the same script.

 

what about that? are the links okay? i would help if you follow the suggestions provided and then provide your feedback on what happens when you try them. in this case, it would help if you tell me whether the links aren't malformed and whether there are any errors generated in the links.

Link to comment
Share on other sites

as i've said, your script looks logical. and there isn't anything wrong with it, assuming you've correctly fixed the previously mentioned errors.

 

okay, i'll make this really simple

 

1. LOAD the page

2. VIEW-source

3. LOOK at all the href attributes of the links i.e. <a> elements.

 

what do you see? is it "your_script_name.php?pagenum=2"?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.