<?php
$con = mysql_connect("localhost","username","password");
mysql_query("set character set 'utf8'");//读库
mysql_query("set names 'utf8'");//写库
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("databasename", $con);
?>
输入关键词
<?php
header("Content-type: text/html; charset=utf-8");
include'conn.php';
$keyword=$_GET['keyword'];
if(!empty($keyword)){
$result =mysql_query( "select * from tablename where xxx like '%$keyword%' ");
}
else {
$result =mysql_query( "select * from tablename ");
}
$count = mysql_num_rows($result);
echo $count;
$Page_size=100;
$page_count = ceil($count/$Page_size);
$init=1;
$page_len=9;
$max_p=$page_count;
$pages=$page_count;
//判断当前页码
if(empty($_GET['page'])||$_GET['page']<0){
$page=1;
}else {
$page=$_GET['page'];
}
$offset=$Page_size*($page-1);
if(!empty($keyword)){
$sql="select * from tablename where xxx like '%$keyword%' limit $offset,$Page_size";
}
else {
$sql="select * from tablename limit $offset,$Page_size";
}
$result=mysql_query($sql,$con);
echo "";
echo "";
echo "axxx " ;
echo "bxxx " ;
echo "cxxx " ;
echo "";
while($row = mysql_fetch_array($result))
{
echo "";
echo " " .$row['a']."" ;
echo " " .$row['b']."";
echo " " .$row['c']."";
echo "";
}
echo "";
$page_len = ($page_len%2)?$page_len:$pagelen+1;//页码个数
$pageoffset = ($page_len-1)/2;//页码个数左右偏移量
$key.='';
$key.="$page/$pages "; //第几页,共几页
if($page!=1){
$key.="第一页 "; //第一页
$key.="上一页"; //上一页
}else {
$key.="第一页 ";//第一页
$key.="上一页"; //上一页
}
if($pages>$page_len){
//如果当前页小于等于左偏移
if($page<=$pageoffset){
$init=1;
$max_p = $page_len;
}else{//如果当前页大于左偏移
//如果当前页码右偏移超出最大分页数
if($page+$pageoffset>=$pages+1){
$init = $pages-$page_len+1;
}else{
//左右偏移都存在时的计算
$init = $page-$pageoffset;
$max_p = $page+$pageoffset;
}
}
}
for($i=$init;$i<=$max_p;$i++){
if($i==$page){
$key.=' '.$i.'';
} else {
$key.=" ".$i."";
}
}
if($page!=$pages){
$key.=" 下一页 ";//下一页
$key.="最后一页"; //最后一页
}else {
$key.="下一页";//下一页
$key.="最后一页"; //最后一页
}
$key.='';
echo "";
echo $key;
mysql_close($con);
?>