Rendering (Anzeige der Nachrichtenliste an der Rezeption)
Hinweis: Die CSS-Implementierung im folgenden Code ist nicht enthalten, Sie können sie implementieren es selbst. Rufen Sie die Daten in der Datenbank über den Erfolg der Ajax-Rückruffunktion usw. ab und geben Sie die Daten dann über js an PHP aus, um die Daten dynamisch anzuzeigen. Zur Erinnerung.
Code-Implementierung
<code>pages.js </code>
<code><span>var</span> curPage =<span>1</span>;<span>//当前页码</span><span>var</span> total,pageSize,totalPage; <span>//获取数据</span><span><span>function</span><span>getData</span><span>(page)</span>{</span> $.ajax({ type:<span>'POST'</span>, url:<span>'page.php'</span>, data:{<span>'pageNum'</span>:page-<span>1</span>}, dataType:<span>'json'</span>, beforeSend:<span><span>function</span><span>()</span>{</span> $(<span>"#list ul"</span>).append(<span>"<li>loading...</li>"</span>); }, success:<span><span>function</span><span>(json)</span>{</span> $(<span>"#list ul"</span>).empty(); total = json.total;<span>//总记录数</span> pageSize = json.pageSize;<span>//每页显示条数</span> curPage = page;<span>//当前页</span> totalPage = json.totalPage;<span>//总页数</span><span>var</span> li =<span>""</span>; <span>var</span> list = json.list; $.each(list,<span><span>function</span><span>(index,array)</span>{</span><span>//遍历json数据列</span><span>if</span>(array[<span>'title'</span>].length ><span>28</span>){ <span>var</span> title_sub = array[<span>'title'</span>].substring(<span>0</span>,<span>20</span>);<span>// 获取子字符串。</span> } elsevar title_sub = array[<span>'title'</span>]; <span>if</span>(<span>0</span>==index &&<span>1</span>==curPage)li +=<span>"<li> <a href="%5C%22%22</span">+ array[<span>'content'</span>]+<span>"\"style=\"color:red;font-weight:900;\"target=\"myIframe\" title=\""</span>+array[<span>'title'</span>]+<span>"\">"</span>+title_sub+<span>"</span></a><div>"+array[<span>'date'</span>]+<span>"</span> </div> </li>"</span>; elseif(<span>1</span>==index &&<span>1</span>==curPage)li +=<span>"<li> <a href="%5C%22%22</span">+ array[<span>'content'</span>]+<span>"\"style=\"color:Darkorange;font-weight:700;\"target=\"myIframe\" title=\""</span>+array[<span>'title'</span>]+<span>"\">"</span>+title_sub+<span>"</span></a><div>"+array[<span>'date'</span>]+<span>"</span> </div> </li>"</span>; elseif(<span>2</span>==index &&<span>1</span>==curPage)li +=<span>"<li> <a href="%5C%22%22</span">+ array[<span>'content'</span>]+<span>"\"style=\"color:Greenyellow;font-weight:500;;\"target=\"myIframe\" title=\""</span>+array[<span>'title'</span>]+<span>"\">"</span>+title_sub+<span>"</span></a><div>"+array[<span>'date'</span>]+<span>"</span> </div> </li>"</span>; <span>else</span> li +=<span>"<li> <a href="%5C%22%22</span">+ array[<span>'content'</span>]+<span>"\"target=\"myIframe\" title=\""</span>+array[<span>'title'</span>]+<span>"\">"</span>+title_sub+<span>"</span></a><div>"+array[<span>'date'</span>]+<span>"</span> </div> </li>"</span>; }); $(<span>"#list ul"</span>).append(li); }, complete:<span><span>function</span><span>()</span>{</span><span>//生成分页条</span> getPageBar(); }, error:<span><span>function</span><span>()</span>{</span> alert(<span>"数据加载失败"</span>); } }); } <span>//获取分页条</span><span><span>function</span><span>getPageBar</span><span>()</span>{</span><span>//页码大于最大页数</span><span>if</span>(curPage>totalPage) curPage=totalPage; <span>//页码小于1</span><span>if</span>(curPage1) curPage=<span>1</span>; pageStr =<span>"<span>共"</span>+total+<span>"条</span><span>"</span>+curPage+<span>"/"</span>+totalPage+<span>"</span>"</span>; <span>//如果是第一页</span><span>if</span>(curPage==<span>1</span>){ pageStr +=<span>"<span>首页</span> <span>上一页</span> "</span>; }<span>else</span>{ pageStr +=<span>"<span><a rel="1">首页</a> </span><span><a rel='"</span>+(curPage-<span>1</span>)+<span>"'>上一页</a> </span>"</span>; } <span>//如果是最后页</span><span>if</span>(curPage>=totalPage){ pageStr +=<span>"<span>下一页</span> <span>尾页</span> "</span>; }<span>else</span>{ pageStr +=<span>"<span><a rel='"</span>+(<span>parseInt</span>(curPage)+<span>1</span>)+<span>"'>下一页</a> </span><span><a rel='"</span>+totalPage+<span>"'>尾页</a> </span>"</span>; } $(<span>"#pagecount"</span>).html(pageStr); } $(<span><span>function</span><span>()</span>{</span> getData(<span>1</span>); $(<span>"#pagecount span a"</span>).live(<span>'click'</span>,<span><span>function</span><span>()</span>{</span><span>var</span> rel = $(<span>this</span>).attr(<span>"rel"</span>); <span>if</span>(rel){ getData(rel); } }); });</code>
<code>page.php </code>
<code><span><?php </span><span>/*数据库连接文件,这个只需连接即可*/</span><span>include_once</span>(<span>'../connect/connect.php'</span>); <span>$page</span>=intval(<span>$_POST</span>[<span>'pageNum'</span>]); <span>$result</span>=mysql_query(<span>"select id from news"</span>); <span>$total</span>=mysql_num_rows(<span>$result</span>);<span>//总记录数</span><span>$pageSize</span>=<span>6</span>;<span>//每页显示数</span><span>$totalPage</span>=ceil(<span>$total</span>/<span>$pageSize</span>);<span>//总页数</span><span>$startPage</span>=<span>$page</span>*<span>$pageSize</span>; <span>$arr</span>[<span>'total'</span>]=<span>$total</span>; <span>$arr</span>[<span>'pageSize'</span>]=<span>$pageSize</span>; <span>$arr</span>[<span>'totalPage'</span>]=<span>$totalPage</span>; <span>$query</span>=mysql_query(<span>"select id,title,content,date from news order by id desc limit $startPage,$pageSize"</span>); <span>while</span>(<span>$row</span>=mysql_fetch_array(<span>$query</span>)){ <span>$arr</span>[<span>'list'</span>][]=<span>array</span>( <span>'id'</span>=><span>$row</span>[<span>'id'</span>], <span>'title'</span>=><span>$row</span>[<span>'title'</span>], <span>'content'</span>=><span>$row</span>[<span>'content'</span>], <span>'date'</span>=>date(<span>"Y-m-d"</span>,<span>$row</span>[<span>'date'</span>]) ); } echojson_encode(<span>$arr</span>); <span>?></span></span></code>
<code>news_manager.php </code>
Achten Sie darauf, ihn in die js-Datei aufzunehmen.
<code>... <divid>"list"<span><span>class</span>="<span>list</span>"> ul</span>><span>ul</span>> <span>div</span>> divid="<span>pagecount</span>"><span>div</span>> ...</divid></code>
Urheberrechtserklärung: Dieser Artikel ist ein [Original]-Artikel des Bloggers. Er darf ohne die Erlaubnis des Bloggers reproduziert werden: [http: //blog.csdn.net /FreeApe]
Das Obige stellt die Paging-Implementierung von php---mysql ajax unordered list (ul li) vor, einschließlich Aspekten des Inhalts. Ich hoffe, dass es für Freunde hilfreich sein wird, die an PHP-Tutorials interessiert sind.