Jump to content

Pagination problems


doddsey_65

Recommended Posts

Okay i will try to explain this the best I can. the page in question is tutorials.php.

 

As you can see i am using if statements because depending on the link they click they are either taken to tutorials.php?sort=animation or tutorials.php?sort=basics.

 

My code decides which page they are viewing and then displays records from a database that match the link they clicked, being either animation or basics. This works fine but when i added my pagination script it went wrong. When i click next i get no results displayed and the page reverts back to the else statement because the url doesnt match the if or elseif statements. This pagination works fine when i am not including these if statements but i need it to work for these. Anyone know how? Heres the code.

 

<?php 
ob_start();
include('header.php'); 
include('db.php');
    $db=mysql_connect($db_host,$db_user,$db_pass)
or die ('I cannot connect to the database because: ' . mysql_error());
    mysql_select_db($db_name,$db);

$url = $_SERVER["REQUEST_URI"];
echo '<div id="page">';
echo '<div id="content">';

if ($url == '/tutorials.php?sort=animation'):

$pagenum = $_GET['pagenum'];
if (!(isset($pagenum))) 
{ 
$pagenum = 1; 
} 

$data = mysql_query("SELECT * FROM tutorials WHERE category='1'") or die(mysql_error()); 
$rows = mysql_num_rows($data); 

$page_rows = 5; 
$last = ceil($rows/$page_rows); 

if ($pagenum < 1) 
{ 
$pagenum = 1; 
} 
elseif ($pagenum > $last) 
{ 
$pagenum = $last; 
} 

$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

$result = mysql_query("SELECT * FROM tutorials WHERE category = '1' $max") or die(mysql_error());
while($row = mysql_fetch_object($result)) {

echo '<div class="post">';

echo '<p class="meta">';

echo $row->fullname. ' | ' .$row->name;
echo '</a>';

echo '</p><div class="entry">';

echo '<center>' . $row->path .'<br>';

echo $row->description;

echo '</center></div></div>';

}

echo " Page $pagenum of $last <p>";

if ($pagenum == 1) 
{
} 
else 
{
echo " <a href='{$_SERVER['PHP_SELF']}?sort=animation&&pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?sort=animation&&pagenum=$previous'> <-Previous</a> ";
} 

echo " ---- ";

if ($pagenum == $last) 
{
} 
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?sort=animation&&pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?sort=animation&&pagenum=$last'>Last ->></a> ";
} 

elseif ($url == '/tutorials.php?sort=basics'):

$pagenum = $_GET['pagenum'];

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

$data = mysql_query("SELECT * FROM tutorials WHERE category='2'") or die(mysql_error()); 
$rows = mysql_num_rows($data); 

$page_rows = 5; 
$last = ceil($rows/$page_rows); 

if ($pagenum < 1) 
{ 
$pagenum = 1; 
} 
elseif ($pagenum > $last) 
{ 
$pagenum = $last; 
} 

$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

$result = mysql_query("SELECT * FROM tutorials WHERE category = '2'") or die(mysql_error());
while($row = mysql_fetch_object($result)) {

echo '<div class="post">';			
echo '<p class="meta"><a href="tutorials.' . $row-fullname. '.php">' . $row->fullname. ' | ' .$row->name.'</a>';
echo '</p><div class="entry">';

echo '<img src="' . $row->path .'">';
echo $row->description;

echo '</div></div>';

}

echo " Page $pagenum of $last <p>";

if ($pagenum == 1) 
{
} 
else 
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
} 

echo " ---- ";

if ($pagenum == $last) 
{
} 
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?sort=basics&&pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?sort=basics&&pagenum=$last'>Last ->></a> ";
} 

else:

echo '<div class="post">';			
echo '<p class="meta">Choose a Category';
echo '</p><div class="entry">';

echo '</div></div>';

endif;

echo '</div>';

include('footer.php');
ob_flush(); 
?>

Link to comment
https://forums.phpfreaks.com/topic/197638-pagination-problems/
Share on other sites

try

<?php 
ob_start();
include('header.php');
include('db.php');
$db=mysql_connect($db_host,$db_user,$db_pass)
or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db($db_name,$db);
$url = $_SERVER["REQUEST_URI"];
echo '<div id="page">';
echo '<div id="content">';
if (!isset($_GET['sort'])) $_GET['sort'] = 'animation';
if ($_GET['sort'] == 'animation') $cat=1; elseif ($_GET['sort'] == 'basics') $cat=2;

$pagenum = $_GET['pagenum'];
if (!(isset($pagenum)))
{
$pagenum = 1;
}

$data = mysql_query("SELECT * FROM tutorials WHERE category='".$cat."'") or die(mysql_error());
$rows = mysql_num_rows($data);

$page_rows = 5;
$last = ceil($rows/$page_rows);

if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}

$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

$result = mysql_query("SELECT * FROM tutorials WHERE category = '".$cat."' $max") or die(mysql_error());
while($row = mysql_fetch_object($result)) {

echo '<div class="post">';

echo '<p class="meta">';

echo $row->fullname. ' | ' .$row->name;
echo '</a>';

echo '</p><div class="entry">';

echo '<center>' . $row->path .'<br>';

echo $row->description;

echo '</center></div></div>';

}

echo " Page $pagenum of $last <p>";

if ($pagenum == 1)
{
}
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?".$_GET['sort']."&pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?sort=".$_GET['sort']."&pagenum=$previous'> <-Previous</a> ";
}

echo " ---- ";

if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?sort=".$_GET['sort']."&pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?sort=".$_GET['sort']."&pagenum=$last'>Last ->></a> ";
}

echo '<div class="post">';
echo '<p class="meta">Choose a Category';
echo '</p><div class="entry">';
echo '</div></div>';
echo '</div>';
include('footer.php');
ob_flush();
?>

Link to comment
https://forums.phpfreaks.com/topic/197638-pagination-problems/#findComment-1037315
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.