Я новичок, может кто-нибудь дать мне несколько советов и совет, как мне получить данные некоторых пользователей. Я создаю резервирование, и я хочу, чтобы, когда пользователь входит в систему, он видит свою историю резервирования и может отменить или удалить ее. Я просто хочу получить только те данные о резервировании, но что происходит, когда он выбирает все данные в базе данных, и еще одна проблема заключается в том, что мое редактирование не работает и оно не выдает ошибку, и я пытаюсь поставить другое условие Мое утверждение if else: редактирование работает, а добавление - нет. я действительно смущен тем, что я собираюсь сделать
historycont
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Historycont extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('book_model');
}
public function index()
{
$data['occasion']=$this->book_model->get_all_books();
$this->load->view('history',$data);
}
public function book_add()
{
$data = array(
'username' => $this->input->post('username'),
'mobilenum' => $this->input->post('mobilenum'),
'homeadd' => $this->input->post('homeadd'),
'occasiontype' => $this->input->post('occasiontype'),
'startdate' => $this->input->post('startdate'),
'enddate' => $this->input->post('enddate'),
);
$insert = $this->book_model->book_add($data);
echo json_encode(array("status" => TRUE));
}
public function ajax_edit($id)
{
$data = $this->book_model->get_by_id($id);
echo json_encode($data);
}
public function book_update()
{
$data = array(
'username' => $this->input->post('username'),
'mobilenum' => $this->input->post('mobilenum'),
'homeadd' => $this->input->post('homeadd'),
'occasiontype' => $this->input->post('occasiontype'),
'startdate' => $this->input->post('startdate'),
'enddate' => $this->input->post('enddate'),
);
$this->book_model->book_update(array('id' => $this->input->post('id')),
$data);
echo json_encode(array("status" => TRUE));
}
public function book_delete($id)
{
$this->book_model->delete_by_id($id);
echo json_encode(array("status" => TRUE));
}
}
history_model
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class history_model extends CI_Model
{
var $table = 'occasion';
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function get_all_books()
{
$this->db->from('occasion');
$query=$this->db->get();
return $query->result();
}
public function get_by_id($id)
{
$this->db->from($this->table);
$this->db->where('id',$id);
$query = $this->db->get();
return $query->row();
}
public function book_add($data)
{
$this->db->insert($this->table, $data);
return $this->db->insert_id();
}
public function book_update($where, $data)
{
$this->db->update($this->table, $data, $where);
return $this->db->affected_rows();
}
public function delete_by_id($id)
{
$this->db->where('id', $id);
$this->db->delete($this->table);
}
}
history_view
<div class= "col-lg-8 col-lg-offset-2">
<h1>Profile Page</h1>
HELLO, <?php echo $_SESSION['username']; ?>
<br><br>
<a href="<?php echo base_url(); ?>index.php/auth/logout">Logout</a>
<div class="container">
<h1>History</h1>
</center>
<h3>Book List</h3>
<br />
<button class="btn btn-success" onclick="add_book()"><i class="glyphicon
glyphicon-plus"></i> Add Book</button>
<br />
<br />
<table id="table_id" class="table table-striped table-bordered"
cellspacing="0" width="100%">
<thead>
<tr>
<th>ID</th>
<th>Username</th>
<th>Mobile Number</th>
<th>Home Address</th>
<th>Occasion Type</th>
<th>Start Date</th>
<th>End Date</th>
<th style="width:125px;">Action
</p></th>
</tr>
</thead>
<tbody>
<?php foreach($occasion as $book){?>
<tr>
<td><?php echo $book->id;?></td>
<td><?php echo $book->username;?></td>
<td><?php echo $book->mobilenum;?></td>
<td><?php echo $book->homeadd;?></td>
<td><?php echo $book->occasiontype;?></td>
<td><?php echo $book->startdate;?></td>
<td><?php echo $book->enddate;?></td>
<td>
<button class="btn btn-warning" onclick="edit_book(<?php echo $book->id;?
>)">
<i class="glyphicon glyphicon-pencil"></i></button>
<button class="btn btn-danger"
onclick="delete_book(<?php echo $book->id;?>)"><i class="glyphicon
glyphicon-remove"></i></button>
</td>
</tr>
<?php }?>
</tbody>
<tfoot>
<tr>
<th>ID</th>
<th>Username</th>
<th>MobileN umber</th>
<th>Home Address</th>
<th>Occasion Type</th>
<th>Start Date</th>
<th>End Date</th>
<th>Action</th>
</tr>
</tfoot>
</table>
</div>
<script src="<?php echo base_url('assests/jquery/jquery-3.1.0.min.js')?
>"></script>
<script src="<?php echo
base_url('assests/bootstrap/js/bootstrap.min.js')?>"></script>
<script src="<?php echo
base_url('assests/datatables/js/jquery.dataTables.min.js')?>"></script>
<script src="<?php echo
base_url('assests/datatables/js/dataTables.bootstrap.js')?>"></script>
<script type="text/javascript">
$(document).ready( function () {
$('#table_id').DataTable();
} );
var save_method; //for save method string
var table;
function add_book()
{
save_method = 'add';
$('#form')[0].reset(); // reset form on modals
$('#modal_form').modal('show'); // show bootstrap modal
//$('.modal-title').text('Add Person'); // Set Title to Bootstrap modal title
}
function edit_book(id)
{
save_method = 'update';
$('#form')[0].reset(); // reset form on modals
//Ajax Load data from ajax
$.ajax({
url : "<?php echo site_url('book/ajax_edit/')?>/" + id,
type: "GET",
dataType: "JSON",
success: function(data)
{
$('[name="id"]').val(data.id);
$('[name="username"]').val(data.username);
$('[name="mobilenum"]').val(data.mobilenum);
$('[name="homeadd"]').val(data.homeadd);
$('[name="occasiontype"]').val(data.occasiontype);
$('[name="startdate"]').val(data.startdate);
$('[name="enddate"]').val(data.enddate);
$('#modal_form').modal('show'); // show bootstrap modal when complete loaded
$('.modal-title').text('Edit Book'); // Set title to Bootstrap modal title
},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Error get data from ajax');
}
});
}
function save()
{
var url;
if(save_method == 'add')
{
url = "<?php echo site_url('book/book_add')?>";
}
else
{
var url;
url = "<?php echo site_url('book/book_update')?>";
}
// ajax adding data to database
$.ajax({
url : url,
type: "POST",
data: $('#form').serialize(),
dataType: "JSON",
success: function(data)
{
//if success close modal and reload ajax table
$('#modal_form').modal('hide');
location.reload();// for reload a page
},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Error adding / update data');
}
});
}
function delete_book(id)
{
if(confirm('Are you sure delete this data?'))
{
// ajax delete data from database
$.ajax({
url : "<?php echo site_url('book/book_delete')?>/"+id,
type: "POST",
dataType: "JSON",
success: function(data)
{
location.reload();
},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Error deleting data');
}
});
}
}
</script>
<!-- Bootstrap modal -->
<div class="modal fade" id="modal_form" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-
label="Close"><span aria-hidden="true">×</span></button>
<h3 class="modal-title">Add Form</h3>
</div>
<div class="modal-body form">
<form action="#" id="form" class="form-horizontal">
<input type="hidden" value="" name="book_id"/>
<div class="form-body">
<div class="form-group">
<label class="control-label col-md-3">Username</label>
<div class="col-md-9">
<input name="username" placeholder="Username" class="form-control"
type="text">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Mobile Number</label>
<div class="col-md-9">
<input name="mobilenum" placeholder="Mobilenum" class="form-
control" type="text">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Home Address</label>
<div class="col-md-9">
<input name="homeadd" placeholder="Homeadd"
class="form-control" type="text">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Occasion Type</label>
<div class="col-md-9">
<input name="occasiontype" placeholder="Occasiontype" class="form-
control" type="text">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Start Date</label>
<div class="col-md-9">
<input name="startdate" placeholder="Startdate" class="form-
control" type="text">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">End Date</label>
<div class="col-md-9">
<input name="enddate" placeholder="Enddate"
class="form-control" type="text">
</div>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" id="btnSave" onclick="save()" class="btn btn-
primary">Save</button>
<button type="button" class="btn btn-danger" data-
dismiss="modal">Cancel</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<!-- End Bootstrap modal -->
</body>
</html>
структура дБ
--
-- Table structure for table `occasion`
--
CREATE TABLE `occasion` (
`username` varchar(55) NOT NULL,
`id` int(50) NOT NULL,
`mobilenum` varchar(50) NOT NULL,
`homeadd` varchar(50) NOT NULL,
`occasiontype` varchar(50) NOT NULL,
`startdate` date NOT NULL,
`enddate` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `occasion`
--
INSERT INTO `occasion` (`username`, `id`, `mobilenum`, `homeadd`, `occasiontype`,
`startdate`, `enddate`) VALUES
('piolo', 22, 'dsada', 'adada', 'ML', '2018-10-18', '2018-11-10'),
('piolo', 23, 'dsadada', 'adada', 'ML', '2018-10-18', '2018-11-10'),
('piolos', 24, 'adad', 'adada', 'adad', '2018-11-14', '2018-11-17'),
('hgfhf', 25, 'hfghf', 'fhfh', 'hfhf', '2018-10-18', '2018-10-19'),
('piolo', 26, 'dsada', 'adada', 'ML', '2018-11-01', '2018-11-10');
-- --------------------------------------------------------
CREATE TABLE `users` (
`user_id` int(11) NOT NULL,
`username` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`gender` varchar(255) NOT NULL,
`created_date` date NOT NULL,
`phone` varchar(255) NOT NULL,
`activated` tinyint(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `users`
--
INSERT INTO `users` (`user_id`, `username`, `email`, `password`, `gender`,
`created_date`, `phone`, `activated`) VALUES
(1, 'Krypto', 'jhonjerico@yahoo.com', 'e2246479f2c24d13d8f134b329cbcd85',
'Female', '2018-08-04', 'fdsfs', 0),
(34, 'abcd', 'johnhular282@yahoo.com',
'20eabe5d64b0e216796e834f52d61fd0b70332fc',
'Male', '0000-00-00', '9082079424', 1),
(35, 'abcde', 'johnhular282333@yahoo.com', 'fcea920f7412b5da7be0cf42b8c93759',
'Male', '0000-00-00', '9082079424', 0),
(39, 'piolo', 'hular17@gmail.com', 'e10adc3949ba59abbe56e057f20f883e', 'Male',
'0000-00-00', '9082079424', 1),
(40, 'aaaaa', 'batotswak2@yahoo.com', 'd0970714757783e6cf17b26fb8e2298f',
'Male',
'2018-09-17', '9082079424', 0),
(42, 'louie', 'andyreaasinas@yahoo.com', 'e10adc3949ba59abbe56e057f20f883e',
'Male',
'2018-10-22', '13213132131', 0),
(43, 'louie', 'andyreaasinas32@yahoo.com', 'e10adc3949ba59abbe56e057f20f883e',
'Male', '2018-10-22', '13213132131', 0),
(44, 'louie', 'andyreaasinas32@yahoo.com3', 'e10adc3949ba59abbe56e057f20f883e',
'Male', '2018-10-22', '13213132131', 0),
(45, 'intsik_potawatomi@yahoo.com', 'ianpilyo66@yahoo.com',
'e10adc3949ba59abbe56e057f20f883e', 'Male', '2018-10-22', '13213132131', 0),
(49, 'michael louie', 'christianhular@gmail.com',
'fcea920f7412b5da7be0cf42b8c93759',
'Male', '2018-10-24', '13213132131', 1);