01.
<html>
02.
<head>
03.
<title>ThaiCreate.Com PHP & SQL Server Tutorial</title>
04.
</head>
05.
<body>
06.
<?php
07.
$objConnect
= mssql_connect(
"localhost"
,
"sa"
,
""
)
or
die
(
"Error Connect to Database"
);
08.
$objDB
= mssql_select_db(
"mydatabase"
);
09.
$strSQL
=
"SELECT * FROM customer ORDER BY CustomerID ASC"
;
10.
$objQuery
= mssql_query(
$strSQL
)
or
die
(
"Error Query ["
.
$strSQL
.
"]"
);
11.
$Num_Rows
= mssql_num_rows(
$objQuery
);
12.
13.
$Per_Page
= 2;
14.
15.
$Page
=
$_GET
[
"Page"
];
16.
if
(!
$_GET
[
"Page"
])
17.
{
18.
$Page
=1;
19.
}
20.
21.
$Prev_Page
=
$Page
-1;
22.
$Next_Page
=
$Page
+1;
23.
24.
$Page_Start
= ((
$Per_Page
*
$Page
)-
$Per_Page
);
25.
if
(
$Num_Rows
<=
$Per_Page
)
26.
{
27.
$Num_Pages
=1;
28.
}
29.
else
if
((
$Num_Rows
%
$Per_Page
)==0)
30.
{
31.
$Num_Pages
=(
$Num_Rows
/
$Per_Page
) ;
32.
}
33.
else
34.
{
35.
$Num_Pages
=(
$Num_Rows
/
$Per_Page
)+1;
36.
$Num_Pages
= (int)
$Num_Pages
;
37.
}
38.
$Page_End
=
$Per_Page
*
$Page
;
39.
IF (
$Page_End
>
$Num_Rows
)
40.
{
41.
$Page_End
=
$Num_Rows
;
42.
}
43.
44.
?>
45.
<table width=
"600"
border=
"1"
>
46.
<tr>
47.
<th width=
"91"
> <div align=
"center"
>CustomerID </div></th>
48.
<th width=
"98"
> <div align=
"center"
>Name </div></th>
49.
<th width=
"198"
> <div align=
"center"
>Email </div></th>
50.
<th width=
"97"
> <div align=
"center"
>CountryCode </div></th>
51.
<th width=
"59"
> <div align=
"center"
>Budget </div></th>
52.
<th width=
"71"
> <div align=
"center"
>Used </div></th>
53.
</tr>
54.
<?php
55.
for
(
$i
=
$Page_Start
;
$i
<
$Page_End
;
$i
++)
56.
{
57.
?>
58.
<tr>
59.
<td><div align=
"center"
><?php
echo
mssql_result(
$objQuery
,
$i
,
"CustomerID"
);?></div></td>
60.
<td><?php
echo
mssql_result(
$objQuery
,
$i
,
"Name"
);?></td>
61.
<td><?php
echo
mssql_result(
$objQuery
,
$i
,
"Email"
);?></td>
62.
<td><div align=
"center"
><?php
echo
mssql_result(
$objQuery
,
$i
,
"CountryCode"
);?></div></td>
63.
<td align=
"right"
><?php
echo
mssql_result(
$objQuery
,
$i
,
"Budget"
);?></td>
64.
<td align=
"right"
><?php
echo
mssql_result(
$objQuery
,
$i
,
"Used"
);?></td>
65.
</tr>
66.
<?php
67.
}
68.
?>
69.
</table>
70.
71.
<br>
72.
Total <?php
echo
$Num_Rows
;?> Record : <?php
echo
$Num_Pages
;?> Page :
73.
<?php
74.
if
(
$Prev_Page
)
75.
{
76.
echo
" <a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page'><< Back</a> "
;
77.
}
78.
79.
for
(
$i
=1;
$i
<=
$Num_Pages
;
$i
++){
80.
if
(
$i
!=
$Page
)
81.
{
82.
echo
"[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i'>$i</a> ]"
;
83.
}
84.
else
85.
{
86.
echo
"<b> $i </b>"
;
87.
}
88.
}
89.
if
(
$Page
!=
$Num_Pages
)
90.
{
91.
echo
" <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page'>Next>></a> "
;
92.
}
93.
mssql_close(
$objConnect
);
94.
?>
95.
</body>
96.
</html>