Selected Data only post within the same page PHP CodeIgniter - php

i have paging data table with checkbox on it,when i selected the checkbox,it only post the selected checkbox in the same page,here is my view :
<label>Category</label>
<select name="cat" id="cat>
<option value="Motorcycle">Motorcycles</option>
<option value="Motorcycle">Cars</option>
</select>
<table id="tabelgg" class="table table-bordered">
<thead>
<tr>
<th>NO.</th>
<th>NAME</th>
<th>ACTION</th>
</tr>
</thead>
<tbody>
<?php
$no=1;
foreach ($cats as $data) {
echo "<tr>";
echo "<td>".$no."</td>";
echo "<td>".$data['name']."</td>";
echo "<td><center><input type='checkbox' name='desc[]' id='desc[]' value='".$data['id_list']."'></center></td></td>";
echo "</tr>";
$no++;
}
?>
</tbody>
</table>
and this is my save controller
if(isset($_POST['desc']))
{
if (is_array($_POST['desc']))
{
foreach($_POST['desc'] as $check){
$data_=array('cat' => $this->input->post('cat'),
'item_head' => $check);
}
} else {
$value = $_POST['ket'];
echo $value;
}
$this->db->insert('tbl_pengelompokan',$data_);
}
redirect('admin/pengelompokan','location');
how do i make change for my controller view so it will post one-to-many data ?

You can use DataTable javascript plugin to do that. I use server side script to show many data as well on one request as dynamic.

Related

DATATABLE Cannot set properties of undefined (setting '_DT_CellIndex')

issue getting in datatable while i m using loop array
Column are equal thead and tbody still getting issue.
i m faching data from command and using array i want output in datatable.output is working but datable shorting and search option is not working.
<div class="m-t-25">
<table id="data-table" class="table dataTable">
<thead>
<tr>
<?php
foreach ($output[0] as $clave=>$fila) {
echo "<th>".$fila."</th>";
}
?>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$output = array_slice($output, 2);
foreach ($output as $fila) {
echo "<tr>";
foreach ($fila as $elemento) {
echo "<td>".$elemento."</td>";
}
echo '<td><select class="act">';
echo '<option value="watch" selected="">graph</option>';
echo '</select></td>';
echo "</tr>";
}
?>
</tbody>

Filtering data values according to live dropdown

Currently I have a table in my view class that is populated with data from the backend using MVC framework in codeigniter. Now I have a dropdown above each column that is filling in the same records from my database. So I want to be able to filter my records as soon as the person clicks the item in the dropdown list.
I know how to filter records when a submit button is clicked, but I want this filtering to happen in live time as soon as the user clicks the dropdown item, for which I'm assuming I'll need to make an AJAX call which I'm not familiar with.
So far I have this in my view class:
<table>
<tr>
<th width="10%">Source</th>
</tr>
<tr>
<td width="5%"><select>
<option value="">All </option>
<?php if($sources) foreach($sources as $source): ?>
<option value="<?php echo $source['title'] ?>"><?php echo $source['title'] ?></option>
<?php endforeach;?>
</select></td>
</tr>
<tbody>
<?php
if(isset($records) && count($records) > 0)
{
foreach($records as $row ){
?>
<tr>
<td><?= $row->source ?></td>
</tr>
<?php } } ?>
</tbody>
Here default the select shows Any with a null value to show all records, and the backend data have thier separate values underneath. Now I want to have a live filtering here. Additionally here I have included only 1 dropdown, but there are multiple dropdowns, so it should make the filtering according to all the dropdown values provided.
Just create an ajax function to receive the request when the option change:
$route['ajax_dropdown'] = 'your_controller/your_function'; // change ajax_dropdown to whatever you want
Then in your controller:
function your_function()
{
$input_value = $this->input->get('your_input_name'); // get the input value of jQuery's get request
$data = array(); // store data in here
// do something like: $data['status'] = $this->your_model->get_status();
echo json_encode($data); // json_encode to make your data object become a string to send }
Finally, in your view you can send ajax request to get data. i prefer to use jQuery so I write it here:
<table>
<tr>
<th width="10%">Source</th>
</tr>
<tr>
<td width="5%">
<select id="your_id_name">
<option value="">All </option>
<?php if ($sources) foreach ($sources as $source) : ?>
<option value="<?php echo $source['title'] ?>"><?php echo $source['title'] ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
<tbody>
<?php
if (isset($records) && count($records) > 0) {
foreach ($records as $row) {
?>
<tr>
<td><?= $row->source ?></td>
</tr>
<?php }
} ?>
</tbody>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$('#your_id_name').on('change', function() {
$.get('<?php echo base_url('ajax_dropdown'); ?>', {
your_input_name: 'your_value' // input value of get request
}, function(res) {
var values = JSON.parse(res); // then do something
})
})
</script>

foreach returning the same row 4 times codeigniter

I'm using CodeIgniter and I'm trying to populate a <table> and just getting the same row 4 times.
My model:
$sql2=
"SELECT *
FROM puntos
WHERE checklist_idchecklist='$idchecklist'";
$qry2=$this->db->query($sql2);
$puntos=$qry2->row();
return $puntos
This query returns 2 arrays of 4 attributes each, i tested it doing the SQL query on phpmyadmin.
My controller:
function verpuntos() {
$this->load->model('checklistm');
$puntos=$this->checklistm->obtenerpuntos();
$this->load->view('plantilla/headerguardia');
$this->load->view('checklist/lista', $puntos);
$this->load->view('plantilla/footer');
}
My view:
$punto=array(
'descripcion' => $descripcion,
'lugar' => $lugar,
'tipo' => $tipo,
'urgencia' => $urgencia);
<table class="table">
<thead>
<tr>
<th>Descripcion</th>
<th>Lugar</th>
<th>Tipo</th>
<th>Urgencia</th>
</tr>
</thead>
<tbody>
<?php foreach($punto as $row) {
echo '<tr>';
echo '<td>'.$descripcion.'</td>';
echo '<td>'.$lugar.'</td>';
echo '<td>'.$tipo.'</td>';
echo '<td>'.$urgencia.'</td>';
echo '</tr>';
}
?>
<tbody>
</table>
And this is what I'm getting, the same row 4 times:
In codeigniter row() fetch the first row in object format.$puntos=$qry2->row(); This statement fetch only first row of your fetched data in object fromat. SO no need to use foreach loop. Just try like this..
echo '<tr>';
echo '<td>'.$puntos->descripcion.'</td>';
echo '<td>'.$puntos->lugar.'</td>';
echo '<td>'.$puntos->tipo.'</td>';
echo '<td>'.$puntos->urgencia.'</td>';
echo '</tr>';
For more see here https://www.codeigniter.com/userguide3/database/results.html
<table class="table">
<tr>
<th>Descripcion</th>
<th>Lugar</th>
<th>Tipo</th>
<th>Urgencia</th>
</tr>
</thead>
<tbody>
<?php
echo '<tr>';
foreach($punto as $row)
{
echo '<td>'.$row.'</td>';
}
echo '</tr>';
?>
</tbody>
<table>
try like this
echo '<tr>';
echo '<td>'.$punto['item_1_name'].'</td>';
echo '<td>'.$punto['item_2_name']..'</td>';
echo '<td>'.$punto['item_3_name']..'</td>';
echo '<td>'.$punto['item_4_name']..'</td>';
echo '</tr>';
item_1_name = index name of array returned

PHP - If database record is a specific value, only show these buttons

I have a multi-step form that allows the user to save for later or submit. If they save for later, i want them to have an option to edit or delete but if they submit, i dont want them to have any option to edit or delete. Although the following code makes sense, its not working; i'm not getting any buttons and it breaks the jquery table plugin and i dont have the search or pagination option:
<table id="myTable" class="table table-bordered table-hover table-striped tablesorter table-responsive">
<thead>
<tr>
<td>Id</td>
<td>Status</td>
<td>Date/Time</td>
<td>Tag</td>
<td>Serial Number</td>
<td>Equipment</td>
<td>Company</td>
<td>Grand Total</td>
<td>Action</td>
</tr>
</thead>
<tbody>
<?php
while($res = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$res['id']."</td>";
echo "<td><span class='label ".$res['labelwarning']."'>".$res['status']."</span></td>";
echo "<td>".$res['todaysdatetime']."</td>";
echo "<td>".$res['tag']."</td>";
echo "<td>".$res['serialnumber']."</td>";
echo "<td>".$res['currentequipment']."</td>";
echo "<td>".$res['company']."</td>";
echo "<td>".$res['total_grandtotal']."</td>";
$Submitted = "Submitted";
$Saved = "Saved";
if ($res['labelwarning'] == $Submitted) {
echo "<td></td>";
} elseif ($res['labelwarning'] == $Saved) {
echo "<td></td>";
}
echo "</tr>";
}
?>
<Script>
$(document).ready(function() {
$("#myTable").dataTable({
"aaSorting": [ [0,'desc'] ]
});
});
</Script>
Oke I will try to explain this process here:
1 - First you will create a php file where you will fetch the results you need.
when you are fetching the records you will put your results in a variable like so:
$result_html = '';
while($res = mysqli_fetch_array($result)) {
$result_html .= "<tr>";
$result_html .= "<td>".$res['id']."</td>";
...en so on
$result_html .= "</tr>";
}
To return the response to your ajax call you need to add also this:
echo json_encode($result_html);
2 - Before the end </body> tag
<script>
$(document).ready(function() {
$.ajax({
url: '/path/to/the_php_file',
type: 'POST',
dataType: 'json',
data : { //only needed if you have to pass parameters to your query },
})
.done(function(data) {
//data is your html response
// and #returnedResponse is the id given to your body tag
$('#returnedResponse').html(data);
$("#myTable").dataTable();
})
.fail(function() {
console.log("error");
});
});
</script>
3 - Your table
<table id="myTable" class="table table-bordered table-hover table-striped tablesorter table-responsive">
<thead>
<tr>
<td>Id</td>
<td>Status</td>
<td>Date/Time</td>
<td>Tag</td>
<td>Serial Number</td>
<td>Equipment</td>
<td>Company</td>
<td>Grand Total</td>
<td>Action</td>
</tr>
</thead>
<tbody id="returnedResponse">
</tbody>
</table>
NOTE:
You need to have the link to the jquery.js before your ajax call.
This is not a copy and paste solution. You asked for an example on how to do it and here you have a good start point to learn it.
Try to change your if/else part to this:
if ($res['labelwarning'] == $Submitted) {
echo "<td></td>";
} else{
echo "<td></td>";
}

PHP blog categories duplication

I have got a question regarding setting up proper categories for my posts that would be done automatically. I'm using PHP.
Here is my sample example document from the database:
{
"_id" : "css-clearfix-explained",
"title" : "CSS Clearfix Explained",
"category" : [
"CSS/HTML",
" Wordpress"
],
"date_time" : ISODate("2014-08-13T21:03:45.367Z"),
"description" : "Blalalal ",
"content" : " ",
"author" : "Maciej Sitko"
}
So, for that purpse I wrote the code that inserts the categories in the view page, to be more specific, it inserts them into the table. This is the code:
<?php if(!isset($_GET['cat'])) {
$categories = $collection->find();?>
<table class="table table-hover table-striped">
<thead class='table-head' >
<tr ><th colspan='2'>Categories</th></tr>
</thead>
<tbody>
<?php foreach($categories as $category) {
foreach($category as $keys) {
if((is_array($keys)) && (!empty($keys))) {
foreach($keys as $key => $value) { ?>
<tr>
<td><a class="normalize" href="">
<?php echo $keys[$key]; ?></a></td>
<td class="small"> Posts</td>
</tr>
<?php } } } } ?>
</tbody>
</table>
The problem is, and you see it (I bet it), that when it is executed this way there would be duplicates of the categories in the table shown. How can I prevent those categories from repeating themselves in the listing? I know its a rookie question, but I'm still learning.
You could write $category into an array and every iteration - before displaying your data - you could check if $category is already in there. You could use "in_array": http://php.net/manual/de/function.in-array.php
<?php if(!isset($_GET['cat'])) {
$categories = $collection->find();?>
<table class="table table-hover table-striped">
<thead class='table-head' >
<tr ><th colspan='2'>Categories</th></tr>
</thead>
<tbody>
<?php
$uniqueCats = array();
foreach($categories as $category) {
foreach($category as $keys) {
if((is_array($keys)) && (!empty($keys))) {
foreach($keys as $key => $value) {
if( in_array($value, $uniqueCats) ) { continue; }
$uniqueCats[] = $value;
?>
<tr>
<td><a class="normalize" href="">
<?php echo $value; ?></a></td>
<td class="small"> Posts</td>
</tr>
<?php } } } } ?>
</tbody>
</table>
I hope that's what you were looking for :)
The code/variables slightly differs from how I would read the data in the example so I might have misinterpreted your question :)

Categories