晓夏

北漂的女孩

Good Luck To You!

PHP生成分页类

浏览量:406

/**
 * 生成分页的字符串,页码的url参数如下: &p=1,2,3
 *
 * @param int $rows 总记录数
 * @param int $currentpage  当前第几页
 * @param string $href  链接字符串
 * @param int $pagesize 每页几条
 * @return  a string of pages.  页码数
 * @return lianghuiju
 */
function get_pages_string($rows, $current_page, $href_string, $page_size=20, $page_numbers=10, $id='')
{
   //
   $pages = intval(($rows / $page_size));
   if($rows % $page_size > 0)
   {
      $pages ++;
   }
   $start = intval($current_page / $page_numbers) * $page_numbers + 1 ;
   if($current_page % $page_numbers == 0 && $current_page > 0)
   {
      $start = $current_page - $page_numbers + 1;
   }
   if($start <= 0)
   {
      $start = 1;
   }
   $end = $start + $page_numbers -1;
   if($end > $pages)
   {
      $end = $pages;
   }
   if($href_string == '')
   {
      $href_string = $_SERVER['PHP_SELF'] . '?';
      $params = $_SERVER['QUERY_STRING'];
      $params = str_replace('&amp;', '&', $params);
      $params_array = explode('&', $params);
      foreach($params_array as $param)
      {
         $index = strpos($param, '=');
         $key = substr($param, 0, $index);
         if($key != 'p')
         {
            $href_string .= $param . '&';
         }
      }
      $href_string = rtrim($href_string,'&');
   }
   $str = "";
   if($current_page > $page_numbers)
   {
      $prev_page = $current_page - $page_numbers ;
      $str .= "<a href='$href_string&p=$prev_page' title='前 $page_numbers 页'> [...] </a> ";
   }
   for($i=$start;$i<=$end;$i++)
   {
      if($i == $current_page)
      $str .= "<font color='red'>$i </font>";
      else
      $str .= "<a href='$href_string&p=$i' title='第 $i 页'>[$i]</a> ";
   }
   if($end < $pages)
   {
      $next_page = $current_page + $page_numbers ;
      if($next_page > $pages)
      {
         $next_page = $pages;
      }
      $str .= "<a href='$href_string&p=$next_page' title='后 $page_numbers 页'>[...]</a> ";
   }
   $n_page =   $current_page+1;
   $p_page =   $current_page-1;
   if($p_page  ==  0)
   {
      $p_page =   1;
   }
   $str .= "&nbsp;<a href='$href_string&p=$p_page' title='上一页'>上一页</a> ";
   $str .= "<a href='$href_string&p=$n_page' title='下一页'>下一页</a>";

   // 输入页码跳转,以及页面一些统计信息
   $str .= "<script language=\"javascript\">
   function next_page".$id."(url)
   {
   var c = document.getElementById('page".$id."');
   url = url + '&p=' + c.value;
   window.location.href = url;
}
</script>
<input type=\"text\" style=\"width:30px\" name=\"page\" id=\"page".$id."\" />

<input type=\"button\" value=\"Go\" style=\"width:35px\" onclick=\"next_page".$id."('".$href_string."')\"/>";
$str .= <<<EOF
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
当前是第<font color="red">{$current_page}</font>页,
共有<font color="red">{$pages}</font>页,
<font color="red">{$rows}</font>条记录
EOF;
return $str;
}


神回复

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。