Goeiemorgen iedereen,
Ben ik weer. Ik probeer mijn query resultaten te verdelen over meerdere paginas. Nu heb ik een mooie tutorial gebvonden op about.com (http://php.about.com/od/phpwithmysql/ss/php_pagination.htm)
Het probleem is, wat ik ook probeer, de variable $pagenum blijft altijd 1.
de code:
<?php
if (!(isset($pagenum)))
{
$pagenum = 1;
}
//Here we count the number of results
//Edit $data to be your query
$data = mysql_query("SELECT * FROM artikelen WHERE categorie_id = '".mysql_real_escape_string($q['id'])."'") or die(mysql_error());
$rows = mysql_num_rows($data);
//This is the number of results displayed per page
$page_rows = 1;
//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 the range to display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
$data_p = mysql_query("SELECT * FROM artikelen WHERE categorie_id = '".mysql_real_escape_string($q['id'])."' $max") or die(mysql_error());
//This is where you display your query results
$cat = mysql_real_escape_string($_GET['category']);
while($info = mysql_fetch_array( $data_p ))
{
echo "<a href=\"".$cat."/".$info['slug'].".html\">".$info['title']."</a><br />";
}
echo $pagenum;
echo "<p>";
// This shows the user what page they are on, and the total number of pages
echo "Pagina $pagenum van $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['PHP_SELF']}?pagenum=1&category=".$cat."'> <<-Eerste</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous&category=".$cat."'> <-Vorige</a> ";
}
//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['PHP_SELF']}?pagenum=$next&category=".$cat."'>Volgende -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last&category=".$cat."'>Laatste ->></a> ";
}
?>
De fout zit hem ergens aan het begin. Bij
<?php
if (!(isset($pagenum)))
{
$pagenum = 1;
} ?>
Als ik dit getal bijv verander in 2, dan begint hij gewoon bij pagina 2 en de daarbij behorende resultaten. Alleen is dit niet de bedoeling. $pagenum word in de url meegegeven, en ik kreeg geen foutmeldingen. De pagina knoppen geven wel het goeie pagina nummer aan, maar ook al zet ik in de url $pagenum = 2, dan nog is de werkelijke $pagenum waarde 1, als ik deze echo. Hij veranderd dus eigenlijk niet =(.
Ik hoop echt dat jullie me weer een zetje in de goeie richting kunnen geven!
Alvast ontzettend bedankt!!
(Er staan nog wat lelijke dingetjes in zoals or_die , die gaan er uiteraard uit!)
1.505 views