php对xml数据分页 php分页查询mysql

在php中要显示txt中的内容,但是,txt中的内容很多,要分页来显示,怎样做?。扛龈龃敕独梢月穑啃恍?/h2>下面是最简单的方法:
$str=file_get_contents('a.txt');//获取文件所有内容
$page_size=1024;//每页文字多少
$page_no=5;//需要显示第几页
echo substr($str, ($pahe_no-1)*$page_size, $page_size);//输出相应页的内容
当然,上面的例子只是演示其原理,事实上比较复杂,比如这样可能把汉字分为两半而产生乱码,如果内容里面有HTML代码的分断后可能出现异常 。这些问题的解决非常麻烦 。
请问,xml,可以分页吗?谢谢!利用FLASH as3做php对xml数据分页的分页的功能php对xml数据分页,加载XMLphp对xml数据分页,在XML中修改数据php对xml数据分页,这里面的重点就是结合XMLphp对xml数据分页,难点就是分页时数据读取后,进行拆分 。根据XML的长度再进行分页读取,加载图片这些都比较简单
通过AS3加载XML,在XML中修改图片连接地址等,可随意修改数据 。。。
PHP显示很多数据库信息,如何自动分页呢?代码PHP代码如下:
/*
Author:默默
Date :2006-12-03
*/
$page=isset($_GET['page'])?intval($_GET['page']):1; //这句就是获取page=18中的page的值,假如不存在page,那么页数就是1 。
$num=10; //每页显示10条数据
$db=mysql_connect("host","name","pass"); //创建数据库连接
$select=mysql_select_db("db",$db); //选择要操作的数据库
/*
首先咱们要获取数据库中到底有多少数据 , 才能判断具体要分多少页 , 具体的公式就是
总数据数除以每页显示的条数,有余进一 。
也就是说10/3=3.3333=4 有余数就要进一 。
*/
$total=mysql_num_rows(mysql_query("select id from table")); //查询数据的总数,id是数据库中的一个自动赋值的字段
$pagenum=ceil($total/$num); //获得总页数
//假如传入的页数参数大于总页数,则显示错误信息
If($page$pagenum || $page == 0){
Echo "Error : Can Not Found The page .";
Exit;
}
$offset=($page-1)*$num; //获取limit的第一个参数的值 , 假如第一页则为(1-1)*10=0,第二页为(2-1)*10=10 。
$info=mysql_query("select name from table limit $offset,$num"); //获取相应页数所需要显示的数据,name是数据里的一个字段
While($it=mysql_fetch_array($info)){
Echo $it['name']."
";
} //显示数据
For($i=1;$i=$pagenum;$i){
$show=($i!=$page)?"$i":"$i";
Echo $show." ";
}
/*显示分页信息,假如是当页则显示粗体的数字,其余的页数则为超连接,假如当前为第三页则显示如下
1 2 3 4 5 6
*/
?
在php中如何对多条记录进行分页方法一:讲sql查询进行分页进行,需要调用几个函数,具体见脚本:
1.pager.class.php
?php
class pager {
public $sql; //SQL查询语句
public $datanum; //查询所有的数据总记录数
public $page_size; //每页显示记录的条数
protected $_errstr;
protected $_conn;
protected $_query_id;
public function query($query)///这个函数有问题,暂时可以不用
{
$ret = false;
if (!empty($query)) {
if ($this-_conn === false || !is_resource($this-_conn)) {
warningLog(__METHOD__ . ': query sql with no connection', true);
return false;
}
$this-_query_id = @mysql_query($query, $this-_conn);
if ($this-_query_id === false) {
$this-_errstr = @mysql_error();
$ret = false;
} else {
$this-_errstr = 'SUCCESS';
$ret = $this-_query_id;
}
}
$msg = ($ret === false) ? 'false' : strval($ret);
debugLog(__METHOD__.": [$msg] returned for sql query [$query]");
return $ret;
}
function __construct($sql,$page_size) {
$result = mysql_query($sql);
$datanum = mysql_num_rows($result);
$this-sql=$sql;
$this-datanum=$datanum;
$this-page_size=$page_size;
}
//当前页数
public function page_id() {
if($_SERVER['QUERY_STRING'] == ""){
return 1;
}elseif(substr_count($_SERVER['QUERY_STRING'],"page_id=") == 0){
return 1;
}else{
return intval(substr($_SERVER['QUERY_STRING'],8));
}
}
//剩余url值
public function url() {
if($_SERVER['QUERY_STRING'] == ""){
return "";
}elseif(substr_count($_SERVER['QUERY_STRING'],"page_id=") == 0){
return "".$_SERVER['QUERY_STRING'];
}else{
return str_replace("page_id=".$this-page_id(),"",$_SERVER['QUERY_STRING']);
}
}
//总页数
public function page_num() {
if($this-datanum == 0){
return 1;
}else{
return ceil($this-datanum/$this-page_size);
}
}
//数据库查询的偏移量
public function start() {
return ($this-page_id()-1)*$this-page_size;
}
//数据输出
public function sqlquery() {
return $this-sql." limit ".$this-start().",".$this-page_size;
}
//获取当前文件名
private function php_self() {
return $_SERVER['PHP_SELF'];
}
//上一页
private function pre_page() {
if ($this-page_id() == 1) { //页数等于1
return "a href="https://www.04ip.com/post/.$this-php_self()."?page_id=1".$this-url()."上一页/a ";
}elseif ($this-page_id() != 1) { //页数不等于1
return "a href="https://www.04ip.com/post/.$this-php_self()."?page_id=".($this-page_id()-1).$this-url()."上一页/a ";
}
}
//显示分页
private function display_page() {
$display_page = "";
if($this-page_num() = 10){ //小于10页
for ($i=1;$i=$this-page_num();$i) //循环显示出页面
$display_page .= "a href="https://www.04ip.com/post/.$this-php_self()."?page_id=".$i.$this-url()."".$i."/a ";
return $display_page;
}elseif($this-page_num()10){ //大于10页
if($this-page_id() = 6){
for ($i=1;$i=10;$i) //循环显示出页面
$display_page .= "a href="https://www.04ip.com/post/.$this-php_self()."?page_id=".$i.$this-url()."".$i."/a ";
return $display_page;
}elseif(($this-page_id()6)($this-page_num()-$this-page_id() = 4)){
for ($i=$this-page_id()-5;$i=$this-page_id() 4;$i) //循环显示出页面
$display_page .= "a href="https://www.04ip.com/post/.$this-php_self()."?page_id=".$i.$this-url()."".$i."/a ";
return $display_page;
}elseif(($this-page_id()6)($this-page_num()-$this-page_id()4)){
for ($i=$this-page_num()-9;$i=$this-page_num();$i) //循环显示出页面
$display_page .= "a href="https://www.04ip.com/post/.$this-php_self()."?page_id=".$i.$this-url()."".$i."/a ";
return $display_page;
}
}
}
//下一页
private function next_page() {
if ($this-page_id()$this-page_num()) { //页数小于总页数
return "a href="https://www.04ip.com/post/.$this-php_self()."?page_id=".($this-page_id() 1).$this-url()."下一页/a ";
}elseif ($this-page_id() == $this-page_num()) { //页数等于总页数
return "a href="https://www.04ip.com/post/.$this-php_self()."?page_id=".$this-page_num().$this-url()."下一页/a ";
}
}
// 设置分页信息
public function set_page_info() {
$page_info = "共".$this-datanum."条 ";
$page_info .= "a href="https://www.04ip.com/post/.$this-php_self()."?page_id=1".$this-url()."首页/a ";
$page_info .= $this-pre_page();
$page_info .= $this-display_page();
$page_info .= $this-next_page();
$page_info .= "a href="https://www.04ip.com/post/.$this-php_self()."?page_id=".$this-page_num().$this-url()."尾页/a ";
$page_info .= "第".$this-page_id()."/".$this-page_num()."页";
return $page_info;
}
}
?
2.脚本2:
?php
//类的用法
// 读取分页类
include("pager.class.php");
// 数据库连接初始化
//$db = new mysql();
$impeach_host = '10.81.43.139';
$impeach_usr = 'vmtest15';
$impeach_passwd = 'vmtest15';
$impeach_name = 'ufeature';
$impeach_con = mysql_connect($impeach_host, $impeach_usr, $impeach_passwd) or
die("Can't connect ".mysql_error());
mysql_select_db($impeach_name, $impeach_con);
// 这是一个sql查询语句,并得到查询结果
$sql = "select word from ufeature.spam_accuse_word_list where flag='0'";
// 分页初始化
$page = new pager($sql,20);
// 20是每页显示的数量
// $res_1 = mysql_query($sql) or
//die("Can't get result ".mysql_error());
$result=mysql_query($page-sqlquery());
while($info = mysql_fetch_array($result,MYSQL_ASSOC)){
// while($info = mysql_fetch_array($res_1, MYSQL_ASSOC)){
echo $info["word"]."br/";
}
// 页码索引条
echo $page-set_page_info();
?
方法二:使用ajax的方法
1、首先了解SQL语句中的limit用法
SELECT * FROM table …… limit 开始位置 ,操作条数 (其中开始位置是从0开始的)
例子
取前20条记录:SELECT * FROM table …… limit0,20
从第11条开始取20条记录:SELECT * FROM table …… limit10 , 20
LIMIT n 等价于 LIMIT 0,n 。
如select * from table LIMIT 5; //返回前5行,和select * from table LIMIT 0 , 5一样
2、分页原理
所谓分页显示,也就是讲数据库中的结果集,一段一段显示出来
怎么分段,当前在第几段 (每页有几条,当前再第几页)
前10条记录:select * from table limit 0,10
第11至20条记录:select * from table limit 10,10
第21至30条记录:select * from table limit 20,10
分页公式:
(当前页数 - 1 )X 每页条数 , 每页条数
Select * from table limit ($Page- 1) * $PageSize, $PageSize
3、$_SERVER["REQUEST_URI"]函数
预定义服务器变量的一种,所有$_SERVER开头的都叫做预定于服务器变量 。
REQUEST_URI的作用是取得当前URI,也就除域名外后面的完整的地址路径 。
例子:
当前页为:;cid=22
echo $_SERVER["REQUEST_URI"]
结果为:/home.php?id=23cid=22
4、parse_url()解析URL函数
parse_url() 是讲URL解析成有固定键值的数组的函数
例子
$ua=parse_url("");
print_r($ua);
结果:
Array
(
[scheme] = http;协议
[host] = hostname;主机域名
[user] = username;用户
[pass] = password;密码
[path] = /path;路径
[query] = arg=value;取参数
[fragment] = anchor;
)
5、代码实例
这个一个留言的分页,分为3个部分,一个是数据库设计 , 一个是连接页面 , 一个是显示页面 。
(1)设计数据库
设计数据库名为bbs , 有一个数据表为message,里面包含title,lastdate,user,content等字段,分别表示留言标题,留言日前,留言人,留言的内容
(2)连接页面
?php
$conn = @ mysql_connect("localhost", "root", "123456") or die("数据库链接错误");
mysql_select_db("bbs", $conn);
mysql_query("set names 'GBK'"); //使用GBK中文编码;
//将空格,换行转换为HTML可解析
function htmtocode($content) {
$content = str_replace("\n", "br", str_replace(" ", " ", $content)); //两个str_replace嵌套
return $content;
}
//$content=str_replace("'","‘",$content);
//htmlspecialchars();
?
(3)显示页面
?php
include("conn.php");
$pagesize=2; //设置每页显示2个记录
$url=$_SERVER["REQUEST_URI"];
$url=parse_url($url);
$url=$url[path];
【php对xml数据分页 php分页查询mysql】$numq=mysql_query("SELECT * FROM `message`");
$num = mysql_num_rows($numq);
if($_GET){
$pageval=$_GET;
$page=($pageval-1)*$pagesize;
$page.=',';
}
if($num$pagesize){
if($pageval=1)$pageval=1;
echo "共 $num 条".
" a href=https://www.04ip.com/post/$url?page=".($pageval-1)."上一页/a a href=https://www.04ip.com/post/$url?page=".($pageval 1)."下一页/a";
}
$SQL="SELECT * FROM `message` limit $page $pagesize ";
$query=mysql_query($SQL);
while($row=mysql_fetch_array($query)){
?
table width=500 border="0" cellpadding="5" cellspacing="1" bgcolor="#add3ef"
tr bgcolor="#eff3ff"
td标题:?php echo $row[title]?/td td时间:?php echo $row[lastdate]?/td
/tr
tr bgcolor="#eff3ff"
td 用户:?php echo $row[user]?/tdtd/td
/tr
tr
td内容:?php echo htmtocode($row[content]);?/td
/tr
br
/table
?php
}
?
方法3:
script
function viewpage(p){
if(window.XMLHttpRequest){
var xmlReq = new XMLHttpRequest();
} else if(window.ActiveXObject) {
var xmlReq = new ActiveXObject('Microsoft.XMLHTTP');
}
var formData = "https://www.04ip.com/post/page=" p;
xmlReq.onreadystatechange = function(){
if(xmlReq.readyState == 4){
document.getElementByIdx_x('content2').innerHTML = xmlReq.responseText;
}
}
xmlReq.open("post", "hotel_list.php", true);
xmlReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlReq.send(formData);
return false;
}
/script
脚本2:
header("Content-Type:text/html;charset=GB2312");
$pagesize=10;
//echo $_POST['page'];
$result = mysql_query("Select count(DISTINCT hotelname) FROM ".TBL_HOTELS);
$myrow = mysql_fetch_array($result);
$numrows=$myrow[0];
$pages=intval($numrows/$pagesize);
if ($numrows%$pagesize)
$pages;
if (isset($_POST['page'])){
$page=intval($_POST['page']);
}
else{
//设置为第一页
$page=1;
}
$first=1;
$prev=$page-1;
$next=$page 1;
$last=$pages;
//计算记录偏移量
$offset=$pagesize*($page - 1);
//读取指定记录数
$result=mysql_query("select `hotelname` , count( * ) from ".TBL_HOTELS." GROUP BY `hotelname` order by id desc limit $offset,$pagesize");
$num = mysql_num_rows($result);
while ($row = mysql_fetch_array($result,MYSQL_NUM)) {
$hotelname[] = $row[0];
$countpeople[] = $row[1];
}
for($a=0;$a$num;$a)
{
//$result=mysql_query("select count(title) from " . TBL_Comments ." where `title`=\"".$title[$a]."\"");
//$row = mysql_fetch_row($result);
echo "TABLE style=\"MARGIN-BOTTOM: 20px\" cellSpacing=0 cellPadding=0 width=100% \n";
echo "TBODY\n";
echo "TR\n";
echo "TD style=\"PADDING-TOP: 5px\" vAlign=top align=left width=80\n";
//rating_bar($title[$a],5);
echo "/TD\n";
echo "TD style=\"PADDING-TOP: 5px\" align=left width=100%A title=$hotelname[$a] style=\"FONT-SIZE: 14px\" href=https://www.04ip.com/post/#$hotelname[$a]/A/n";
echo "/TD/TR\n";
echo " TR\n";
echo "TD/TD\n";
echo "TD style=\"PADDING-LEFT: 0px\"\n";
echo "IMG src=https://www.04ip.com/"images/comment.gif\"推荐人数:($countpeople[$a]) |\n";
echo "SPAN平均分:STRONG/STRONG (".$count."票) | 评论数:()/SPAN\n";
echo "/TD/TR/TBODY/TABLE\n";
}
echo "TABLE style=\"MARGIN-TOP: 30px\" cellSpacing=0 cellPadding=0 width=\"100%\"";
echo "";
echo "TBODYTRTD colSpan=3 height=20";
echo "DIV align=center";
echo "P align=leftFONT color=red第".$page."页/总".$pages."页 | 总".$numrows."条/FONT | ";
if ($page1) echo "a onclick=\"viewpage(".$first.")\" href='https://www.04ip.com/post/#'首页/a | ";
if ($page1) echo "a onclick=\"viewpage(".$prev.")\" href='https://www.04ip.com/post/#'上页/a | ";
if ($page$pages) echo "a onclick=\"viewpage(".$next.")\" href='https://www.04ip.com/post/#'下页/a | ";
if ($page$pages) echo "a onclick=\"viewpage(".$last.")\" href='https://www.04ip.com/post/#'尾页/a";
echo "转到第 INPUT maxLength=3 size=3 value=https://www.04ip.com/post/1 name=goto_page 页 INPUT hideFocus onclick=/"viewpage(document.all.goto_page.value)\" type=button value=https://www.04ip.com/post/Go name=cmd_goto";
echo "/P/DIV/TD/TR/TBODY/TABLE";
php对xml数据分页的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于php分页查询mysql、php对xml数据分页的信息别忘了在本站进行查找喔 。

    推荐阅读