I have a problem with datatable, it's working but it's not working right..
I have the next code:
<table id="dt_basic" class="table table-striped table-bordered table-hover" width="100%">
<thead>
<tr>
<th>Codigo PT</th>
<th><i class="text-muted hidden-md hidden-sm hidden-xs"></i>Producto Terminado</th>
</tr>
</thead>
<tbody>
<?php
foreach ($registros as $reg)
{
?>
<tr style="border-bottom:2px solid #000;background: #cee8ff;">
<td><?php echo $reg -> id_codigo;?></td>
<td><?php echo utf8_encode($reg ->id_product_term);?></td>
</tr>
<?php
$AllRows = $_interfaz -> get_all($reg -> id_interfaz);
foreach ($AllRows as $row)
{
?>
<tr>
<td><b style="font-size:20px;">—</b></td>
<td><?php echo $row -> id_product_term;?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
It has a forearch() { forearch() { } } two foreach, a nested foreach.
The problem is that the datatable put the first foreach result at last and the result of the nested (SECOND) foreach in the beginning, like this:
second foreach result
second foreach result
second foreach result
first foreach result
first foreach result
first foreach result
why does datable organize the table as above?
why does not it do this?:
first foreach result
second foreach result
first foreach result
second foreach result
first foreach result
second foreach result
because one foreach is nested..
how can i fix it?
my js script is:
$('#dt_basic').dataTable({
"sDom": "<'dt-toolbar'<'col-xs-12 col-sm-6'f><'col-sm-6 col-xs-12 hidden-xs'l>r>"+
"t"+
"<'dt-toolbar-footer'<'col-sm-6 col-xs-12 hidden-xs'i><'col-xs-12 col-sm-6'p>>",
"autoWidth" : true,
"oLanguage": {
"sSearch": '<span class="input-group-addon"><i class="glyphicon glyphicon-search"></i></span>'
}});
Thanks.
why does datable organize the table as above?
Because dataTables is ordering the rows by default as [[0, 'asc']]. Thats why all your rows with
<tr><td><b style="font-size:20px;">—</b></td>...</tr>
is shown first. There is a extension called rowGroup which purpose is exactly what you are trying to do. Include the rowGroup source, then add an extra invisible column to group by :
<tr style="border-bottom:2px solid #000;background: #cee8ff;">
<td><?php echo $reg -> id_codigo;?></td>
<td><?php echo $reg -> id_codigo;?></td>
<td><?php echo utf8_encode($reg ->id_product_term);?></td>
</tr>
...
<tr>
<td><?php echo $reg -> id_codigo;?></td>
<td><b style="font-size:20px;">—</b></td>
<td><?php echo $row -> id_product_term;?></td>
</tr>
Make the new group column invisible and rowGroup that column :
$('#dt_basic').dataTable({
columnDefs: [
targets: 0, visible: false }
],
rowGroup: {
dataSrc: 0
}
...
})
Here is an example of using rowGroup upon an invisible column -> http://jsfiddle.net/8r88gsfk/
Related
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>
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
I am using this code to create an infinite table for my mysql queries:
<table cellspacing='0'> <!-- cellspacing='0' is important, must stay -->
<!-- Table Header -->
<thead>
<tr>
<th>User</th>
<th>SteamID</th>
<th>Banned by</th>
<th>Admin SteamID</th>
<th>Time Banned (min)</th>
<th>Reason</th>
</tr>
</thead>
<!-- Table Header -->
<!-- Table Body -->
<tbody>
<?php
echo '<tr>';
for($i = 0; $bans = mysqli_fetch_array($query2); $i = ($i+1)%3){
echo '<td>'.$bans['name'].'</td>';
echo '<td>'.$bans['steamid'].'</td>';
echo '<td>'.$bans['nameAdmin'].'</td>';
echo '<td>'.$bans['steamidAdmin'].'</td>';
echo '<td>'.$bans['time'].'</td>';
echo '<td>'.$bans['reason'].'</td>';
if($i == 2)
echo '</tr><tr>';
}
echo '</tr>';
?>
</tbody>
I got that code from Mysql fetch array, table results
It works fine, except it doesn't CORRECTLY go further down than 6 rows. The other rows for whatever reason are placed to the right of my last column as shown in this screenshot:
http://puu.sh/h0qZF/a12de1dd87.png
How can I fix this? Is there something wrong with my code? Why is it happening?
Well, your looping makes no sense. Using $i to inject new rows, like is done here, is not necessary; you can just loop over each row and then output it as a row:
<table>
<!-- <thead>...</thead> -->
<tbody>
<?php while ($bans = mysqli_fetch_array($query2)): ?>
<tr>
<td><?php echo $bans['name'] ?></td>
<td><?php echo $bans['steamid'] ?></td>
<td><?php echo $bans['nameAdmin'] ?></td>
<td><?php echo $bans['steamidAdmin'] ?></td>
<td><?php echo $bans['time'] ?></td>
<td><?php echo $bans['reason'] ?></td>
</tr>
<?php endwhile ?>
</tbody>
</table>
You are making two columns.
You have code that will print out the end of the table row every two sets of data:
if($i == 2)
echo '</tr><tr>';
It should just be echo '</tr><tr>';
Use a while loop as instructed here . So something like this:
$result = $conn->query($sql);
while($bans = $result->fetch_assoc()) {
echo '<td>'.$bans['name'].'</td>';
echo '<td>'.$bans['steamid'].'</td>';
echo '<td>'.$bans['nameAdmin'].'</td>';
}
I'm a newbiw in Codeigniter and i so desperate facing this problem. So, I just made a simple application with CI. When I want to check an array of object (in this case array of $kunj->DIAG), I get an error notice.
Here's some appearance of my view:
View:
<table class="table table-striped table-bordered table-condensed" id="tablescroll" role="grid">
<thead>
<tr class="tablesorter-headerRow" role="row">
<th>No.</th>
<th>Nama</th>
<?php if(!empty($kunj->DIAG)){ echo "<th>ICD</th>"; } ?>
</tr>
</thead>
<tbody aria-live="polite" aria-relevant="all">
<?php if(!empty($kunj)) print_r(count($kunj));
if(!empty($kunj)){
$no = 0;
foreach ($kunj as $i): ?>
<tr align='center'>
<td><?php $no++; echo $no; ?></td>
<td><?php echo $i->NAMA; ?></td>
<?php if(!empty($i->DIAG)){ echo "<td>".$i->DIAG."</td>"; } ?>
</tr>
<?php endforeach ?>
<?php } ?>
</tbody>
</table>
Controller:
function kunjunganpx(){
$this->load->model('simrs/diagnosa_model');
$cari = array(
'icd' => $this->input->post('icd'),
'asr' => $this->input->post('asuransi'),
'tgl_strt' => $this->input->post('tgl_strt'),
'tgl_end' => $this->input->post('tgl_end'),
'stat' => $this->input->post('px_stat'),
'urut' => $this->input->post('urut'),
'opsi' => $this->input->post('opsi'));
$data['kunj'] = $this->diagnosa_model->get_kunjungan_list($cari);
$this->load->view('kunjunganpx_view',$data);
}
And this is notice I get:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: simrs/kunjunganpx_view.php
From googling some site, I realize that array of object can not checked with empty(). But I desperately want to know how to fix this, since I generate my query result in object.
All I wanna know is how to 'hide' the hardprint of ICD when there is no value in $kunj->DIAG. And show it if the variable has some values. Thanks in advance.
UPDATE
As request from a member I attach the var_dump($data); result here:
array(1) { ["kunj"]=> array(2)
{ [0]=> object(stdClass)#21 (20)
{ ["NAMA"]=> string(13) "SUPRIYANTO BP"
["DIAG"]=> string(5) "D48.0" }
[1]=> object(stdClass)#22 (20)
{ ["NAMA"]=> string(13) "SUPRIYANTO BP"
["DIAG"]=> string(5) "C44.9" }
}
}
OK ... Based on the fact that $kunj=array() this is what I came up with.
empty() is not going to work, because the object has the property or not. If it has the property, then you want to parse the extra column.
You will need to test if the property DIAG exists in the object.
if (property_exists($kunj[0], "DIAG"))
After that you can test if $kunj is empty, just like you are doing. In the foreach loop you will again have to test on the property before parsing the extra table cell. If that property is present, you will not have to test for empty.
I have also changed some of your logic. It all results to this:
<table class="table table-striped table-bordered table-condensed" id="tablescroll" role="grid">
<thead>
<tr class="tablesorter-headerRow" role="row">
<th>No.</th>
<th>Nama</th>
<?php
if (property_exists($kunj[0], "DIAG")) {
echo "<th>ICD</th>";
}
?>
</tr>
</thead>
<tbody aria-live="polite" aria-relevant="all">
<?php if(!empty($kunj)){ foreach ($kunj as $k => $i): ?>
<tr align='center'>
<td><?php echo ++$k; ?></td>
<td><?php echo $i -> NAMA; ?></td>
<?php
if (property_exists($kunj[0], "DIAG")) {
echo "<td>{$i->DIAG}</td>";
}
?>
</tr>
<?php endforeach; } ?>
</tbody>
</table>
codepad example: http://codepad.viper-7.com/3Jxi7k
if($kunj->DIAG != ''){ echo "<th>ICD</th>"; } ?>
I think this will fix it, since some times
as i have seen, or understod, u dont get null values, but empty in thos vars.
Could try this:
if (is_object($kunj) && !empty($kunj->DIAG)) {
// do your stuff
}
I am having this problem and is_null is the solution
try this :
if(!is_null($kunj->DIAG)){echo "<th>ICD</th>";}
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 :)