Using the HTML5 Api for paging

Source: Internet
Author: User
Tags dsn php foreach


htmlapi.php

<?php

Header ("Content-type:text/html;charset=utf-8");

$dsn = ' Mysql:host=127.0.0.1;dbname=edusoho ';

$db =new PDO ($dsn, ' root ', ' root ');

$page =isset ($_get[' P '])? $_get[' P ']: ' 1 ';

$pagesize = ' 10 ';

$limit = ($page-1) * $pagesize;

$sql = ' SELECT count (*) as num from Course_lesson_text ';

$rs = $db->query ($sql);

$ar = $rs->fetch ();

$count = $ar [' num '];

$pagecount =ceil ($count/$pagesize);

$sql = "Select Id,title from Course_lesson_text limit $limit, $pagesize";

$rs = $db->query ($sql);

$arr = $rs->fetchall ();

$next = $page +1> $pagecount? $pagecount: $page +1;

$up = $page -1<1?1: $page-1;

$pagea = "<a class= ' pa ' href= '? p=1 ' > Home </a><a class= ' pa ' href= '? p= $up ' > Prev </a><a class= ' pa ' Href= '? p= $next ' > Next </a><a class= ' pa ' href= '? p= $pagecount ' > Last </a> ';

$exit [' arr ']= $arr;

$exit [' page ']= $pagea;

Exit (Json_encode ($exit));

?>

index.php

<?php

Header ("Content-type:text/html;charset=utf-8");

$dsn = ' Mysql:host=127.0.0.1;dbname=edusoho ';

$db =new PDO ($dsn, ' root ', ' root ');

$page =isset ($_get[' P '])? $_get[' P ']: ' 1 ';

$page = 1;

$pagesize = ' 10 ';

$limit = ($page-1) * $pagesize;

$sql = ' SELECT count (*) as num from Course_lesson_text ';

$rs = $db->query ($sql);

$ar = $rs->fetch ();

$count = $ar [' num '];

$pagecount =ceil ($count/$pagesize);

$sql = "Select Id,title from Course_lesson_text limit $limit, $pagesize";

$rs = $db->query ($sql);

$arr = $rs->fetchall ();

?>

<meta charset= ' Utf-8 '/>

<ul class= "List" >

<?php foreach ($arr as $key = = $value) {

?>

<li><?php echo $value [' title ']?></li>

<?php

}?>

</ul>

<?php

$next = $page +1> $pagecount? $pagecount: $page +1;

$up = $page -1<1?1: $page-1;

?>

<div id= "PageA" >

<a class= ' pa ' href= "? p=1" > Home </a>

<a class= ' pa ' href= "? p=<?php echo $up?>" > Prev </a>

<a class= ' pa ' href= "? p=<?php echo $next?>" > Next </a>

<?php

/*for ($i =1; $i < $pagecount; $i + +) {

echo "<a class= ' pa ' href= ' p= $i ' > $i </a>";

}*/

?>

<a class= ' pa ' href= "? p=<?php echo $pagecount?>" > Last </a>

</div>

<script src= './jquery-1.8.0.js ' ></script>

<script>

$ (function () {

var Ajax,

CurrentState;

$ (". Pa"). Live ("Click", Function (e) {

E.preventdefault ();

var target = E.target,

URL = $ (target). attr ("href");

!$ (This). Hasclass (' current ') && $ (this). addclass (' current '). Siblings (). Removeclass ("current");

if (Ajax = = undefined) {

CurrentState = {

Url:document.location.href,

Title:document.title,

HTML: $ (". List"). HTML ()

};

}

Ajax = $.ajax ({

Type: "Get",

URL: ' htmlapi.php ' +url,

DataType: "JSON",

Success:function (data) {

var html = "";

if (data[' arr '].length > 0) {

$.each (data[' arr '],function (k,v) {

HTML + = "<li>" +v.title+ "</li>"

})

$ (". List"). html (HTML);

}

var state = {

Url:url,

Title:document.title,

HTML: $ (". List"). HTML ()

};

$ ("#pagea"). html (data[' page ');

History.pushstate (State,null,url);

}

});

});

Window.addeventlistener ("Popstate", function (event) {

if (Ajax = = null) {

Return

}else if (event && event.state) {

Document.title = Event.state.title;

$ (". List"). html (event.state.html);

}else{

Document.title = Currentstate.title;

$ (". List"). html (currentstate.html);

}

});

});


Using the HTML5 Api for paging

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.