Jump to content

Pagination Help


ivatanako

Recommended Posts

I needed to paginate my article section at my site so that i wont have 20 paragraphs page, now when I tried this on LOCALHOST i encountered no problem at all. But when I uploaded it on my server, the pagination doesn't work. I think the problem here is about the global variables, but im not sure and I dont know how to fix it. Here's my code:

 

<?php
$pagenum = $_GET['pagenum'];
$pagetitle = 'article Sections';
$active = '2';
include('../includes/header.php');
$dbcnt = mysql_connect("localhost", "xxxxxx", "xxxxx"); 
mysql_select_db("articles");

echo '<!-- content-wrap starts here -->
<div id="content-wrap">	'; 
include('../includes/sidebar.php');
echo '<div id="main">';
		//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 

		$data = mysql_query("SELECT * FROM dlink");
		$rows = mysql_num_rows($data);

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

//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; 
} 

$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
$data_p = mysql_query("SELECT * FROM dlink $max") or die(mysql_error());
		// Query the database
		while ($row = mysql_fetch_assoc($data_p)) {
			echo '<div class="box">';
			echo '<h1>' .$row['downName'].'</h1>';
			echo '<p>Added on: <cite>'.$row['downTime'].'<cite></p>';
			echo '<img src='.$row['downImg'].' width=140 height=110 class="dimg" /><p style="text-indent:1px;">'.$row['downDes'].'</p>';
			echo '<br clear=none/><p class=comments align-right>Download link:<a href='.$row['downLink'].' target="_self">Download link</a></p>';
			echo '</div><div class="boxBottom"><img src="http://ivatanako.000webhost.info/images/block-bottom-bg.jpg" alt=""></div>';
		}
echo '<div class="box"><p class=comments align-right>';

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

// 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='index.php?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='index.php?pagenum=$previous'> <-Previous</a> ";
} 


//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='index.php?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='index.php?pagenum=$last'>Last ->></a> ";
} 
echo '</div><div class="boxBottom"><img src="http://xxx.com/images/block-bottom-bg.jpg" alt=""></div>';
// end all div
echo '</div></div>';			
include('../includes/footer.php');
?>

Link to comment
Share on other sites

I dont know if I did went wrong because this is working on testing via LOCALHOST. But when it was uploaded to the webhost, it doesn't seem to work.

 

$pagenum = $_GET['pagenum'];

Now, if I remove the code above on localhost, i will encounter the same problem, so that's why I have a feeling that this could have something to do with GLOBAL_VARIABLES, but I dont know how to fix global_variables.

Link to comment
Share on other sites

What the line does is it checks if the "?pagenum" is present in the URL and if its not set then it'll set it as ?pagenum=1 for the first page. I tried changing

 

if (!(isset($pagenum))) 
{ 
$pagenum = 1; 
} 

 

to

if (!isset($pagenum)) 
{ 
$pagenum = 1; 
} 

 

But still there where no changes. This is where the query happens:

 

$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
$data_p = mysql_query("SELECT * FROM dlink $max") or die(mysql_error());
		// Query the database
		while ($row = mysql_fetch_assoc($data_p)) {
			echo '<div class="box">';
			echo '<h1>' .$row['downName'].'</h1>';
			echo '<p>Added on: <cite>'.$row['downTime'].'<cite></p>';
			echo '<img src='.$row['downImg'].' width=140 height=110 class="dimg" /><p style="text-indent:1px;">'.$row['downDes'].'</p>';
			echo '<br clear=none/><p class=comments align-right>Download link:<a href='.$row['downLink'].' target="_self">Download link</a></p>';
			echo '</div><div class="boxBottom"><img src="http://ivatanako.000webhost.info/images/block-bottom-bg.jpg" alt=""></div>';
		}
echo '<div class="box"><p class=comments align-right>';

 

 

 

Im sorry if I dont get your instructions and Im sorry about my bad english.

Link to comment
Share on other sites

Ironically enough, nothing's wrong with the query. I want to point out that this brilliant user as the or die(mysql_error()) on the stuff, so it's fine there.

 

I believe he's right with the variables however, and I want to try changing the code so you echo the LIMIT into the query instead of a string for it. $start_from is a calculation to determine what row to start counting from to a maximum of $perpage rows (so if you're on page1, it starts at row 0 to 10 rows [assuming 10 rows perpage.. etc])

 

$start_from = ($pagenum-1)*$page_rows;
$perpage = $page_rows;
		//This is your query again, the same one... the only difference is we add $max into it
$data_p = mysql_query("SELECT * FROM dlink LIMIT $start_from,$perpage") or die(mysql_error());
		// Query the database
		while ($row = mysql_fetch_array($data_p)) {
			echo '<div class="box">';
			echo '<h1>' .$row['downName'].'</h1>';
			echo '<p>Added on: <cite>'.$row['downTime'].'<cite></p>';
			echo '<img src='.$row['downImg'].' width=140 height=110 class="dimg" /><p style="text-indent:1px;">'.$row['downDes'].'</p>';
			echo '<br clear=none/><p class=comments align-right>Download link:<a href='.$row['downLink'].' target="_self">Download link</a></p>';
			echo '</div><div class="boxBottom"><img src="http://ivatanako.000webhost.info/images/block-bottom-bg.jpg" alt=""></div>';
		}
echo '<div class="box"><p class=comments align-right>';

Link to comment
Share on other sites

Thank you for the help. But it is still not working.

 

i like to add that, the last time I encountered this problem on testing at localhost is that when I forgot to add this

$pagenum = $_GET['pagenum'];

 

So assuming that Global_variables in the php.ini file is off, how do I change my variables so that I could get the value of pagenum in the url?

 

 

Link to comment
Share on other sites

i dont know if you have change this line

 

if (!(isset($pagenum))) <----if (!(boolean))

{

$pagenum = 1;

}

 

if (!(isset($pagenum))) <----if (!(boolean))

that line looks this way

if (!(true)) or if(!(false))

 

i dont think your gonna get the right result out of that condition

 

Link to comment
Share on other sites

I get this:

 

SELECT * FROM dlink limit 0,4

 

using echo ('SELECT * FROM dlink'.$max);

 

please do that on page 1 and 2 and print here the result

so that we know where to start looking (you only print the query for page one wheres page 2?)

 

Link to comment
Share on other sites

ill try explain the code, this code is placed inside index.php, when the page load, it'll set the index.php into index.php?pagenum=1 then fetch 4 data from the database

$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
$data_p = mysql_query("SELECT * FROM dlink $max") or die(mysql_error());

 

And automatically paginates the page, so if I have 5 data from the database, it will only display 4 data in the index.php?pagenum=1 then on index.php?pagenum=2 will display the last data.

 

This code creates the NEXT link

if ($pagenum == 1) 
{
} 
else 
{
echo " <a href='index.php?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='index.php?pagenum=$previous'> <-Previous</a> ";
} 


//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='index.php?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='index.php?pagenum=$last'>Last ->></a> ";
} 

 

Sorry, I really dont get want you mean about page 2.

Link to comment
Share on other sites

paging is generated by sql statement by setting its limit

 

so your first query gives this

SELECT * FROM dlink limit 0,4

which means in your page one it will display records form 0-4

and on your page 2 if you print your query it should print this way

SELECT * FROM dlink limit 4,8 

which means record 4-8

 

what im suspecting  is that your getting SELECT * FROM dlink limit 0,4 in page 1 in 2

 

so post you query in your index.php?pagenum=1 and index.php?pagenum=2

 

 

sorry i cant test that code so im asking those info

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.