复杂检索数据并分页显示的处理方法

1276次阅读  |  发布于5年以前

系统标题:复杂检索数据并分页显示的处理方法
系统功能:利用临时表检索数据库数据,然后分页显示的方法:
处理方法:采用临时表存放数据中间结果,根据中间结果显示数据
数据的显示采用隔行的方式处理
处理优点:对于复杂的查询,特别是涉及到多表的数据查询,如果直接使用查询条件,系统的
开销将很大,利用临时表把数据先保存,然后处理。这样对数据库的查询只要开销一次。
使用方法:只要把连接数据库的用户信息和数据表改变即可使用

<?
//连接数据库
$dbh = mysql_connect('localhost:3306','root','');
mysql_select_db('test');

//把数据检索的结果保存到临时表中
$ls_sql = ' create temporary table temps ';
$ls_sql .= ' select lk_title,lk_link from lk_t_content ';
$ls_sql .= " where lk_title like '%".$searchcontent."%' ";
$res = mysql_query($ls_sql, $dbh);

//得到检索数据的总数
$ls_sql = 'select count(*) as rcnt_con from temps ';
$res = mysql_query($ls_sql, $dbh);
$rcon = $row["rcnt_con"];

$pages=ceil($rcon / 20); //$pages变量现在总的页数
if (empty($offset)) {
$offset=1;
$curline = 0;
} else
$curline = ($offset - 1) * 20;
//打印表头
print '

';
print '';
print '';
print "
';
if ($offset <> 1) { //如果偏移量是0,不显示前一页的链接
$newoffset=$offset - 1;
print "前一页";
} else {
print "前一页";
print " ";
}
//显示所有的页数
for ($i=1; $i <= $pages; $i++) {
$temps = "<a href='".$PHP_SELF.'?offset='.$i."'>".$i."";
print $temps;
print " ";
}
//检查是否是最后一页
if ($pages!=0 && $offset!=$pages) {
$newoffset=$offset+1;
print "下一页";
} else print "下一页";
print '
';
print "当前页:".$offset." 共".$pages."页";
print '
";

//显示查询信息
print '

';
print ' ';
print '';
print '';

$query = "select lk_title,lk_link from temps order by lk_title desc LIMIT ".$curline.",20";
$res = mysql_query($query, $dbh);

$li_num = 0;
while ($row = mysql_fetch_array($res)) {
//采用隔行显示的方法显示信息内容
if ($li_number == 0) {

$li_number = 1; } else { $li_number = 0; } $tempstr = "".$row['lk_title'].""; print ''; print ''; } print "
查询结果信息
'.$tempstr.'
"; ?> \---------------------------- 欢迎访问:zhangcg.oso.com.cn

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8