Monday, 29 December 2014

Although PHP is not fresh today, it is still there. And php paging is still useful. Let's have a look. There are three portions of this article. To begin with, the result code will be given. To the next part, it is the basic threads of how to do it. The last step is explaining my code.

First thing first, to do paging, create database is basic. And then, the connection file conn.php, paging file paging1.php and css file pagingCSS.css. Named them anything you like, in this case, they named as listed.

Create Database code:
-------------------------------------------------------------------------------------------------------

create database `pagingDB`;

create table tablePages
(
    id bigint(30) unsigned NOT NULL AUTO_INCREMENT,
    name varchar(64) NOT NULL default '' UNIQUE,
    phone varchar(3) NOT NULL default '',
    primary key (id)
)

INSERT INTO `tablePages`(`id`, `name`, `phone`) VALUES (1,'cao',1);
INSERT INTO `tablePages`(`id`, `name`, `phone`) VALUES (2,'ni',2);
INSERT INTO `tablePages`(`id`, `name`, `phone`) VALUES (3,'ma',3);
INSERT INTO `tablePages`(`id`, `name`, `phone`) VALUES (4,'wocao',4);
INSERT INTO `tablePages`(`id`, `name`, `phone`) VALUES (5,'caoni',5);
INSERT INTO `tablePages`(`id`, `name`, `phone`) VALUES (6,'mabi',6);
INSERT INTO `tablePages`(`id`, `name`, `phone`) VALUES (7,'gan',7);
INSERT INTO `tablePages`(`id`, `name`, `phone`) VALUES (8,'niM',8);
INSERT INTO `tablePages`(`id`, `name`, `phone`) VALUES (9,'mabiya',9);
INSERT INTO `tablePages`(`id`, `name`, `phone`) VALUES (10,'caogan',10);
INSERT INTO `tablePages`(`id`, `name`, `phone`) VALUES (11,'diao',11);
INSERT INTO `tablePages`(`id`, `name`, `phone`) VALUES (12,'si',12);
INSERT INTO `tablePages`(`id`, `name`, `phone`) VALUES (13,'nimabi',13);
INSERT INTO `tablePages`(`id`, `name`, `phone`) VALUES (14,'ganya',14);
INSERT INTO `tablePages`(`id`, `name`, `phone`) VALUES (15,'caoya',15);
INSERT INTO `tablePages`(`id`, `name`, `phone`) VALUES (16,'baoru',16);


conn.php
-------------------------------------------------------------------------------------------------------
<?php
            $theUser = 'root';
            $thePsw = ‘111111111’;
            $theDBname = 'pagingDB';
            $pagingConn = mysql_connect('localhost', $theUser, $thePsw) or die("connection Error, ".mysql_error());
            mysql_select_db($theDBname);
?>
-------------------------------------------------------------------------------------------------------

paging1.php
-------------------------------------------------------------------------------------------------------
<?php
            echo "The server name is --- ".$_SERVER['SERVER_NAME'];
            require_once 'conn.php';
            $pagingTBname = 'tablePages';
            $sqlSen = "select count(*) as total from ".$pagingTBname;
            $rst = mysql_query($sqlSen);
            $row = mysql_fetch_array($rst);
            $rowsNum = $row['total'];
            mysql_free_result($rst);

            $sqlAllRecords = "select * from ".$pagingTBname;
            $serverRST = mysql_query($sqlAllRecords);
            /*$rowsNum = mysql_num_rows($serverRST);*/
            $columnsNum = mysql_num_fields($serverRST);
            echo "<br>the rows are --- ".$rowsNum." ||| the fields are: ".$columnsNum.".<br>";
            $j = 0;
            while ($j<$columnsNum)
            {
                        $fields[$j] = mysql_field_name($serverRST, $j);
                        $j++;
            }

            $pageSize = 3;
            $initialPage = 1;
            $pageTotalNum = ceil($rowsNum/$pageSize);
           
            if (empty($_GET['page'])||$_GET['page']<$initialPage)
            {
                        $page = $initialPage;
            }
            else
            {
                        $page = $_GET['page'];
            }
            if ($page >= $pageTotalNum)
            {
                        $page = $pageTotalNum;
            }

            $offsetRecordPosition = $pageSize*($page-1);
            $singlePagesRecordNumSQL = "select * from ".$pagingTBname." limit ".$offsetRecordPosition.",".$pageSize;
            $recordsInOnePage = mysql_query($singlePagesRecordNumSQL);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<html>
            <head>
                        <title>paging1</title>
                        <link rel="stylesheet" type="text/css" href="pagingCSS.css">
            </head>
            <body class="basicbody">
                                    <table cellpadding="0" cellspacing="0" align="center" class="tbStyle" border="1">
                                                <tr>
                                                            <?php
                                                                        reset($fields);
                                                                        while (list(,$field_names) = each($fields))
                                                                        {
                                                            ?>
                                                                        <th>
                                                                                    <?php echo "$field_names"; ?>
                                                                        </th>
                                                            <?php
                                                                        }
                                                            ?>
                                                </tr>
                                    <?php
                                                while ($contentRow = mysql_fetch_array($recordsInOnePage))
                                                {
                                    ?>
                                                <tr>
                                                            <?php
                                                                        for ($i=0; $i < $columnsNum; $i++)
                                                                        {
                                                            ?>
                                                                                    <td><?php echo $contentRow[$i]; ?></td>
                                                            <?php
                                                                        }
                                                            ?>
                                                </tr>
                                    <?php
                                                }
                                    ?>
                                    </table>
                                    <div class="divContainer">
                                                <div class="divSlices">
                                                            <a href="paging1.php?page=1" class="aStyle">firstPage</a>
                                                </div>
                                                <div class="divSlices">
                                                            <a href="paging1.php?page=<?php if ($page>$initialPage) {echo $page-1;} else{echo "$initialPage";} ?>" class="aStyle">previousPage</a>
                                                </div>
                                                <div class="divSlices">
                                                            <!-- <a href="" class="aStyle">1234</a> -->
                                                <?php
                                                            for ($i=1; $i <= $pageTotalNum; $i++)
                                                            {
                                                ?>
                                                            <a href="paging1.php?page=<?php echo $i; ?>" class="aStyle"><?php echo $i." "; ?></a>
                                                <?php
                                                            }
                                                ?>
                                                </div>
                                                <div class="divSlices">
                                                            <a href="paging1.php?page=<?php if ($page<$pageTotalNum) {echo $page+1;} else{echo "$pageTotalNum";} ?>" class="aStyle">nextPage</a>
                                                </div>
                                                <div class="divSlices">
                                                            <a href="paging1.php?page=<?php echo $pageTotalNum; ?>" class="aStyle">lastPage</a>
                                                </div>
                                    </div>
            </body>
            <?php
                        /*mysql_close();*/
            ?>
</html>
-------------------------------------------------------------------------------------------------------


pagingCSS.css
-------------------------------------------------------------------------------------------------------
table.tbStyle
{
            width: 210px;
            text-align: center;
}

a.aStyle
{
            text-decoration: none;
            font-family:"Times New Roman", Times, serif;
            font-size:12px;
            font-weight: bolder;
            text-align:center;
}

div.divContainer
{
            width: 416px; height: 28px;
            position: relative;
            top: 20px;
            margin: 0 auto;
            padding: 0px;
}

div.divSlices
{
            width: 80px; height: 20px;
            margin: 0 auto;
            float: left;
            text-align: center;
            background-color: #E0EEEE;
            line-height: 20px;
}
-------------------------------------------------------------------------------------------------------


In this section, the basic thread about how to do it.
First of all, a database is necessary. Create database and table. Insert contents to the table. Nothing has to be explained.
Followed by connection function as every php website does. Connect to database.
The third file is our paging file that is display the information on webpage. It has paging function. It displays few records from database on one webpage and changes to other pages by clicking button links. As can be seen from the image displayed below. The database contains 17 records; every record has 3 columns; 3 records would be listed in one page. You can do these as you like.


The last one is CSS file. It is very usual, nothing to say.


This is the last segment of this article, which will explain the codes. But the only file that needs to be clarified is paging1.php. Others are too plain.

-------------------------------------------------------------------------------------------------------
//find the server name. Useless, doesn’t have to do it.
echo "The server name is --- ".$_SERVER['SERVER_NAME'];
//connect to database
            require_once 'conn.php';
//the table name
            $pagingTBname = 'tablePages';
//count the total number of records in database(SQL).
            $sqlSen = "select count(*) as total from ".$pagingTBname;
//get result by using $sqlSen;
            $rst = mysql_query($sqlSen);
//get one field array to $row from $rst.
            $row = mysql_fetch_array($rst);
//get sum of records.
            $rowsNum = $row['total'];
//release the source.
            mysql_free_result($rst);

//select every thing from database.
            $sqlAllRecords = "select * from ".$pagingTBname;
//get result.
            $serverRST = mysql_query($sqlAllRecords);
//another way to get the sum of records.
            /*$rowsNum = mysql_num_rows($serverRST);*/
//get sum of columns.
            $columnsNum = mysql_num_fields($serverRST);
//display the sum of records. Display sum of column in one records.
            echo "<br>the rows are --- ".$rowsNum." ||| the fields are: ".$columnsNum.".<br>";
-------------------------------------------------------------------------------------------------------

//set an array $fields to collect all fields names.
$j = 0;
            while ($j<$columnsNum)
            {
                        $fields[$j] = mysql_field_name($serverRST, $j);
                        $j++;
            }

//every page displays 3 records.
            $pageSize = 3;
//the beginning page number is 1. Start from page 1, default is page 0;
            $initialPage = 1;
//how many pages required. 17/3 = 6;
            $pageTotalNum = ceil($rowsNum/$pageSize);
           

//If page number is not set or less than 1, display page 1.
            if (empty($_GET['page'])||$_GET['page']<$initialPage)
            {
                        $page = $initialPage;
            }
//otherwise, get page number.
            else
            {
                        $page = $_GET['page'];
            }
//if page number larger or equal the last page, display the last page.
            if ($page >= $pageTotalNum)
            {
                        $page = $pageTotalNum;
            }
-------------------------------------------------------------------------------------------------------
//what is the first record in each webpage?
$offsetRecordPosition = $pageSize*($page-1);
//the range of records for each webpage.
$singlePagesRecordNumSQL = "select * from ".$pagingTBname." limit ".$offsetRecordPosition.",".$pageSize;
//get result of the range of records for each webpage.
(Although the range in page 6 is 3, but only display 2 records, because only last 2 left.)
$recordsInOnePage = mysql_query($singlePagesRecordNumSQL);


-------------------------------------------------------------------------------------------------------
<table cellpadding="0" cellspacing="0" align="center" class="tbStyle" border="1">
                                                <tr>
                                                            <?php
//set array pointer to head.
                                                                        reset($fields);
//display fields name: id, name, phone from database.
                                                                        while (list(,$field_names) = each($fields))
                                                                        {
                                                            ?>
                                                                        <th>
                                                                                    <?php echo "$field_names"; ?>
                                                                        </th>
                                                            <?php
                                                                        }
                                                            ?>
                                                </tr>
                                    <?php
//display different records in respective webpage.
                                                while ($contentRow = mysql_fetch_array($recordsInOnePage))
                                                {
                                    ?>
                                                <tr>
                                                            <?php
//display information ordered by column name.
                                                                        for ($i=0; $i < $columnsNum; $i++)
                                                                        {
                                                            ?>
                                                                                    <td><?php echo $contentRow[$i]; ?></td>
                                                            <?php
                                                                        }
                                                            ?>
                                                </tr>
                                    <?php
                                                }
                                    ?>
                                    </table>

-------------------------------------------------------------------------------------------------------
<div class="divContainer">
//all button are actual links.
            <div class="divSlices">
//linke to webpage itself, and then check the page number.
                        <a href="paging1.php?page=1" class="aStyle">firstPage</a>
</div>
<div class="divSlices">
//if page number larger than 1, the previousPage button will works. The previous page is current page-1.
                        <a href="paging1.php?page=<?php if ($page>$initialPage) {echo $page-1;} else{echo "$initialPage";} ?>" class="aStyle">previousPage</a>
</div>
<div class="divSlices">
            <?php
                        for ($i=1; $i <= $pageTotalNum; $i++)
                        {
                        ?>
            <a href="paging1.php?page=<?php echo $i; ?>" class="aStyle"><?php echo $i." "; ?></a>
<?php
                        }
            ?>
</div>
<div class="divSlices">
                        <a href="paging1.php?page=<?php if ($page<$pageTotalNum) {echo $page+1;} else{echo "$pageTotalNum";} ?>" class="aStyle">nextPage</a>
</div>
<div class="divSlices">
            <a href="paging1.php?page=<?php echo $pageTotalNum; ?>" class="aStyle">lastPage</a>
</div>
</div>

-------------------------------------------------------------------------------------------------------


No comments:

Post a Comment