1
2<?php
3
4$connect = new PDO("mysql:host=localhost; dbname=testing", "root", "");
5
6/*function get_total_row($connect)
7{
8 $query = "
9 SELECT * FROM tbl_webslesson_post
10 ";
11 $statement = $connect->prepare($query);
12 $statement->execute();
13 return $statement->rowCount();
14}
15
16$total_record = get_total_row($connect);*/
17
18$limit = '5';
19$page = 1;
20if($_POST['page'] > 1)
21{
22 $start = (($_POST['page'] - 1) * $limit);
23 $page = $_POST['page'];
24}
25else
26{
27 $start = 0;
28}
29
30$query = "
31SELECT * FROM tbl_webslesson_post
32";
33
34if($_POST['query'] != '')
35{
36 $query .= '
37 WHERE webslesson_post_title LIKE "%'.str_replace(' ', '%', $_POST['query']).'%"
38 ';
39}
40
41$query .= 'ORDER BY webslesson_post_id ASC ';
42
43$filter_query = $query . 'LIMIT '.$start.', '.$limit.'';
44
45$statement = $connect->prepare($query);
46$statement->execute();
47$total_data = $statement->rowCount();
48
49$statement = $connect->prepare($filter_query);
50$statement->execute();
51$result = $statement->fetchAll();
52$total_filter_data = $statement->rowCount();
53
54$output = '
55<label>Total Records - '.$total_data.'</label>
56<table class="table table-striped table-bordered">
57 <tr>
58 <th>ID</th>
59 <th>Post Title</th>
60 </tr>
61';
62if($total_data > 0)
63{
64 foreach($result as $row)
65 {
66 $output .= '
67 <tr>
68 <td>'.$row["webslesson_post_id"].'</td>
69 <td>'.$row["webslesson_post_title"].'</td>
70 </tr>
71 ';
72 }
73}
74else
75{
76 $output .= '
77 <tr>
78 <td colspan="2" align="center">No Data Found</td>
79 </tr>
80 ';
81}
82
83$output .= '
84</table>
85<br />
86<div align="center">
87 <ul class="pagination">
88';
89
90$total_links = ceil($total_data/$limit);
91$previous_link = '';
92$next_link = '';
93$page_link = '';
94
95//echo $total_links;
96
97if($total_links > 4)
98{
99 if($page < 5)
100 {
101 for($count = 1; $count <= 5; $count++)
102 {
103 $page_array[] = $count;
104 }
105 $page_array[] = '...';
106 $page_array[] = $total_links;
107 }
108 else
109 {
110 $end_limit = $total_links - 5;
111 if($page > $end_limit)
112 {
113 $page_array[] = 1;
114 $page_array[] = '...';
115 for($count = $end_limit; $count <= $total_links; $count++)
116 {
117 $page_array[] = $count;
118 }
119 }
120 else
121 {
122 $page_array[] = 1;
123 $page_array[] = '...';
124 for($count = $page - 1; $count <= $page + 1; $count++)
125 {
126 $page_array[] = $count;
127 }
128 $page_array[] = '...';
129 $page_array[] = $total_links;
130 }
131 }
132}
133else
134{
135 for($count = 1; $count <= $total_links; $count++)
136 {
137 $page_array[] = $count;
138 }
139}
140
141for($count = 0; $count < count($page_array); $count++)
142{
143 if($page == $page_array[$count])
144 {
145 $page_link .= '
146 <li class="page-item active">
147 <a class="page-link" href="#">'.$page_array[$count].' <span class="sr-only">(current)</span></a>
148 </li>
149 ';
150
151 $previous_id = $page_array[$count] - 1;
152 if($previous_id > 0)
153 {
154 $previous_link = '<li class="page-item"><a class="page-link" href="javascript:void(0)" data-page_number="'.$previous_id.'">Previous</a></li>';
155 }
156 else
157 {
158 $previous_link = '
159 <li class="page-item disabled">
160 <a class="page-link" href="#">Previous</a>
161 </li>
162 ';
163 }
164 $next_id = $page_array[$count] + 1;
165 if($next_id >= $total_links)
166 {
167 $next_link = '
168 <li class="page-item disabled">
169 <a class="page-link" href="#">Next</a>
170 </li>
171 ';
172 }
173 else
174 {
175 $next_link = '<li class="page-item"><a class="page-link" href="javascript:void(0)" data-page_number="'.$next_id.'">Next</a></li>';
176 }
177 }
178 else
179 {
180 if($page_array[$count] == '...')
181 {
182 $page_link .= '
183 <li class="page-item disabled">
184 <a class="page-link" href="#">...</a>
185 </li>
186 ';
187 }
188 else
189 {
190 $page_link .= '
191 <li class="page-item"><a class="page-link" href="javascript:void(0)" data-page_number="'.$page_array[$count].'">'.$page_array[$count].'</a></li>
192 ';
193 }
194 }
195}
196
197$output .= $previous_link . $page_link . $next_link;
198$output .= '
199 </ul>
200
201</div>
202';
203
204echo $output;
205
206?>
207
208