Paginatie (blijft altijd op page 1)
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:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?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> ";
}
?>
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
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!)
Gewijzigd op 16/06/2012 08:17:47 door Patrick vd Pols
Tevens is het volstrekt onnodig om een hele tabel op te halen om het aantal rijen te bepalen, dat kan heel simpel met:
Code (php)
1
2
3
4
5
6
2
3
4
5
6
<?php
//even zonder fouthandeling en beveiliging
$result = mysql_query("SELECT COUNT(*) FROM artikelen WHERE category = " . $cat);
$row = mysql_fetch_row($result);
$total = $row[0];
?>
//even zonder fouthandeling en beveiliging
$result = mysql_query("SELECT COUNT(*) FROM artikelen WHERE category = " . $cat);
$row = mysql_fetch_row($result);
$total = $row[0];
?>
Gewijzigd op 16/06/2012 08:48:06 door Ger van Steenderen