automatische-paginanummering
Gesponsorde koppelingen
PHP script bestanden
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
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
<?php
//connect
mysql_connect("***","***","***");
mysql_select_db("***");
//Retrieve and create the $each and $each_array vars
if(isset($_GET['each'])&&is_numeric($_GET['each'])) $each = $_GET['each'];
else $each = 10;
$each_options = array("5", "10", "15", "20");
//Create the $counter var
$query = mysql_query("select count(*) as num from test");
$counter = mysql_result($query, 0, "num");
//Create the $pages var
$pages = $counter / $each;
$pages = ceil($pages);
//Check on witch page you are now
if(isset($_GET['pag'])&&$_GET['pag'] <= $pages&&$_GET['pag']>0&&is_numeric($_GET['pag'])) $pag = $_GET['pag'];
else $pag = 1;
//Get the results from the database
$first_result = $pag * $each - $each;
$query = mysql_query("select * from test limit ".$first_result.", ".$each);
function render_each_dropdown($each_options,$each)
{
echo '<form action="index.php">';
echo '<select name="each" onchange="this.form.submit();">'."\n";
foreach($each_options as $item)
{
echo '<option value="'.$item.'"';
if($item==$each) echo ' selected';
echo '>'.$item.'</option>'."\n";
}
echo '</select></form>';
}
function render_arrow($kind,$pag,$pages,$each)
{
$pag_back = $pag-1;
$pag_next = $pag+1;
if($kind == "back")
{
echo '<a ';
if($pag > 1) echo 'href="index.php?pag='.$pag_back.'&each='.$each.'"';
echo '><<</a>';
}
if($kind == "forward")
{
echo '<a ';
if($pag < $pages) echo 'href="index.php?pag='.$pag_next.'&each='.$each.'"';
echo '>>></a>';
}
}
function render_pagenumbers($pag,$pages,$each)
{
$i = 1;
while($i<=$pages)
{
echo '<a ';
if($i!=$pag) echo 'href="index.php?pag='.$i.'&each='.$each.'"';
echo '>'.$i.'</a> ';
$i++;
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Pages test</title>
<style type="text/css" media="all">
<!--
#Comment { float: left;
margin: 5px 50px 0px 0px;
padding: 2px 0px;
background-color: #C3C3C3;
width: 100%; }
#Comment .date { color: #CCFFCC;
font-size: 11px; }
-->
</style>
</head>
<body>
<?php
//Echo the information about the table and settings
echo 'aantal rows: '.$counter."<br />\n";
echo 'Aantal rows per pagina: '.$each."<br />\n";
echo 'Aantal pagina\'s: '.$pages."<br />\n";
echo "<br />\n";
render_each_dropdown($each_options,$each);
render_arrow(back,$pag,$pages,$each);
echo $pag;
render_arrow(forward,$pag,$pages,$each);
echo "<br />\n";
render_pagenumbers($pag,$pages,$each);
//echo the rows
while($r=mysql_fetch_array($query))
{
$id=$r["id"];
$name=$r["name"];
$text=$r["text"];
$date=$r["date"];
$time=$r["time"];
echo '<div id="Comment"><b>'.$name.'</b><br>'.$text.'<br><span class="date">'.$date.' | '.$time.' (<a href="index.php?edit='.$id.'">'.$id.'</a>)</span></div><br>';
}
?>
</body>
</html>
//connect
mysql_connect("***","***","***");
mysql_select_db("***");
//Retrieve and create the $each and $each_array vars
if(isset($_GET['each'])&&is_numeric($_GET['each'])) $each = $_GET['each'];
else $each = 10;
$each_options = array("5", "10", "15", "20");
//Create the $counter var
$query = mysql_query("select count(*) as num from test");
$counter = mysql_result($query, 0, "num");
//Create the $pages var
$pages = $counter / $each;
$pages = ceil($pages);
//Check on witch page you are now
if(isset($_GET['pag'])&&$_GET['pag'] <= $pages&&$_GET['pag']>0&&is_numeric($_GET['pag'])) $pag = $_GET['pag'];
else $pag = 1;
//Get the results from the database
$first_result = $pag * $each - $each;
$query = mysql_query("select * from test limit ".$first_result.", ".$each);
function render_each_dropdown($each_options,$each)
{
echo '<form action="index.php">';
echo '<select name="each" onchange="this.form.submit();">'."\n";
foreach($each_options as $item)
{
echo '<option value="'.$item.'"';
if($item==$each) echo ' selected';
echo '>'.$item.'</option>'."\n";
}
echo '</select></form>';
}
function render_arrow($kind,$pag,$pages,$each)
{
$pag_back = $pag-1;
$pag_next = $pag+1;
if($kind == "back")
{
echo '<a ';
if($pag > 1) echo 'href="index.php?pag='.$pag_back.'&each='.$each.'"';
echo '><<</a>';
}
if($kind == "forward")
{
echo '<a ';
if($pag < $pages) echo 'href="index.php?pag='.$pag_next.'&each='.$each.'"';
echo '>>></a>';
}
}
function render_pagenumbers($pag,$pages,$each)
{
$i = 1;
while($i<=$pages)
{
echo '<a ';
if($i!=$pag) echo 'href="index.php?pag='.$i.'&each='.$each.'"';
echo '>'.$i.'</a> ';
$i++;
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Pages test</title>
<style type="text/css" media="all">
<!--
#Comment { float: left;
margin: 5px 50px 0px 0px;
padding: 2px 0px;
background-color: #C3C3C3;
width: 100%; }
#Comment .date { color: #CCFFCC;
font-size: 11px; }
-->
</style>
</head>
<body>
<?php
//Echo the information about the table and settings
echo 'aantal rows: '.$counter."<br />\n";
echo 'Aantal rows per pagina: '.$each."<br />\n";
echo 'Aantal pagina\'s: '.$pages."<br />\n";
echo "<br />\n";
render_each_dropdown($each_options,$each);
render_arrow(back,$pag,$pages,$each);
echo $pag;
render_arrow(forward,$pag,$pages,$each);
echo "<br />\n";
render_pagenumbers($pag,$pages,$each);
//echo the rows
while($r=mysql_fetch_array($query))
{
$id=$r["id"];
$name=$r["name"];
$text=$r["text"];
$date=$r["date"];
$time=$r["time"];
echo '<div id="Comment"><b>'.$name.'</b><br>'.$text.'<br><span class="date">'.$date.' | '.$time.' (<a href="index.php?edit='.$id.'">'.$id.'</a>)</span></div><br>';
}
?>
</body>
</html>
Dan heb ik dit zelf als MySQL table gebruikt:
Code (php)
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
CREATE TABLE `test` (
`id` int(11) NOT NULL auto_increment,
`name` text collate utf8_bin NOT NULL,
`text` text collate utf8_bin NOT NULL,
`date` text collate utf8_bin NOT NULL,
`time` text collate utf8_bin NOT NULL,
PRIMARY KEY (`id`),
KEY `id_2` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=75 ;
`id` int(11) NOT NULL auto_increment,
`name` text collate utf8_bin NOT NULL,
`text` text collate utf8_bin NOT NULL,
`date` text collate utf8_bin NOT NULL,
`time` text collate utf8_bin NOT NULL,
PRIMARY KEY (`id`),
KEY `id_2` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=75 ;
Toelichting:
id :: de ID van de row, vult hij automatisch in
name :: de naam van de poster (zelf op te geven in een form oid)
text :: de input vanuit een textarea, bedoeld als gastenboek reactie
date/time :: deze heb ik gebruikt om timestamps toe te voegen binnen mijn gastenboek
Deze table is verre van efficient en goed ingericht, ik weet het. Het was een van mijn eerste SQL projectjes, een simpel gastenboek waar je simpel op kon reageren en kon deleten. Ik heb deze tabel opnieuw gebruikt, hier had ik nog een entry of 60 instaan.