分页
<?php
$link1=mysqli_connect('www.wangjianhong.com','root','root','two_month');
$bookname=isset($_GET['name'])?$_GET['name']:'';
mysqli_query($link1,'set names tf8');
//求出总条数
if ( $bookname ) {
$sql1="select * from book where bookName like '%$bookname%'";
} else {
$sql1="select * from book ";
}
$count=mysqli_query($link1,$sql1);
$count=mysqli_num_rows($count);
//当前在第几页
$page=isset($_GET['page'])?$_GET['page']:1;
//每页显示的条数
$limit=7;
//总长度除以每页显示的条数
$numPage = ceil($count/$limit);
//页数一定比总页数
if ( $page>$numPage ) {
$page = $numPage;
}
//判断页数不能为0
if ($page<=0) {
$page = 1;
}
//计算页面偏移量
$x=($page-1)*$limit;
$link=mysqli_connect('www.wangjianhong.com','root','root','two_month');
mysqli_query($link,'set names utf8');
if ( $bookname ) {
$sql="select * from book where bookName like '%$bookname%' limit $x,$limit";
} else {
$sql="select * from book limit $x,$limit ";
}
$res=mysqli_query($link,$sql);
while ($arr=mysqli_fetch_assoc($res)) {
$data[]=$arr;
}
?>
书名搜索<input type="text" id='sel'><button onclick="sel()">搜索</button>
<div id="div">
<table border="1">
<h2>图书表</h2>
<tr>
<th>选择</th>
<th>编号</th>
<th>书名</th>
<th>作者</th>
<th>出版社</th>
<th>出版年月</th>
<th>图片</th>
<th>操作</th>
</tr>
<?php
foreach ($data as $key => $value) {
?>
<tr id='<?php echo $value['book_id'];?>'>
<td><input type="checkbox" name="checkall" value="<?php echo $value['book_id'];?>"></td>
<td> <?php echo $value['book_id'];?></td>
<td> <?php echo $value['bookName'];?></td>
<td> <?php echo $value['bookWriter'];?></td>
<td> <?php echo $value['bookPress'];?></td>
<td> <?php echo $value['bookYear'];?></td>
<td> <img src="./load/<?php echo $value['book_image'];?>" width="50px" height="50px"></td>
<td>
<a href="table.php">返回首页</a>
<a href="delete.php?id=<?php echo $value['book_id'];?>">删除</a>
<a href="default.php?id=<?php echo $value['book_id'];?>">修改</a>
<a href="download.php?path=./load/<?php echo $value['book_image'];?>">下载此图片</a>
</td>
</tr>
<?php
}
?>
</table>
</div>
//默认第一页
<input type="hidden" id='page' value="1">
<button onclick="fun1()">全选</button>
<button onclick="fun2()">取消</button>
<button onclick="fun3()">反选</button>
<button onclick="fun4()">批量删除</button>
<button onclick="fun5()">ajax批量删除</button>
<button onclick="fun6()">上一页</button>
<button onclick="fun7()">下一页</button>
<script type="text/javascript">
function fun6() {
var page = document.getElementById('page').value;
page = page-1;
if ( page<1 )
{
page = 1;
}
var ajax= new XMLHttpRequest();
ajax.open('get', 'sel.php?page='+page,true);
ajax.send();
ajax.onreadystatechange=function() {
if ( ajax.readyState==4 && ajax.status==200 ) {
document.getElementById('page').value=page;
div.innerHTML=ajax.responseText;//替换
}
}
}
function fun7() {
var page = document.getElementById('page').value;
page = parseInt(page)+1;
var ajax= new XMLHttpRequest();
ajax.open('get', 'sel.php?page='+page,true);
ajax.send();
ajax.onreadystatechange=function() {
if ( ajax.readyState==4&& ajax.status==200 ) {
document.getElementById('page').value=page;
div.innerHTML=ajax.responseText;//替换
}
}
}
</script>
<a href="show.php?page=<?php echo $page-1;?>&name=<?php echo $bookname;?>">上一页</a>
<a href="show.php?page=<?php echo $page+1;?>&name=<?php echo $bookname;?>">下一页</a>
<script>
function fun1(){
var checkall=document.getElementsByName('checkall');
for (var i = 0; i < checkall.length; i++) {
checkall[i].checked=true;
}
}
function fun2(){
var checkall=document.getElementsByName('checkall');
for (var i = 0; i < checkall.length; i++) {
checkall[i].checked=false;
}
}
function fun3(){
var checkall=document.getElementsByName('checkall');
for (var i = 0; i < checkall.length; i++) {
if (checkall[i].checked==false) {
checkall[i].checked=true;
}
else
{
checkall[i].checked=false;
}
}
}
function fun4(){
var checkall=document.getElementsByName('checkall');
var str='';
for (var i = 0; i < checkall.length; i++) {
if (checkall[i].checked) {
str=str+checkall[i].value+',';
}
}
var str=str.substr(0,str.length-1);
location.href='delete.php?id='+string;
}
function fun5(){
var boxs=document.getElementsByName('checkall');
var str='';
for (var i = 0; i < boxs.length; i++) {
if ( boxs[i].checked ) {
str+=boxs[i].value+',';
}
}
str=str.substr(0,str.length-1)
var ajax=new XMLHttpRequest();
ajax.open('get', 'delete.php?id='+str,true);
ajax.send();
ajax.onreadystatechange=function(){
if ( ajax.readyState==4 && ajax.status==200 ) {
if ( ajax.responseText == true) {
str=str.split(',');
for (var i = 0; i < str.length; i++) {
var tr=document.getElementById(str[i]);
tr.parentNode.removeChild(tr);
};
}
}
}
}
</script>
<script>
function sel(){
var userName = document.getElementById('sel').value;
location.href='show.php?name='+userName;
}
</script>
搜索
<?php
$link1=mysqli_connect('www.wangjianhong.com','root','root','two_month');
$bookname=isset($_GET['name'])?$_GET['name']:'';
mysqli_query($link1,'set names tf8');
$sql1="select * from book ";
$count=mysqli_query($link1,$sql1);
$count=mysqli_num_rows($count);
//当前在第几页
$page=isset($_GET['page'])?$_GET['page']:1;
//每页显示的条数
$limit=7;
//总长度除以每页显示的条数
$numPage = ceil($count/$limit);
//页数一定比总页数
if ( $page>$numPage ) {
$page = $numPage;
}
//判断页数不能为0
if ($page<=0) {
$page = 1;
}
//计算页面偏移量
$x=($page-1)*$limit;
$link=mysqli_connect('www.wangjianhong.com','root','root','two_month');
mysqli_query($link,'set names utf8');
if ( $bookname ) {
$sql="select * from book where bookName like '%$bookname%' limit $x,$limit";
} else {
$sql="select * from book limit $x,$limit ";
}
$res=mysqli_query($link,$sql);
while ($arr=mysqli_fetch_assoc($res)) {
$data[]=$arr;
}
?>
$str = "<table border='1'>
<tr>
<th>选择</th>
<th>编号</th>
<th>书名</th>
<th>作者</th>
<th>出版社</th>
<th>出版年月</th>
<th>图片</th>
<th>操作</th>
</tr>
";
foreach ($data as $value)
{
$str=$str."
<tr id='{$value['book_id']}'>
<td><input type='checkbox' name='checkall' value='{$value['book_id']}'></td>
<td>{$value['book_id']}</td>
<td>{$value['bookName']}</td>
<td>{$value['bookWriter']}</td>
<td>{$value['bookPress']}</td>
<td>{$value['bookYear']}</td>
<td><img src='./load/{$value['book_image']}' width='50px' height='50px'></td>
<td>
<a href='table.php'>返回首页</a>
<a href='delete.php?id={$value['book_id']}'>删除</a>
</td>
</tr>
";
}
$str.="</table>";
echo $str;