issue to get multiple checkbox array values Codeigniter - php

Facing issue to get multiple checkbox array values in Codeignite
<div class="form-actions">
<form action="candidates/posted" name="posted" method="post">
<button class="btn btn-primary" type="submit" value="submit" name="submit">Action</button>
</div>
<table class="table table-striped table-bordered table-condensed">
<thead>
<tr>
<th class="header">#</th>
<th class="checkbox">Status</th>
<th class="yellow header headerSortDown">Roll No</th>
<th class="green header">Class Name</th>
<th class="red header">Full Name</th>
<th class="red header">Father Name</th>
<th class="red header">Sections</th>
<th class="red header">Actions</th>
</tr>
</thead>
<tbody>
<?php
foreach($candidates as $row)
{
echo '<tr>';
echo '<td>'.$row['id'].'</td>';
//echo '<td>' .'</td>';
echo '<td><input type="checkbox" class ="chkCheckBoxId" value='.$row['id'].' name="chk_id[]"/></td>';
echo '<td>'.$row['roll_no'].'</td>';
echo '<td>'.$row['class_name'].'</td>';
echo '<td>'.$row['full_name'].'</td>';
echo '<td>'.$row['father_name'].'</td>';
echo '<td>'.$row['section_name'].'</td>';
echo '<td class="crud-actions">
view & edit
delete
</td>';
echo '</tr>';
}
?>
</form>
Controller here:
public function posted()
{
if(!empty($_POST['chk_id'])) {
foreach($_POST['chk_id'] as $check) {
echo $check;
}
}
}

Just try like this inside foreach loop.There is conflict on using quotes.
$id = $row['id'];
echo '<td><input type="checkbox" class ="chkCheckBoxId" value="'.$id.'" name="chk_id[]"/></td>';
In codeigniter you can use $this->input->post() to get posted values.So following is a better way..
public function posted()
{
if(!empty($this->input->post('chk_id')) {
foreach($this->input->post('chk_id') as $check) {
echo $check;
}
}
}

Correct me on this but I believe that an unchecked checkbox does not get sent back to the server. I have always checked to see if a checkbox is checked by seeing if it is in the $_POST[] array (I tend to use method="post").
When I need an array of checkboxes, I actually give them their own names. If I am listing a group of products for users to choose, for example, I will give each checkbox a name that starts with "chk_" and then append the ID of the product. This way, during processing the data, all I have to do is loop through the $POST[] array and every object that starts with chk is a checkbox and the rest indicates which product they chose.

Related

table display having a column of checkboxes gets hidden when using datatable

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.....

Populate Dropdown Boxes while also Populating Table

I have a table that is populated using a foreach loop. One of my columns is SKU Group, which is a column of dropdown boxes. However, whenever the foreach loop runs, only the one corresponding value for that row is displayed inside the dropdown box.
How can I get it so that I can run the foreach loop and it correctly populates the table, while the dropdowns are also populated with every SKU Group name and not just the single value that corresponds with each row?
Normally, I would just run this foreach($var->fetchAll() as $var1) for the dropdowns to populate them, but I dont think that it can be ran properly inside another loop that is already running. So that is why I am having this issue.
HTML Table:
<table id="skuTable" cellspacing="5" class="ui-widget ui-widget-content">
<thead>
<tr class="ui-widget-header">
<th style="display: none">Product ID</th>
<th class="skuRow">Major Category</th>
<th class="skuRow">Minor Category</th>
<th class="skuRow">Report Code</th>
<th class="skuRow">SKU</th>
<th class="skuRow">SKU Description</th>
<th class="skuRow">SKU Status</th>
<th class="skuRow">Create Date</th>
<th class="skuRow">SKU Group</th>
<th class="skuRow">Edit</th>
</tr>
</thead>
<tbody>
<?php foreach ($dbh->query($query) as $row) {?>
<tr>
<td style="display: none" class="prod_id" id="product_id-<?php echo intval ($row['Product_ID'])?>"><?php echo $row['Product_ID']?></td>
<td class="major_cat" id="major_cat-<?php echo intval ($row['Major Category'])?>"><?php echo $row['Major Category']?></td>
<td class="minor_cat" id="minor_cat-<?php echo intval ($row['Minor Category'])?>"><?php echo $row['Minor Category']?></td>
<td class="rep_code" id="rep_code-<?php echo intval ($row['Product Report Code'])?>" align="center"><?php echo $row['Product Report Code']?></td>
<td class="sku" id="sku-<?php echo intval ($row['SKU'])?>" align="center"><?php echo $row['SKU']?></td>
<td class="sku_desc" id="sku_desc-<?php echo intval ($row['SKU Description'])?>"><?php echo $row['SKU Description']?></td>
<td class="sku_status" id="sku_status-<?php echo intval ($row['SKU Status'])?>" align="center"><?php echo $row['SKU Status']?></td>
<td class="create_date" id="create_date-<?php echo intval ($row['Date'])?>" align="center"><?php echo $row['Date']?></td>
<td class="sku_group" id="sku_group-<?php echo intval ($row['SKU Group'])?>" align="center">
<select id="sku_group_dropdown">
<option
value=""
data-name="<?php echo $row ['SKU Group'];?>"
>
<?php echo $row ['SKU Group'];?>
</option>
</select>
</td>
<td><input type="button" class="edit" name="edit" value="Edit"></td>
</tr>
<?php } ?>
</tbody>
</table>
first need to populate your dropdown list using loop
<?php
$i = 0;
$content = '';
$names = array('Test1','Test2');
foreach ($names as $row) {
if($i== 0){
$names = array('Test1','Test2');// replace it echo $row ['SKU Group'];
foreach($names as $key)
{
$content .= '<option value="'.$key.'">'.$key.'</option>';
}
}
$i++;
?>
<tr>
<td>sadasdsa</td>
<td class="sku_group" id="sku_group-<?php echo intval ($row['SKU Group'])?>" align="center">
<select id="sku_group_dropdown">
<?php echo $content?>
</select>
</td>
<td><input type="button" class="edit" name="edit" value="Edit"> </td>
</tr>
<?php } ?>
in this code replace your array data or if you want to populate dropdown data outside your loop then first get your group data for you can use this code

MySQL is deleting the last row of html table / Code Igniter

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.

jQuery.FilterTable doesn't work on a table created with php

I found this plugin JQuery.FilterTable for searching on a table bun when i put at my table doesn't work .
I don't know what is the problem but from my opinion i think is because the table is made with PHP and the datas for table are from database.
The code lis like this:
<div class="issue-table">
<table class="table table-striped" id="data-table">
<thead>
<tr>
<th scope="col" title="President Number">Nr:</th>
<th scope="col">Brand</th>
<th scope="col">Model</th>
<th scope="col">Color</th>
<th scope="col">Release date</th>
<th scope="col">Price</th>
<?php
if ($user->data()->type == 1) {
echo "<th>Delete</th>";
}
?>
</tr>
</thead>
<tbody>
<?php
$x = 1;
foreach ($car as $index => $car_val) {
$cars = DB::table('brands')->get(['id','=',$car_val->id_make]);
$car_name = $cars->first()->name;
echo "<tr>";
echo "<td>".$x++."</td>";
echo "<td>".$car_name."</td>";
echo "<td>".$car_val->model."</td>";
echo "<td>".$car_val->color."</td>";
echo "<td>".$car_val->release_date."</td>";
echo "<td>".$car_val->value."</td>";
if ($user->data()->type == 1) {
echo
"<td>
<form action='index.php?page=carlist' method='POST'>
<input type='hidden' value='".$car_val->id."' name='id'>
<input type='submit' value='X' class='btn btn-danger'>
</form>
</td>";
}
echo "</tr>";
}
?>
</tbody>
</table>
I used this code but isn't working:
<script>
$('table').filterTable();
</script>
How can I make this plugin to work?

Is it possible to have a cursor like operation in codeigniter

I am creating a billing application and I am using codeigniter. I have a view where I can view the employee details.In this view file I have a action like delete and edit separately for each employee record. Is is possible to have a single edit button that can edit any employee record that is listed in my view?
My view file
<table class="resizable" bordercolor="#993300" border="1">
<thead>
<tr>
<th class="header">Employee id</th>
<th class="yellow header headerSortDown">First name</th>
<th class="green header">Last name</th>
<th class="red header">Email</th>
<th class="red header">Emergency contact</th>
<th class="red header">Category</th>
<th class="red header">ID card</th>
<th class="red header">Time in</th>
<th class="red header">Time out</th>
<th class="red header">Date of hire</th>
<th class="red header">Date of termination</th>
<th class="red header">Date of rehire</th>
<th class="red header">Reference number</th>
<th class="red header">Service limitation</th>
<th class="red header">Chair renter</th>
<th class="red header">Actions</th>
</tr>
</thead>
<tbody>
<?php
foreach($employee as $row)
{
echo '<tr>';
echo '<td>'.$row['id'].'</td>';
echo '<td>'.$row['emp_first_name'].'</td>';
echo '<td>'.$row['emp_last_name'].'</td>';
echo '<td>'.$row['emp_email_id'].'</td>';
echo '<td>'.$row['emp_emergency_contact'].'</td>';
echo '<td>'.$row['category'].'</td>';
echo '<td>'.$row['emp_id_card'].'</td>';
echo '<td>'.$row['emp_time_in'].'</td>';
echo '<td>'.$row['emp_time_out'].'</td>';
echo '<td>'.$row['emp_date_of_hire'].'</td>';
echo '<td>'.$row['emp_date_of_termination'].'</td>';
echo '<td>'.$row['emp_date_of_rehire'].'</td>';
echo '<td>'.$row['emp_reference_num'].'</td>';
echo '<td>'.$row['emp_service_limitation'].'</td>';
echo '<td>'.$row['chair_renter'].'</td>';
echo '<td class="crud-actions">
view & edit
delete
</td>';
echo '</tr>';
}
?>
</tbody>
</table>
Here i have a <a> to perform edit and delete option. This action passes the id that i choose to edit or delete. Instead of having separate buttons for each employee.I need to have a single button and allow the user to select employee.Like this image
The easiest solution would be to use JQuery to attach a selected class to the row which is clicked via something like:
$('tr').on('click', function () {
// Remove selection from other rows
$('tr.selected').removeClass('selected');
// Add selection to current row
$(this).addClass('selected');
});
That will allow you to place a 'cursor' onto a row, you'll want to style the rows with the selected class using CSS or something similar.
Now you can have a single Edit button and use something like the following code to handle the click event:
$('#edit').on('click', function () {
if ($('tr.selected').length() == 0) {
alert('Must select a row!');
} else {
// Handle 'edit' event by passing $(this) to whatever you need
// $(this) will be the currently selected row
}
});
This works since the $(this) object acts as a reference to the currently selected row.

Categories