-
Notifications
You must be signed in to change notification settings - Fork 6
/
display_paged.php
144 lines (122 loc) · 3.42 KB
/
display_paged.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/*
// Program : Display a paged list of records from a MySQL table
// Author : Ap.Muthu <apmuthu@usa.net>
// Version : 1.0
// Release Date : 2015-08-06
// Example Usage : http://DOMAIN.TLD/PATH/display_paged.php
// Form POST var : recid
// Usage Notes : $encstr is used for pseudo salting the GET filter recid value
*/
// References:
// http://www.phpjabbers.com/php--mysql-select-data-and-split-on-pages-php25.html
// http://www.w3schools.com/css/tryit.asp?filename=trycss_table_fancy
/*
// Sample table schema:
CREATE TABLE `mytable` (
`id` INT(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`datefield` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Entry TimeStamp',
`txtfield1` VARCHAR(15) DEFAULT NULL COMMENT 'Field 1',
`txtfield2` TEXT COMMENT 'Field 2',
PRIMARY KEY (`id`)
) ENGINE=MYISAM COMMENT='My Table';
*/
// constants
$db_host = 'localhost';
$db_user = 'dbuser';
$db_pass = 'secret123';
$db_name = 'mydb';
$limit = 20;
$encstr = "abc";
// Incoming and Computed variables
//$_POST["recid"] = '1';
$idval = '';
if (isset($_POST["recid"])) {
$recidsize = strlen($_POST['recid']);
if ($recidsize > 0 && $recid+0 > 0) $idval = $_POST[recid];
else exit;
}
elseif (isset($_GET["recid"])) $idval = substr(base64_decode($_GET["recid"], true), strlen($encstr));
else exit;
$m = base64_encode($encstr . $idval);
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $limit;
$sql = "SELECT id, datefield, txtfield1, txtfield2 FROM mytable
WHERE id = '$idval')
ORDER BY datefield DESC
LIMIT $start_from, $limit";
$sql_total = "SELECT COUNT(*) AS recs FROM mytable WHERE id = '$idval'";
// db connection
$link = mysql_connect($db_host, $db_user, $db_pass);
if (!$link) { die('Could not connect: ' . mysql_error()); }
// use db
$db_selected = mysql_select_db($db_name, $link);
if (!$db_selected) { die ("Can't use $db_name : " . mysql_error()); }
// get records to display
$rs_result = mysql_query ($sql, $link);
?>
<style>
#records {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
width: 100%;
border-collapse: collapse;
}
#records td, #records th {
font-size: 1em;
border: 1px solid #98bf21;
padding: 3px 7px 2px 7px;
}
#records th {
font-size: 1.1em;
text-align: left;
padding-top: 5px;
padding-bottom: 4px;
background-color: #A7C942;
color: #ffffff;
}
#records tr.alt td {
color: #000000;
background-color: #EAF2D3;
}
</style>
<table>
<tr align="center"><td><b><u>Records for: <?php echo $idval; ?></u></b></td></tr>
<tr align="center"><td>
<table id="records">
<tr>
<th>ID</th>
<th>Entry TimeStamp</th>
<th>Field 1</th>
<th>Field 2</th>
</tr>
<?php
while ($row = mysql_fetch_assoc($rs_result)) {
if ($i%2) echo '<tr class="alt">';
else echo '<tr>';
?>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['datefield']; ?></td>
<td><?php echo $row['txtfield1']; ?></td>
<td><?php echo $row['txtfield2']; ?></td>
</tr>
<?php
$i++;
};
?>
</table>
<?php
// pagination
$rs_result = mysql_query($sql_total, $link);
$row = mysql_fetch_row($rs_result);
$total_records = $row[0];
$total_pages = ceil($total_records / $limit);
$pagename = $_SERVER['PHP_SELF'];
for ($i=1; $i<=$total_pages; $i++) {
if ($total_pages > 1) {
if ($page == $i) echo "<b>".$i."</b> ";
else echo "<a href='$pagename?recid=$m&page=".$i."'>".$i."</a> ";
}
}
?>
</td></tr>
</table>