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

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>

Related

Selected Data only post within the same page PHP CodeIgniter

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.

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

Check <th> value and update in <td> based on condition

I have a result and a json , where $response['json'] contain table header columns and table data comes for the $response['results']. i need to check if the header contain column name as "NN ID" then respective table column data should be in a anchor Data tag.
PHP code:
$json = $response['JSON'];
$result = $response['RESULT'];
echo '<strong>Search Results</strong>
<h4 class="bg-default"><strong>Total Records ('.count($result).')</strong></h4>';
if (count($result) > 0) {
echo '<table class="table table-striped table-hover table-bordered">
<thead class="bg-primary">
<tr>';
foreach (array_values($json) as $column) {
echo '<th>'.$column.'</th>';
}
echo '</tr>
</thead>
<tbody>';
foreach ($result as $row) {
echo '<tr>';
foreach (array_keys($json) as $field) {
echo '<td>'.$row[$field].'</td>';
}
echo '</tr>';
}
echo '</tbody>
</table>';
}
Need to add href for
<td>
<a onclick="parent.LoadIframe(\'/view.php?nnid='.$row[field].'\')">
'.$row[field].'
</a>
</td>
Current View:
Need View as a link for NN ID Column data
A simple if statement is all you need:
foreach ($result as $row) {
echo '<tr>';
foreach ( array_keys($json) as $field) {
if ( $field == 'NN ID' ) {
echo '<td><a onclick="parent.LoadIframe(\'/view.php?nnid='.$row[$field].'\')">
'.$row[$field].'
</a></td>';
} else {
echo '<td>'.$row[$field].'</td>';
}
}
echo '</tr>';
}

Formatting php into html table correctly

I have a php array that contains information from my database $name[] = $row['name']. There are also about 3 other rows that container email, age, and screen-resolution.
I am trying to neatly assemle this into a table that looks like:
name----------email----------age----------screen-res
name1---------email1--------age1---------res1
name2---------email2--------age2---------res2
However mine currently looks like this:
name----------email----------age----------screen-res
name1---------name2----------name3------name4---------name5------name6-------name7------name8
email1---------email2----------email3------email---------email5------email6-------email7
My Code
<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Age</th>
<th>Screen-Res</th>
</tr>
<?php
echo "<tr>";
foreach ($name as $nameVal) {
echo "<td>$nameVal</td>";
}
echo "</tr>";
echo "<tr>";
foreach ($email as $emailVal) {
echo "<td>$emailVal</td>";
}
echo "</tr>";
echo "<tr>";
foreach ($age as $ageVal) {
echo "<td>$ageVal</td>";
}
echo "</tr>";
echo "<tr>";
foreach ($screen-res as $screen-resVal) {
echo "<td>$screen-resVal</td>";
}
echo "</tr>";
?>
</table>
You are forming your tables incorrectly. You your table to look something like this. The good thing is you only need one array for all your data
<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Age</th>
<th>Screen-Res</th>
</tr>
<?php
foreach ($rows as $row) {
echo "<tr>";
echo "<td>".$row['nameVal']."</td>";
echo "<td>".$row['emailVal']."</td>";
echo "<td>".$row['ageVal']."</td>";
echo "<td>".$row['screen-resVal']."</td>";
echo "</tr>";
}
?>
</table>

mysql query to display results in repeating columns

I am trying to build a comparison table using mysql query and php.
I would like the result to be displayed in a columns, like this:
<table border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="151" scope="col">product</td>
<td width="89" scope="col">product1</td>
<td width="78" scope="col">product2</td>
<td width="77" scope="col">product3</td>
</tr>
<tr>
<td>type</td>
<td>type2</td>
<td>type3</td>
<td>type5</td>
</tr>
<tr>
<td>size</td>
<td>size2</td>
<td>size1</td>
<td>size4</td>
</tr>
<tr>
<td>price</td>
<td>4.99</td>
<td>3.99</td>
<td>3.59</td>
</tr>
</table>
but I can only get the table to show the results - not a row title too (i.e. I want the first column to show 'product', 'type', 'size', 'price'.
The code I have so far is
<?php
// query the database
$result = mysql_query($query_getproducts);
// cols we are interested in (from the SQL query)
$cols = array(
'product',
'type',
'size',
'price',
);
// initialize rotated result using cols
$rotated = array();
foreach($cols as $col) {
$rotated[$col] = array();
}
// fill rotated array
while(($row = mysql_fetch_assoc($result)) !== false) {
foreach($cols as $col) {
$rotated[$col][] = $row[$col];
}
}
// echo html
echo "<table border=1 width=473>";
echo "<tr>";
echo "</tr>";
foreach($rotated as $col => $values) {
echo "<tr>";
foreach($values as $value) {
echo "<td> " . htmlentities($value) . "</td>";
}
echo "</tr>";
}
echo "</table>";
?>
hope someone can help.
First of all mysql_* functions are deprecated. You should use PDO or Mysqli.
If you want table headers static ie you want to show table header as "Product,Type,Size, Price"
Then use
<tr>
<th>Product</th>
<th>Type</th>
<th>Size</th>
<th>Price</th>
</tr>
Then if your should use mysql_fetch_assoc which returns associative array with column name as there key. You can use that array and print the result using loop.
eg:
<?php
$rs=mysql_query($query);
while($row=mysql_fetch_assoc($rs) ){
?>
<tr>
<td><?php echo $row['keyname']?></td>
.....
.....
</tr>
<?php
}
?>
try like this ,
echo "<table border=1 width=473>";
echo " <tr>
<th>Product Name</th>
<th>Description</th>
<th>Product Size</th>
<th>Price</th>
</tr>";
foreach($rotated as $col => $values) {
echo "<tr>";
foreach($values as $value) {
echo "<td> " . htmlentities($value) . "</td>";
}
echo "</tr>";
}
echo "</table>";

Categories