Php 代码点火器分页和JQuery操作
我正在创建一个图书管理web应用程序,在使用分页时删除图书时遇到问题 在我的控制器中,我有以下代码:Php 代码点火器分页和JQuery操作,php,jquery,codeigniter,Php,Jquery,Codeigniter,我正在创建一个图书管理web应用程序,在使用分页时删除图书时遇到问题 在我的控制器中,我有以下代码: <?php class Books extends CI_Controller { function __construct() { parent::__construct(); #$this->load->model('books_model'); $this->load-
<?php
class Books extends CI_Controller {
function __construct()
{
parent::__construct();
#$this->load->model('books_model');
$this->load->helper('path');
}
function index() {
$config['base_url'] = base_url().'books/index';
$config['total_rows'] = $this->db->count_all('tbl_books');
$config['per_page'] = 2;
$this->pagination->initialize($config);
$data['books'] = $this->db->get('tbl_books',$config['per_page'], $this->uri->segment(3));
$data['pagination'] = $this->pagination->create_links();
$data['page'] = 'Books';
$this->load->view('books_view', $data);
}
}
?>
<p class="pagination ta-right">
<?=$pagination?>
</p>
<?php foreach($books->result() as $row): ?>
<a href="<?=base_url()?><?=$row->image?>" class="nyroModal"> <img src="<?=base_url()?><?=$row->image?>" alt="" /></a>
<a href="#<?=$row->id?>" title="<?=$row->title?>" class="nyroModal"><b class="big"><?=$row->title?></b></a> ·
<?php
$sql = $this->db->query("select * from tbl_transactions where bookid=".$row->id." and (type='reserve' or type='borrowed')");
$book_info = $sql->result();
if($sql->num_rows() > 0) {
if($book_info[0]->type == 'reserve') { ?>
<span class="label label-red">Reserved</span>
<?php }elseif($book_info[0]->type == 'borrowed') { ?>
<span class="label label-blue">Borrowed</span>
<?php } else { ?>
<span class="label label-green">Available</span>
<?php }
} else { ?>
<span class="label label-green">Available</span>
<?php } ?>
Author: <b><?=$row->authors?></b> | Category: </small><br/>
<div id="action-<?=$row->id?>">
<a href="" id="remove-<?=$row->id?>">remove</a> · <a href="#">edit</a>
</div>
<img src="<?=base_url()?>img/small-loader.gif" style="display:none;" id="ajax-load-<?=$row->id?>" />
<div id="<?=$row->id?>" style='display: none;'><h3><?=$row->title?></h3><p align="justify"><?=$row->description?></p></div>
<?php endforeach; } ?>
<script type="text/javascript">
$(document).ready(function() {
<?php foreach($books->result() as $row): ?>
$('#remove-<?=$row->id?>').click(function() {
var stats = confirm('Are you sure you want to delete this entry?');
if(stats) {
$('#action-<?=$row->id?>').hide();
$('#ajax-load-<?=$row->id?>').show();
$.ajax({
type: 'POST',
url: "<?=base_url()?>bookacts/delbook/",
data: ({id: <?=$row->id?>}),
cache: false,
success: function (msg){
if(msg == ""){
$('#ajax-load-<?=$row->id?>').hide();
$('.box-error').fadeIn("slow");
$('#action-<?=$row->id?>').fadeIn();
} else {
$('#ajax-load-<?=$row->id?>').hide();
$('.box-success').fadeIn("slow")
.animate({opacity: 1.0}, 2000)
.fadeOut('slow');
$('#action-<?=$row->id?>').fadeIn();
}
}
}); return false;
} else {
return false;
}
});
<?php endforeach; ?>
});
</script>
在我的书籍视图中,我有以下代码:
<?php
class Books extends CI_Controller {
function __construct()
{
parent::__construct();
#$this->load->model('books_model');
$this->load->helper('path');
}
function index() {
$config['base_url'] = base_url().'books/index';
$config['total_rows'] = $this->db->count_all('tbl_books');
$config['per_page'] = 2;
$this->pagination->initialize($config);
$data['books'] = $this->db->get('tbl_books',$config['per_page'], $this->uri->segment(3));
$data['pagination'] = $this->pagination->create_links();
$data['page'] = 'Books';
$this->load->view('books_view', $data);
}
}
?>
<p class="pagination ta-right">
<?=$pagination?>
</p>
<?php foreach($books->result() as $row): ?>
<a href="<?=base_url()?><?=$row->image?>" class="nyroModal"> <img src="<?=base_url()?><?=$row->image?>" alt="" /></a>
<a href="#<?=$row->id?>" title="<?=$row->title?>" class="nyroModal"><b class="big"><?=$row->title?></b></a> ·
<?php
$sql = $this->db->query("select * from tbl_transactions where bookid=".$row->id." and (type='reserve' or type='borrowed')");
$book_info = $sql->result();
if($sql->num_rows() > 0) {
if($book_info[0]->type == 'reserve') { ?>
<span class="label label-red">Reserved</span>
<?php }elseif($book_info[0]->type == 'borrowed') { ?>
<span class="label label-blue">Borrowed</span>
<?php } else { ?>
<span class="label label-green">Available</span>
<?php }
} else { ?>
<span class="label label-green">Available</span>
<?php } ?>
Author: <b><?=$row->authors?></b> | Category: </small><br/>
<div id="action-<?=$row->id?>">
<a href="" id="remove-<?=$row->id?>">remove</a> · <a href="#">edit</a>
</div>
<img src="<?=base_url()?>img/small-loader.gif" style="display:none;" id="ajax-load-<?=$row->id?>" />
<div id="<?=$row->id?>" style='display: none;'><h3><?=$row->title?></h3><p align="justify"><?=$row->description?></p></div>
<?php endforeach; } ?>
<script type="text/javascript">
$(document).ready(function() {
<?php foreach($books->result() as $row): ?>
$('#remove-<?=$row->id?>').click(function() {
var stats = confirm('Are you sure you want to delete this entry?');
if(stats) {
$('#action-<?=$row->id?>').hide();
$('#ajax-load-<?=$row->id?>').show();
$.ajax({
type: 'POST',
url: "<?=base_url()?>bookacts/delbook/",
data: ({id: <?=$row->id?>}),
cache: false,
success: function (msg){
if(msg == ""){
$('#ajax-load-<?=$row->id?>').hide();
$('.box-error').fadeIn("slow");
$('#action-<?=$row->id?>').fadeIn();
} else {
$('#ajax-load-<?=$row->id?>').hide();
$('.box-success').fadeIn("slow")
.animate({opacity: 1.0}, 2000)
.fadeOut('slow');
$('#action-<?=$row->id?>').fadeIn();
}
}
}); return false;
} else {
return false;
}
});
<?php endforeach; ?>
});
</script>
&米德多;
我现在看不出有什么问题。然而,在我看来,您使用CodeIgniter和Jquery都是错误的,所以我将给您一些提示
首先,您可能知道,Codeigniter使用MVC模式。而且视图实际上并不适用于SQL。更好的是,大多数时候控制器甚至不用于SQL。您应该尝试将SQL保留在模型中
另一个小小的代码点火器。我认为最好使用:
function index($page=0) {
然后:
$data['books'] = $this->db->get('tbl_books',$config['per_page'], $page);
而不是使用$this->uri->segment(3)
其次,还应该使用Jquery“dynamic”。现在,您正在为每本书创建一个函数,而您始终希望执行相同的操作
添加一个类:
<a href="" id="remove-<?=$row->id?>" class="book-remove">remove</a>
然后作为JS:
<script type="text/javascript">
$(document).ready(function() {
$('.book-remove').each(function() {
$(this).click(function() {
var stats = confirm('Are you sure you want to delete this entry?');
if(stats) {
var bookID = $(this).attr("id").replace("remove-", "");
$('#action'+bookID).hide();
$('#ajax-load-'+bookID).show();
$.ajax({
type: 'POST',
url: "<?=base_url()?>bookacts/delbook/",
data: ({id: bookID}),
cache: false,
success: function (msg){
if(msg == ""){
$('#ajax-load-'+bookID).hide();
$('.box-error').fadeIn("slow");
$('#action-'+bookID).fadeIn();
} else {
$('#ajax-load-'+bookID).hide();
$('.box-success').fadeIn("slow")
.animate({opacity: 1.0}, 2000)
.fadeOut('slow');
$('#action-'+bookID).fadeIn();
}
}
}); return false;
} else {
return false;
}
});
});
</script>
$(文档).ready(函数(){
$('.book remove')。每个(函数(){
$(此)。单击(函数(){
var stats=confirm('您确定要删除此条目吗?');
如果(统计){
var bookID=$(this.attr(“id”).replace(“remove-”,”);
$('#action'+bookID).hide();
$('#ajax加载-'+bookID).show();
$.ajax({
键入:“POST”,
url:“bookacts/delbook/”,
数据:({id:bookID}),
cache:false,
成功:功能(msg){
如果(msg==“”){
$('#ajax加载-'+bookID).hide();
$('.box错误').fadeIn(“慢”);
$('#action-'+bookID).fadeIn();
}否则{
$('#ajax加载-'+bookID).hide();
$('.box success').fadeIn(“慢”)
.animate({opacity:1.0},2000年)
.fadeOut(“慢”);
$('#action-'+bookID).fadeIn();
}
}
});返回false;
}否则{
返回false;
}
});
});
然而,我认为即使你改变了这些事情,你的问题可能还没有解决。但如果不看看它的作用,就很难找出问题所在
只要查看javascript是否正确(使用ID)的源代码,检查您是否收到JS确认消息。检查它是否符合AJAX,发出一些警报。检查是否存在javascript错误。等等
祝你好运