Home > Backend Development > PHP Tutorial > php, ajax implement paging

php, ajax implement paging

WBOY
Release: 2016-07-29 08:37:39
Original
816 people have browsed it

I have summarized some fart experiences
1. After using ajax to post data to the background page, you need to reconnect to the database. Don’t think that it is enough to connect with the previous session
2. In order to deal with the problem of returning garbled characters, I added header( "Content-Type: text/html;charset=GB2312"); can be displayed normally. Later, when I checked it under firefox, I was prompted to download this web page. After searching a lot of information on the Internet, I got a vague understanding that the code of the web page has There is a syntax error. For security reasons, Firefox will not display it directly but prompts for downloading. I rechecked the statement just now and found that I had written an extra "". After removing it, the problem was solved. Haha, so I encountered this Question, please check the HTML tag carefully. After all, Firefox is not as smart as IE
3. Finally, as a web site developer, you must be responsible. Don’t think that everything will be fine if you pass the test under IE. After all, not everyone uses it. ie, you have to do more tests in other browsers to show your professionalism
ajax script:

Copy the code The code is as follows:


<script> <br>function viewpage(p ){ <br>if(window.XMLHttpRequest){ <br>var xmlReq = new XMLHttpRequest(); <br>} else if(window.ActiveXObject) { <br>var xmlReq = new ActiveXObject('Microsoft.XMLHTTP'); = " page="+p; <br>xmlReq.onreadystatechange = function(){ <br>if(xmlReq.readyState == 4){ <br>document.getElementById('content2').innerHTML = xmlReq.responseText; <br>} <br>} <br>xmlReq.open ("post", "hotel_list.php", true); <br>xmlReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); <br>xmlReq.send(formData); <br>return false; <br>} <br></script>


Call:



Copy code The code is as follows:


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 "n"; 
echo "n"; 
echo "n"; 
echo "n"; 
//rating_bar($title[$a],5); 
echo "n"; 
echo "$hotelname[$a]n"; 
echo "n"; 
echo " n"; 
echo "n"; 
echo "n"; 
echo "  推荐人数:($countpeople[$a]) |n"; 
echo "平均分: (".$count."票) | 评论数:()n"; 
echo "n"; 

echo "echo "border=0>"; 
echo ""; 
echo ""; 
echo "第".$page."页/总".$pages."页 | 总".$numrows."条 | "; 
if ($page>1) echo " | "; 
if ($page>1) echo " | "; 
if ($page<$pages) echo " | "; 
if ($page<$pages) echo ""; 
echo "转到第  页 echo "

";

以上就介绍了 php,ajax实现分页,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template