This is my controller, that i use to send the row to the model:
function excluir(){
$codcliente = $this->input->post('codcliente');
$this->load->model("Cliente_class");
$this->Cliente_class->excluirCliente($codcliente);
redirect('cliente/busca');
}
my model:
function excluirCliente($codcliente){
$this->db->delete('cliente', array('codcliente' => $codcliente));
}
my table (view):
<form action="#" method="POST" id="form">
<div style="float: left;">
<input type="button" onclick="cadastro();" value="Novo cliente" name="botao" class="btn btn-primary">
</div>
<div class="dataTable_wrapper">
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>Código</th>
<th>Nome</th>
<th>Cidade</th>
<th>Telefone</th>
<th> </th>
</tr>
<tbody>
<?php
foreach ($records as $row){ ?>
<tr>
<td><input name="codcliente" id="codcliente" value="<?php echo $row->codcliente ?>"></td>
<td><?php echo $row->nome ?></td>
<td><?php echo $row->cidade ?></td>
<td><?php echo ($row->telefone == null) ? "-" : $row->telefone ?></td>
<td><center><input type="button" onclick="excluir();" value="Delete"></td>
</tr>
<?php } ?>
</tbody>
</form>
</thead>
</table>
and my JS:
function excluir(){
document.forms['form'].action = "<?php base_url();?>excluir";
document.forms['form'].submit();
}
its working fine, but the code is deleting my last row in relation with the table on html. i used the order by too see whats beeing deleted, and it works the same...
i dont know what's wrong
Your problem is that you are using the same form name name="codcliente" for all your rows and then submitting the whole form when you delete. The server sees the last value for codcliente which will be your last row and hence deletes it.
To fix, I suggest you use unique form names for each row for all your row fields. Eg. Instead of name="codcliente" use name="codcliente_<?php echo $row->id ?>". And then when you post to the server to delete, use a data-name=<?php echo $row->codcliente ?> on the delete button and in the onClick handler use $(this).data('name') to get the unique name of the field and post that to the server. I suggest you use ids instead of names as well.
Related
Help needed!
I have done coding using php mvc technique, using wamp server and tried using datatables(only for search functionality).
I have a table display in my Admin index page which fetches data from mysql database. I also have checkbox included at each row so that it can be selected and deleted.
...
<form action="<?php echo URLROOT; ?>/posts" method="post">
<button type="submit" value="Delete" name="delete_multiple_data">Delete Multiple Items</button>
<table id="mtab" class="display table table-striped table-bordered dt-responsive nowrap" style="width:100%">
<thead class="thead-dark">
<tr class="danger">
<tr class="danger">
<th scope="col">ID</th>
<th scope="col">Pack Name</th>
<th scope="col">Select</th>
</tr>
</thead>
<tbody>
<?php foreach($data['posts'] as $post) : ?>
<tr>
<td><?php echo $post->id; ?></td>
<td><?php echo $post->pckname; ?></td>
<td><input type="checkbox" name="delete[]" value="<?php echo $post->id; ?>"></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</form>
...
my controller,
...
if(isset($_POST['delete_multiple_data'])){
if(isset($_POST['delete'])){
foreach($_POST['delete'] as $deleteid){
$deletepack = $this->postModel->deletePost($deleteid);
}
}
}
$this->view('posts/index', $data);
...
without table id="mtab"(removing table id="mtab") everything is working fine.
but for search to be enabled in my table id="mtab" i used the below script,
...
<script type="text/javascript">
$(document).ready(function(){
$("#mtab").DataTable({
});
});
</script>
...
if i use this script, it hides my checkboxes totally from display, please help
I have identified the error it was <table class="display table table-striped table-bordered dt-responsive nowrap" style="width:100%">
after I removed nowrap it was working fine, both my checkbox as well as search feature was visible.
Thanks all.....
I am developing an attendance management codeigniter app and I am using switchery switch plugin to update attendance status. The process will be:
System display the list of student with attendance status in checkbox with switchery plugin.
Using check or uncheck the checkbox with switchery plugin, if the student is present or absent or simply user change the status.
System sends the form using AJAX, if the student present 1 or absent 0, with student ID and attendance date.
And simply system will change the attendance status of student.
NOTE* THE VALUE OF $date $month $year $program_id $batch_id and $section_id is loaded before in controller function parameter.
And this my app's preview. I need to this form working using AJAX.
And this is my code for the view:
<!-- student attendence status update form -->
<form method="post" action="<?php echo base_url(); ?>index.php?admin/student_attendance">
<!-- start datatable -->
<table class="table table-hover dataTable table-striped width-full" id="departmentTableTools">
<thead>
<tr>
<th class="text-center"><?php echo get_phrase('rool'); ?></th>
<th class="text-center"><?php echo get_phrase('name'); ?></th>
<th class="text-center"><?php echo get_phrase('status'); ?></th>
</tr>
</thead>
<tbody>
<?php
foreach($students as $row):?>
<tr>
<td class="text-center"><?php echo $row['roll_no'];?></td>
<td class="text-center"><?php echo $row['name'];?></td>
<td class="text-center">
<input type="checkbox" value="1" name="status_<?php echo $row['student_id'];?>" data-plugin="switchery" <?php if($status == 1) echo 'checked'; ?>/>
</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
<input type="hidden" name="date" value="<?php echo $full_date;?>" />
</form>
<!-- end the attendance status update form -->
And this my controller code:
if($_POST) // if the update request is send
{
foreach ($students as $row)
{
$this->crud_model->update_student_attendance($students['student_id'], $this->input->post('status_' . $row['student_id']), $this->input->post('date'));
}
$this->session->set_flashdata('flash_message' , get_phrase('data_updated'));
redirect(base_url() . 'index.php?admin/student_attendance/'.$date.'/'.$month.'/'.$year.'/'.$program_id.'/'.$batch_id , 'refresh');
}
And this is my model code to update and db values:
function update_student_attendance($student_id, $attendance_status, $full_date){
$this->db->where('student_id' , $student_id);
$this->db->where('date' , $full_date);
$this->db->update('attendance' , array('status' => $attendance_status));
}
I have a view page (ikhtisar_view). And when I click one ID, it show modal (link to ikhtisar/detail) contain data for that ID only.
Here's my ikhtisar view td (contain data which I can click for detail):
<td><a href="<?php echo base_url();?>index.php/ikhtisar/detail" class='iframe'><?php echo $ikh['id']?></a></td>
This is my controller function:
public function detail()
{
$this->load->model('ikhtisar_model');
$detailr = $this->ikhtisar_model->trx();
$data['detail'] = $detailr;
$this->load->view('ikhtisar/trx_view',$data);
}
And this is my model function:
public function trx()
{
$ikh = $this->input->post('id');
$this->db->select('t.id_transaksi AS id, t.tgl_transaksi AS tgl, t.id_member AS memberid, t.nama AS nama, p.kode_produk AS kode, tp.hp_tujuan AS nohp, d.nama AS nama2, t.saldo_awal AS saw, tp.hpp AS hpp, t.saldo_akhir AS sak, tp.laba AS laba, t.ket AS ket, sr.ket AS ket2, sr.jenis AS sj', FALSE)
->from('transaksi_pulsa tp')
->join('transaksi t','tp.id_transaksi=t.id_transaksi', 'left')
->join('produk p','tp.kode_produk = p.kode_produk', 'left')
->join('set_report sr','t.status=sr.jenis', 'left')
->join('distributor d','tp.id_distributor=d.id_distributor and tp.com=d.com', 'left')
->where('t.id_transaksi', $ikh);
$ikhtisar = $this->db->get();
return $ikhtisar;
}
And finally this is my trx_view.php:
<table class="table table-striped table-hover table table-bordered" id="ikhtisar">
<thead style="display: table-header-group; vertical-align: middle; border-color: inherit;">
<tr>
<th>Status</th>
<th>No</th>
<th>Tanggal</th>
<th>Kode RS</th>
<th>Nama RS</th>
<th>Produk</th>
<th>Tujuan</th>
<th>Stok</th>
<th>Harga</th>
<th>Saldo Akhir</th>
<th>Laba</th>
</tr>
</thead>
<tbody>
<?php foreach($ikhlist->result_array() as $ikh): ?>
<tr>
<!--<td><?php echo $ikh['id']?></td>-->
<td style="font-size: 15px;" align="center"><?php if($ikh['sj'] == '1'){?><span><i class="glyphicon glyphicon-ok green"></i></span><?php }elseif($ikh['sj'] == '2'){ ?><span><i class="glyphicon glyphicon-remove red"><?php }else{ ?><span><i class="glyphicon glyphicon-question-sign yellow"><?php }?></i></span></td>
<td><?php echo $ikh['id']?></td>
<td><?php echo $ikh['tgl']?></td>
<td><?php echo $ikh['memberid']?></td>
<td><?php echo $ikh['nama']?></td>
<td><?php echo $ikh['kode']?></td>
<td><?php echo $ikh['nohp']?></td>
<td><?php echo $ikh['nama2']?></td>
<td class="text-right"><?php echo number_format($ikh['hpp'], 0, ",", ".")?></td>
<td class="text-right"><?php echo number_format($ikh['sak'], 0, ",", ".")?></td>
<td><?php echo $ikh['laba']?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
But I got just a page (trx_view.php) with no data. It contain only header. So, how to show the correct data? In ikhtisar_view (before click id for detail), no problem.
Try to debug your code on your own,
try to print the query from the model, by using
echo $this->db->last->query()
Try to run that query on the above received query database, and check whether you are getting the data or not.
And then try to print the $detail variable in the view and check whether your view is actually getting the data or not.
If I use this in my ikhtisar_view:
<td>
<form method="post" action="<?php echo base_url();?>index.php/ikhtisar/detail">
<input type="hidden" name="id"
value="<?php echo $ikh['id']; ?>" />
<input type="submit" value="<?php echo $ikh['id']; ?>" />
</form>
</td>
Instead of:
<td><a href="<?php echo base_url();?>index.php/ikhtisar/detail" class='iframe' name="id">
<input name="id" value="<?php echo $ikh['id']; ?>" />
</a></td>
The form-method succeed with exactly data I clicked (id). But, this is not modal. How to convert it to modal?
I already try add class='iframe' to that method, it didn't worked. Modal show up, but blank, no data, no header, no footer.
So I'm creating a database for my website and I want to create an admin section that allows you to add or delete from the table. Here is a snapshot of what I want to achieve...
In my php file I have if($_POST['delete_category']) which correctly gets the delete button clicks, but then I'm not sure how to distinguish which delete button was actually clicked. I'm sure this is a very simple solution, but I'm stuck. Thanks!
You can discern which button is submitted by this following markup (based on your example fetched results from DB):
<?php
if(isset($_POST['delete_category'])) {
$id = $_POST['delete_category']; // the value="1" or value="3" goes in here
echo $id;
}
?>
<form method="POST" action="">
<table border="1">
<tr>
<th>ID</th><th>Name</th><th>Delete</th>
</tr>
<tr>
<td>1</td>
<td>Senior Pictures</td>
<td><button typpe="submit" name="delete_category" value="1">Delete</button></td>
<!-- each delete button has the same name, but different values -->
</tr>
<tr>
<td>3</td>
<td>Engagements</td>
<td><button typpe="submit" name="delete_category" value="3">Delete</button></td>
</tr>
</table>
</form>
If I had to guess this makes sense on the fetching: (Note: Just a sample!)
<form method="POST" action="">
<table border="1">
<tr>
<th>ID</th><th>Name</th><th>Delete</th>
</tr>
<?php while($row = $results->fetch_assoc()): ?> <!-- assuming mysqli() -->
<?php while($row = mysql_fetch_assoc($result)): ?> <!-- assuming on mysql (bleh) -->
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td>
<button typpe="submit" name="delete_category" value="<?php echo $row['id']; ?>">Delete</button>
</td>
</tr>
<?php endwhile; ?>
</table>
</form>
I'm going to try to explain this as best I can. I'm returning an array of results from my controller. I wish to send over the primary key of the selected row to another controller that will manage inserting the record into the Database when a user clicks the "add employee" button. My primary key is MOVIE_ID. It's currently returning an array of two MOVIE_ID {12341, 31351} like so. My questions are:
How do I associate each movie ID uniquely with that corresponding rows' button?
Can I pass a variable to a controller through the action="" attribute , do I need JS?
<fieldset id= "results">
<?php if (isset($results ) And count($results)) : ?>
<table id="box-table-a" summary="Employee Sheet">
<thead>
<tr>
<th scope="col">Employee</th>
<th scope="col">Salary</th>
<th scope="col">Bonus</th>
<th scope="col">Supervisor</th>
<th scope="col">Action</th>
</tr>
<?php foreach ($results as $result) : ?>
<tr>
//Primary key <?php echo $result['MOVIE_ID!'];?>
<td><span class="Employee_name"><?php echo $result['Employee']; ?> </span></td>
<td><span class="Salary"><?php echo $result['Salary']; ?> </span> </td>
<td><span class="Bonus"><?php echo $result['Bonus']; ?> </span> </td>
<td><span class="Supervisor_name"><?php echo $result['Supervisor']; ?> </span></td>
<td><input type="submit" value="add employee" > </td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</thead>
<tbody>
</fieldset>
One trick is to pass the id via the submit button, like so:
<input name="submit[12341]" type="submit" value="add employee" />
When clicked it would send submit[12341]=add+employee in the POST data (assuming you have wrapped everything inside a <form> tag). The other buttons will not be submitted, only the one that's clicked.
In PHP you can retrieve the id like so:
$id = key($_POST['submit']);