PHP MySQL Pagination
It very easy and simple to Pagination Multiple row from MySQL database using php.![]() |
PHP MySQL Pagination |
Overview and Structure:
You will need 3 files.1. connect.php [To Established Connection with Database]
2. index.php [Displaying Data with pagination]
3. style.css [styling our Index page]
Save all files in same folder.
![]() |
PHP MySQL Simple Pagination |
Database Structure:
![]() |
PHP MySQL Simple Pagination |
Database Name: codenair
Table Name: pagination
Creating Table "pagination"
1 2 3 4 5 6 | CREATE TABLE IF NOT EXISTS `pagination` ( `id` int (11) NOT NULL AUTO_INCREMENT, ` name ` varchar (150) NOT NULL , `email` varchar (150) NOT NULL , PRIMARY KEY (id) ) ENGINE=MyISAM AUTO_INCREMENT=15 DEFAULT CHARSET=latin1; |
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 | INSERT INTO `pagination` (`id`, ` name `, `email`) VALUES ( NULL , 'ANJANBD' , 'Anjankumardhali6@gmail.com' ), ( NULL , 'Krishna' , 'krishna@mail.com' ), ( NULL , 'Rupam Mondal' , 'RupomMondal@gmail.com' ), ( NULL , 'ANJAN' , 'KumarANjan@gmail.com' ), ( NULL , 'Biswas Shiuli' , 'ShiuliBiswas@gmail.com' ), ( NULL , 'Hrithik' , 'Hrithik@gmail.com' ), ( NULL , 'Modhu Bonik' , 'Modhu360@gmail.com' ), ( NULL , 'Rupam Mondal Joy' , 'RupomMondal@gmail.com' ), ( NULL , 'Rupa' , 'Rupa@hotmail.com' ), ( NULL , 'ANJAN' , 'Kumar@gmail.com' ), ( NULL , 'ANJANBD' , 'Anjankumardhali6@gmail.com' ), ( NULL , 'Krishna' , 'krishna@mail.com' ), ( NULL , 'Rupam Mondal' , 'RupomMondal@gmail.com' ), ( NULL , 'ANJAN' , 'KumarANjan@gmail.com' ), ( NULL , 'Biswas Shiuli' , 'ShiuliBiswas@gmail.com' ), ( NULL , 'Hrithik' , 'Hrithik@gmail.com' ), ( NULL , 'Modhu Bonik' , 'Modhu360@gmail.com' ), ( NULL , 'Rupam Mondal Joy' , 'RupomMondal@gmail.com' ), ( NULL , 'Rupa' , 'Rupa@hotmail.com' ), ( NULL , 'ANJAN' , 'Kumar@gmail.com' ), ( NULL , 'ANJANBD' , 'Anjankumardhali6@gmail.com' ), ( NULL , 'ANJANBD60' , 'Anjankumardhali6@gmail.comss' ), ( NULL , 'Priyoshi' , 'Priyoshi@gmail.com' ), ( NULL , 'Rupom Chakma' , 'Rupam@gmail.com' ), ( NULL , 'Monisha Roy' , 'Monisharoy@gmail.com' ); |
Now copy the below codes for different files..
connect.php
You have to create connect.php file to create connection between your database and server..
1 2 3 4 5 6 7 8 9 10 11 12 | <?php $host = "localhost" ; //Host Name $username = "root" ; //MySQL UserName $password = "" ; //MySQL Password $dbname = "codenair" ; //Database Name //Creating Connection $conn = new mysqli( $host , $username , $password , $dbname ); //Generating Error if ( $conn ->connect_error){ die ( "connection Failed" . $conn ->connect_error); } ?> |
index.php
Now You have to create index.php file to display data from database with limit and pagination...
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 | <?php include ( "connect.php" ); $perpage =5; //5 Articles per page if (isset( $_GET [ 'page' ])){ $page = intval ( $_GET [ 'page' ]); } else { $page =1; } $cal = $page * $perpage ; //Calculation Total Page $start = $cal - $perpage ; $result =mysqli_query( $conn , "SELECT*FROM pagination ORDER BY id DESC LIMIT $start,$perpage" ); ?> <html> <head> <title>PHP MySQL Pagination</title> <link rel= "stylesheet" href= "style.css" type= "text/css" /> </head> <table> <tr> <th>ID</th> <th>Name</th> <th>Email</th> </tr> <?php while ( $row = $result ->fetch_array()){?> <tr> <td><?php echo $row [ 'id' ];?></td> <td><?php echo $row [ 'name' ];?></td> <td><?php echo $row [ 'email' ];?></td> </tr> <?php }?> </table> <?php if (isset( $page )){ //Counting Total rows from pagination table.. $result =mysqli_query( $conn , "select count(*)as total from pagination" ); $rows = $result ->num_rows; if ( $rows ){ $rs = $result ->fetch_assoc(); $total = $rs [ 'total' ]; } //Getting total pages ..total rows/perpage $totalPages = ceil ( $total / $perpage ); if ( $page <=1){ echo "<ul>" ; echo "<li><a href=# class=active>No More page</a></li>" ; } else { $j = $page -1; echo '<ul>' ; echo "<li><a href='index.php?page=$j'>Preview</a><li>" ; } for ( $i =1; $i <= $totalPages ; $i ++){ if ( $i <> $page ) { echo "<li><a href='index.php?page=$i'>$i</a><li>" ; } else { echo "<li><a href=# class=active>" . $i . "</a></li>" ; } } if ( $page == $totalPages ){ echo "<li><a href=# class=active>No More page</a></li>" ; } else { $front = $page +1; echo "<li><a href='index.php?page=$front'>Next</a></li>" ; echo "</ul>" ; } } ?> </html> |
style.css
You have to create style.css page to design pagination links and tables..
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 | table th,td{ border : 1px solid green ; text-align : left ; padding : 8px ; } table{ border-collapse : collapse ; width : 50% ; } th{ padding : 10px ; font-size : 16px ; font-weight : bold ; background : black ; color : white ; } tr:nth-child(odd){ background : #0088CC ; color : white ; } ul li{ float : left ; display : block ; } ul li a{ text-decoration : none ; font-size : 20px ; display : block ; padding : 4px 10px ; margin-left : 2px ; margin-top : 4px ; border-radius: 4px ; background : #0088cc ; color : white ; } .active{ cursor :not-allowed; background : #ccc ; color : red ; } |
that's it how to create php MySQL pagination.. I hope you Enjoy This article.Please Like and Share with your Friends.
and Keep Visiting for More Codes.Thank you.
No comments:
Post a Comment
Thank You for Your Comment
Note: Only a member of this blog may post a comment.