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 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>
I want to implement a logic for creating a three column table using foreach loop. A sample code will look like this.
$array = ['0','1','2','3','4','5','6'];
$table = '<table class="table"><tbody>';
foreach($array as $a=>$v){
//if 0th or 3rd???????????wht should be here?
$table .= '<tr>';
$table .= '<td>$v</td>';
//if 2nd or 5th??????????and here too???
$table .= '</tr>';
}
$table = '</tbody></table>';
Any ideas?
Expected output is a simple 3X3 table with the values from the array
Use this you may looking for this
<?php
echo('<table><tr>');
$i = 0;
foreach( $array as $product )
{
$i++;
echo '<td>'.$product .'</td>';
if($i % 3==0)
{
echo '</tr><tr>';
}
}
echo'</tr></table>';
?>
Result Here:
<table>
<tr>
<td>data1</td>
<td>data2</td>
<td>data3</td>
</tr>
<tr>
<td>data4</td>
<td>data5</td>
<td>data6</td>
</tr>
</table>
This should work for you:
(See that I added a tr at the start and end before and after the foreach loop. Also I changed the quotes to double quotes and made sure you append the text everywhere.)
<?php
$array = ['0','1','2','3','4','5','6'];
$table = "<table class='table'><tbody><tr>";
//^^^^ See here the start of the first row
foreach($array as $a => $v) {
$table .= "<td>$v</td>";
//^ ^ double quotes for the variables
if(($a+1) % 3 == 0)
$table .= "</tr><tr>";
}
$table .= "</tr></tbody></table>";
//^ ^^^^^ end the row
//| append the text and don't overwrite it at the end
echo $table;
?>
output:
<table class='table'>
<tbody>
<tr>
<td>0</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>6</td>
</tr>
</tbody>
</table>
Here is an easy solution with array_chunk():
<?php
$array = array('0','1','2','3','4','5','6');
$d = array_chunk($array, 3);
$table = "<table border='1' class='table'><tbody>";
foreach($d as $v)
$table .= "<tr><td>" . implode("</td><td>", $v) . "</td></tr>";
$table .= "</tbody></table>";
echo $table;
?>
Here is an easy solution with incrementing that works with a dynamically generated table like the one in Magento product view page to help display product attributes in two table columns preceded by attribute label -> practically we have 4 table columns together with attribute labels. This is useful for products with multiple attributes that take a long page to display.
<?php
$_helper = $this->helper('catalog/output');
$_product = $this->getProduct();
if($_additional = $this->getAdditionalData()): ?>
<h2><?php echo $this->__('Additional Information') ?></h2>
<table class="table table-bordered table-hover" id="product-attribute-specs-table">
<col width="25%" />
<col />
<tbody>
<?php $i = 1; ?>
<tr>
<?php foreach ($_additional as $_data): ?>
<?php $_attribute = $_product->getResource()->getAttribute($_data['code']);
if (!is_null($_product->getData($_attribute->getAttributeCode())) && ((string)$_attribute->getFrontend()->getValue($_product) != '')) { ?>
<th style="width: 20%;"><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
<td class="data" style="width:20%;"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
<?php $i++; if($i > 1 && ($i & 1) ) echo "</tr><tr>";?>
<?php } ?>
<?php endforeach; ?>
</tr>
</tbody>
</table>
<script type="text/javascript">decorateTable('product-attribute-specs-table')</script>
This is my code in controller...
function get_product(){
$purchase_id=$_POST['purchase_id'];
if($purchase_id!=''){
//$post_array['cart']='';
$res = $this->db->query("select * from phppos_productdetails WHERE
purchase_id='$purchase_id'");
?>
<tr>
<th>Product Name</th>
<th>Quantity </th>
<th>Unit </th>
<th>Unit Rate</th>
<th>Action</th>
</tr>
<?php
$i=0;
foreach($res->result() as $row )
{
$sess_products[$i]['product_id'] = $row->product_id;
$sess_products[$i]['quantity'] = $row->quantity;
$sess_products[$i]['unit'] = $row->unit;
$sess_products[$i]['unit_rate'] = $row->unit_rate;
$this->session->set_userdata('sess_products',$sess_products);
$query = $this->db->query("SELECT product_name FROM phppos_product WHERE
product_id='".$row->product_id."'");
foreach ($query->result() as $row1 )
{
$product_name=$row1->product_name;
}
echo "<tr>";
echo "<td>".$product_name."</td>";
echo "<td>".$row->quantity."</td>";
echo "<td>".$row->unit."</td>";
echo "<td>".$row->unit_rate."</td>";
echo "<td><a href='javascript:void(0)' rownum='".$i."' class='remove_from_cart'><img src='images/close.png'/></a></td>";
echo "</tr>";
$i++;
}
}
}
This is function in controller that is being called by ajax call. So how do I store that query result in session array in codeigniter??
I am acessing like this
$demo_arr['cart']= $this->session->userdata('sess_products');