• You must know how to connect to the DB.
• You have created a table and the table contains informations to wait ... paging
You create a file list.php has a complete code as follows:
• The syntax of this passage is:
Page = check variable page exists or not [?]
If exists, get the value of this variable and use the function filter intval to receive safe values [:]
If not then will have the default value is 1:
Getting [1] divided by [2] It will be several pages
To rounding values from (x.1 -> x.9 of 1 + x) we use the function ceil ()
<?php $page = isset ( $_GET["page"] ) ? intval ( $_GET["page"] ) : 1;
$rows_per_page = 20;
$page_start = ( $page - 1 ) * $rows_per_page;
$page_end = $page * $rows_per_page;
$sql_query = mysql_query("SELECT * FROM table_name");
$number_of_page = ceil ( mysql_num_rows( $sql_query ) / $rows_per_page );
if ( $number_of_page > 1 )
{
$list_page = " <td> Page: </td>";
for ( $i = 1; $i <= $number_of_page; $i++ )
{
if ( $i == $page )
{
$list_page .= " <td>[ <b>{$i}</b> ]</td> ";
}
else
{
$list_page .= "<td><a href=’list.php?page={$i}’> {$i} </a></td>";
}
}
}
$i = 0;
while ( $result = mysql_fetch_array ( $sql_query ) )
{
if ( $i >= $page_start )
{
print "";
}
$i++;
if ($i >= $page_end)
{
break;
}
}
print <<<EOF
<table cellspacing="0" cellpadding="0" border="0">
<tr>
{$list_page}
</tr>
</table>
EOF;
?>
Explain each paragraph:• The syntax of this passage is:
Page = check variable page exists or not [?]
If exists, get the value of this variable and use the function filter intval to receive safe values [:]
If not then will have the default value is 1:
$page=isset($_GET["page"])?intval($_GET["page"]):1;• Numbers of line on a page:
$rows_per_page = 20;• Count the number of lines in the opening page based on variable $page:
$page_start=($page-1)*$rows_per_page;• Count the number of lines in the last page based on variable $page:
$page_end=$page*$rows_per_page;Example of the opening page and last page according to the above formula:
Suppose the page being viewed is 1, we are
$page_start=(1-1)*20=0
And
• Retrieve into table_name: $page_start = (1 - 1) * 20 = 0So we have a value 0 is begin and 20 is end.
$sql_query = mysql_query("SELECT * FROM table_name");
• Count the numbers of page, do by following: mysql_num_rows($sql_query);is the table's total number of lines have retrieved [1]
$rows_per_pageis equal to 20 ( above value that we have set) [2]
Getting [1] divided by [2] It will be several pages
To rounding values from (x.1 -> x.9 of 1 + x) we use the function ceil ()
$number_of_page=ceil( mysql_num_rows( $sql_query )/$rows_per_page);
• If the page number is greater than 1 then it will list the pages
$i = 0;
• List all lines in a table.
if ( $number_of_page > 1 )
{
$list_page = "<td> Page: </td>";
for ( $i = 1; $i <= $number_of_page; $i++ )
{
if ( $i == $page )
{
$list_page .= "<td> [ <b> {$i} </b>] </td>";
}
else
{
$list_page .= "<td><a href=’list.php?page={$i}’> {$i} </a></td>";
}
}
}
• Set variable $i equal zero.$i = 0;
• List all lines in a table.
while ( $result = mysql_fetch_array ( $sql_query ) )
{if ( $i >= $page_start )
{
print $result[content];
} $i++;
if ($i >= $page_end)
{
break;
}
}
• Above we loaded content to variable $list _ page, now we print it.
print <<<EOF
<table cellspacing="0" cellpadding="0" border="0">
<tr>
{$list_page}
</tr>
</table>
EOF
Translate from Admin's post <sinhvienit.net>


0 comments:
Post a Comment