Home >Backend Development >PHP Tutorial >How to handle complex data retrieval and page display in PHP

How to handle complex data retrieval and page display in PHP

WBOY
WBOYOriginal
2016-08-08 09:34:081199browse

系统功能:利用临时表检索数据库数据,然后分页显示的方法:
处理方法:采用临时表存放数据中间结果,根据中间结果显示数据
          数据的显示采用隔行的方式处理
处理优点:对于复杂的查询,特别是涉及到多表的数据查询,如果直接使用查询条件,系统的
          开销将很大,利用临时表把数据先保存,然后处理。这样对数据库的查询只要开销一次。
使用方法:只要把连接数据库的用户信息和数据表改变即可使用
        
//连接数据库
$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 = "".$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)) {
//Use interlaced display method to display information content
If ($li_number == 0) {
           
$li_number = 1;
} else {
          
$li_number = 0;
}
$tempstr = "".$row['lk_title']."";
Print '';
​​​print '';
}
Print "
  
查询结果信息
 '.$tempstr.'
";
?>

The above introduces the processing method of complex data retrieval and paging display in PHP, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn