I'm using the datatables plugin to format my tables with my data.
I make the call to my model from my controller to gather all the data.
I send the data array to the view file.
I start a table tag in my view with the datatable class which calls the plugin to turn the regular html table into a datatables table.
I check to verify that there is data in the array in the view and then if there is I do a foreach loop to create the table rows. If there is not data then I echo out a string of no data found.
My issue is when there is data it works fine however when there is not data I receive Requested Unknown Parameter 1 from the data source for row 0 error message.
I've seen others that have come across this same issue before however all the other posts I have seen have explained that they have used the datatables json data while specifically working with the datatables plugin.
Any ideas on how to prevent this error message from displaying.
<table class="dynamicTable table table-striped table-bordered table-condensed">
<thead>
<tr>
<th style="width: 1%;" class="uniformjs" id="checkboxth"><input type="checkbox" /></th>
<th class="center">ID</th>
<th>Name</th>
<th class="center">Date</th>
<th class="center" id="created_updated"></th>
<th class="right" id="actions">Actions</th>
</tr>
</thead>
<tbody>
<?php
//vardump($themes);
if (isset($themes) && is_array($themes))
{
foreach ($themes AS $theme)
{
echo '<tr class="selectable">';
echo '<td class="center uniformjs"><input type="checkbox" /></td>';
echo '<td class="center">' . $theme->id . '</td>';
echo '<td>' . $theme->name . '</td>';
if ($theme->updated_at != '0000-00-00 00:00:00')
{
echo '<td class="center" style="width: 80px;">' . date('d M Y', strtotime($theme->created_at)) . '</td>';
echo '<td class="center" style="width: 80px;"><span class="label label-block label-inverse">updated</span></td>';
}
else
{
echo '<td class="center" style="width: 80px;">' . date('d M Y', strtotime($theme->created_at)) . '</td>';
echo '<td class="center" style="width: 80px;"><span class="label label-block label-important">created</span></td>';
}
echo '<td class="center" style="width: 60px;">';
echo '<i></i>';
echo '<i></i>';
echo '</td>';
echo '</tr>';
}
}
else
{
echo '<tr>';
echo '<td class="center" colspan="7">There are no themes. Select Add a New Theme.</td>';
echo '</tr>';
}
?>
</tbody>
</table>
This is the code that came with the theme.
/* DataTables */
if ($('.dynamicTable').size() > 0)
{
$('.dynamicTable').dataTable({
"sPaginationType": "bootstrap",
"sDom": "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
"oLanguage": {
"sLengthMenu": "_MENU_ records per page"
}
});
}
Hi Please remove else condition from your code.Then your code will look like:
<table class="dynamicTable table table-striped table-bordered table-condensed">
<thead>
<tr>
<th style="width: 1%;" class="uniformjs" id="checkboxth"><input type="checkbox" /></th>
<th class="center">ID</th>
<th>Name</th>
<th class="center">Date</th>
<th class="center" id="created_updated"></th>
<th class="right" id="actions">Actions</th>
</tr>
</thead>
<tbody>
<?php
//vardump($themes);
if (isset($themes) && is_array($themes))
{
foreach ($themes AS $theme)
{
echo '<tr class="selectable">';
echo '<td class="center uniformjs"><input type="checkbox" /></td>';
echo '<td class="center">' . $theme->id . '</td>';
echo '<td>' . $theme->name . '</td>';
if ($theme->updated_at != '0000-00-00 00:00:00')
{
echo '<td class="center" style="width: 80px;">' . date('d M Y', strtotime($theme->created_at)) . '</td>';
echo '<td class="center" style="width: 80px;"><span class="label label-block label-inverse">updated</span></td>';
}
else
{
echo '<td class="center" style="width: 80px;">' . date('d M Y', strtotime($theme->created_at)) . '</td>';
echo '<td class="center" style="width: 80px;"><span class="label label-block label-important">created</span></td>';
}
echo '<td class="center" style="width: 60px;">';
echo '<i></i>';
echo '<i></i>';
echo '</td>';
echo '</tr>';
}
}
/* else
{
echo '<tr>';
echo '<td class="center" colspan="7">There are no themes. Select Add a New Theme.</td>';
echo '</tr>';
}*/
?>
</tbody>
</table>
Hope it will works.Because datatable automatically handle no data in table.
Related
I am trying to show results that I get from a SQL table, it is this:
what I want to do is show results 3 by 3, like this:
I mean a table for every 3 results that the "assigned_bank" field matches, and if there are 4 results with the same number in "assigned_bank", I also show it in that same table, that is; one table for each different "assigned_bank" id.
I've been trying most of the day and the closest thing I've come to is this:
This is my last code:
<?php
$tables = sizeof($search) / 3;
for ($i = 0; $i < $tables; $i++) {
?>
<table class="table customers">
<thead class="thead-blue">
<tr>
<th scope="col-xs-2">Name</th>
<th scope="col-xs-2">Lastname</th>
<th scope="col-xs-2">Bank ID</th>
</tr>
</thead>
<tbody>
<?php
foreach ($search as $item){
echo '<tr align="left">';
echo '<td class="col-xs-2">' . $item["p_name"] . '</td>' . "\r\n";
echo '<td class="col-xs-2">' . $item["p_lastname"] . '</td>' . "\r\n";
echo '<td class="col-xs-2">' . $item["assigned_bank"] . '</td>' . "\r\n";
echo '</tr>';
}
?>
</tbody>
</table>
<?php
echo "\r\n";
}
?>
Thank you very much for any possible help or comments and thank you for taking the time to respond.
<?php
$result = array();
foreach ($search as $key => $item) {
$result[$item['assigned_bank']][$key] = $item;
}
foreach($result as $key=>$search_items){
echo '<table class="table customers" border="2" >
<thead class="thead-blue">
<tr>
<th scope="col-xs-2">Name</th>
<th scope="col-xs-2">Lastname</th>
<th scope="col-xs-2">Bank ID</th>
</tr>
</thead>
<tbody>';
foreach($search_items as $skey=>$item){
echo '<tr align="left">';
echo '<td class="col-xs-2">' . $item["p_name"] . '</td>' . "\r\n";
echo '<td class="col-xs-2">' . $item["p_lastname"] . '</td>' . "\r\n";
echo '<td class="col-xs-2">' . $item["assigned_bank"] . '</td>' . "\r\n";
echo '</tr>';
}
echo '</tbody>
</table>';
}
<?>
You can use order by on assigned_bank column with ascending order:
SELECT p_name, p_lastname, assigned_bank FROM your_table order by
assigned_bank asc
"KOHANA FRAMEWORK" submit button how to get selected check box value and how to insert in database.
My page has two fields textbox and second checkbox.
I trying to insert checkbox and textbox value but textbox value is possible to get but checkbox value is not.
=====> add.php
<?php echo form::open('backend/notifications/add/', array('enctype' => 'multipart/form-data')) ?>
<div class="staffWrapper">
<h2 style="float: left;">List Companies</h2>
<div class="clear"></div>
<table style="width:100%; table-layout:fixed;" border="0">
<tr>
<th width="20"><input type='checkbox' id='selectall' style='width:15px;'/></th>
<th width="210">Name</th>
<th width="100">Contact Person</th>
<th width="100">Phone</th>
<th width="210">Email</th>
<!-- <th width="80" colspan="2">Actions</th> -->
</tr>
<?php
if (count($companies)) {
$i = (isset($_GET['page']) ? ($_GET['page']-1)*50+1 : 1);
foreach ($companies as $company) {
echo $i % 2 ? '<tr class="even">' : '<tr class="odd">';
// echo "<td align='center'>" . $i . "</td>";
echo "<td align='center'>";
echo '<input type="checkbox" id="PILIH" name="PILIH[]" class="PILIH" value='.$company['id'].' style="width:15px;"/>';
echo "</td>";
echo "<td>" . $company['name'] . "</td>";
echo "<td>" . $company['contact_person'] . "</td>";
echo "<td>" . $company['phone'] . "</td>";
echo "<td>" . $company['email'] . "</td>";
$i++;
}
} else if (empty ($companies) && $searchKey){
echo '<tr class="odd">';
echo '<td colspan="7" align="center"><h3>No mathcing results were found for this search term.</h3></td>';
echo '</tr>';
} else if (empty ($companies)){
echo '<tr class="odd">';
echo '<td colspan="7" align="center"><h3>There are no companies.</h3></td>';
echo '</tr>';
}
?>
</table>
<?php echo $pagination; ?>
</div>
<div class="clear"></div>
<div style="float:right; margin-right: 335px; padding:10px 0 10px 0;">
<?php echo form::submit('', 'Create Notifications', array('class' => 'submit')) ?>
</div>
<div class="clear"></div>
<?php echo form::close(); ?>
In the same way as in pure PHP.
$PILIH = !empty($_POST['PILIH'])?$_POST['PILIH']:Array(); // pure PHP
$PILIH = Array::get($_POST, 'PILIH', Array());
$PILIH = $this->request->post('PILIH', Array()); //Where $this is Controller
$PILIH = Request::current()->post('PILIH', Array());
As a result, you will get an array from company-ID or an empty array, so:
$company_id = NULL;
$q = DB::query(Databse::INSERT, 'INSERT INTO foo (company_id) VALUES (:company_id)')->bind(':company_id', $company_id);
foreach($PILIH AS $company_id) {
$q->execute();
}
You did not give too much input, so: happy code analysis. And: RTFM.
I am fetching data from Mysql database and populating them in a table.
However, i cannot seem to make the cell autofit to contents. I have tried width as the property of the table but i cant get it to work
Would really appreciate your help. Thanks
Here's what i have done so far
<div id="page-content-wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-lg-12">
<table class="table table-bordered" style="width:100%">
<thead>
<tr>
<th><center>ID</center></th>
<th>Name</th>
<th><center>Email</center></th>
<th>Number</th>
<th>Package</th>
<th>Flexibility</th>
<th >Date</th>
<th>Departuring From</th>
<th>Departure Date</th>
<th>Destination</th>
<th>Arrival Date</th>
<th>Price</th>
<th>Consolidator</th>
</tr>
</thead>
<tbody>
<?php
$query = 'SELECT * FROM queries';
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
echo '<tr>';
echo '<td>'. $row['id'] . '</td>';
echo '<td>'. $row['name'] . '</td>';
echo '<td>'. $row['email'] . '</td>';
echo '<td>'. $row['contactnumber'] . '</td>';
echo '<td>'. $row['packagedetails'] . '</td>';
echo '<td>'. $row['flexibility'] . '</td>';
echo '<td>'. $row['datetoday'] . '</td>';
echo '<td>'. $row['departure'] . '</td>';
echo '<td>'. $row['dateofdeparture'] . '</td>';
echo '<td>'. $row['destination'] . '</td>';
echo '<td>'. $row['dateofarrival'] . '</td>';
echo '<td>'. $row['price'] . '</td>';
echo '<td>'. $row['vendor'] . '</td>';
echo '<td width=250>';
echo '<a class="btn btn-success" href="readquery.php?id='.$row['id'].'">Read</a>';
echo ' ';
echo '<a class="btn btn-success" href="updatequery.php?id='.$row['id'].'">Update</a>';
echo ' ';
echo '<a class="btn btn-danger" href="deletequery.php?id='.$row['id'].'">Delete</a>';
echo '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
I never see you echo </tr> or </td>. It would be helpful to give us an output of the HTML being generated by your while loop.
The problem was not with my table. To make it work, i increased the width of my container in which my table was inside, and it worked
Is there a better solution, apart from a template engine like smarty, for huge code like this?
<table class="table table-striped dataTable table-bordered">
<thead>
<tr>
<?php
$output = '<th class="sorting_numeric_html sorting_default_desc">' . TH_ORDERS_ID . '</th>';
$output .= '<th class="sorting_date_eu">' . TH_ORDERS_DATE . '</th>';
$output .= '<th>' . TH_ORDERS_NAME . '</th>';
$output .= '<th>' . TH_ORDERS_STATUS . '</th>';
$output .= '<th class="sorting_disabled">' . TH_ACTION . '</th>';
echo $output;
?>
</tr>
</thead>
<tbody>
<?php
$output = '';
foreach ($orders['RESULT'] as $order) {
$output .= '<tr>';
$output .= '<td class="text-right">' . inc_buildLink(inc_url(FILENAME_ORDERS, 'oID=' . $order['orders_id'] . '&action=edit'), $order['orders_id']) . '</td>';
$output .= '<td>' . inc_datetime_short($order['date_purchased']) . '</td>';
$output .= '<td>' . inc_buildLink(inc_url(FILENAME_CUSTOMERS, 'cID=' . $order['customers_id']. '&action=edit'), $order['delivery_name']) . '</td>';
$output .= '<td class="status' . $order['orders_status'] . '">' . $order['orders_status_name'] . '</td>';
$output .= '<td class="btn-group actions">';
$output .= inc_buildLink(inc_url(FILENAME_ORDERS, 'oID=' . $order['orders_id'] . '&action=edit'),
inc_image(DIR_WS_ICONS . 'edit.png', ''), TOOLTIP_EDIT, 'class="tip btn btn-mini"');
$output .= modal_delete_orders($order['customers_name'], $order['orders_id'], 'class="tip btn btn-mini"');
$output .= '</td>';
$output .= '</tr>';
}
echo $output;
?>
</tbody>
</table>
For sure Smarty would be a solution, but is there another way?
I'd do it like this, makes it easier to read as the HTML is a little bit more separated from the PHP:
<table class="table table-striped dataTable table-bordered">
<thead>
<tr>
<th class="sorting_numeric_html sorting_default_desc"><?php echo TH_ORDERS_ID; ?></th>
<th class="sorting_date_eu"><?php echo TH_ORDERS_DATE; ?></th>
<th><?php echo TH_ORDERS_NAME; ?></th>
<th><?php echo TH_ORDERS_STATUS; ?></th>
<th class="sorting_disabled"><?php echo TH_ACTION; ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($orders['RESULT'] as $order): ?>
<tr>
<td class="text-right"><?php echo inc_buildLink(inc_url(FILENAME_ORDERS, 'oID=' . $order['orders_id'] . '&action=edit'), $order['orders_id']); ?></td>
<td><?php echo inc_datetime_short($order['date_purchased']); ?></td>
<td><?php echo inc_buildLink(inc_url(FILENAME_CUSTOMERS, 'cID=' . $order['customers_id'] . '&action=edit'), $order['delivery_name']); ?></td>
<td class="status<?php echo $order['orders_status']; ?>"><?php echo $order['orders_status_name']; ?></td>
<td class="btn-group actions">
<?php echo inc_buildLink(inc_url(FILENAME_ORDERS, 'oID=' . $order['orders_id'] . '&action=edit'),
inc_image(DIR_WS_ICONS . 'edit.png', ''), TOOLTIP_EDIT, 'class="tip btn btn-mini"'),
modal_delete_orders($order['customers_name'], $order['orders_id'], 'class="tip btn btn-mini"'); ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
Instead of <php echo you can also use <?=, but lots of people dislike the use of it. It's only 4 more characters to put echo so i'd stick with that!
Yes, you normally want to separate the most you can PHP and HTML, leaving the html alone, like this:
<table class="table table-striped dataTable table-bordered">
<thead>
<tr>
<th class="sorting_numeric_html sorting_default_desc">
<?php echo TH_ORDERS_ID; ?>
</th>
<th class="sorting_date_eu">
<?php echo TH_ORDERS_DATE; ?>
</th>
...
If your system supports it, you can try instead changing the <?php echo for <?=, which is known as short tags as read in the manual, in which case the code would look much neater:
<table class="table table-striped dataTable table-bordered">
<thead>
<tr>
<th class="sorting_numeric_html sorting_default_desc">
<?= TH_ORDERS_ID; ?>
</th>
<th class="sorting_date_eu">
<?= TH_ORDERS_DATE; ?>
</th>
...
However, a couple of notes that would require you to change your code further down. They might not be for now, but they more for your future code:
Why are you echoing constants? Normally you'd like to store your data in variables and then echo them.
It's better normally to avoid putting too many classes in the html since it's the CSS what gets cached better normally. I'd go with an id="tableorders" and then simply class="id", class="date" and so on.
I have this block of PHP and HTML:
<table width="57%" border="0" class="tabela_master" id="aps-cat">
<tr>
<?php
while ($row = $db->sql_fetchrow($result_1))
{
echo '<td width="33%" rowspan="12" align="center"><img src="../images/' . $row['picture'] . '" /></td>';
echo '<td width="20%" align="right" class="tabela_master" style="font-weight: bold">Emri / Mbiemri:</td>';
echo '<td width="1%"> </td>';
echo '<td width="46%" class="tabela_master">' . $row['firstname'] . " " . $row['lastname'] . '</td>';
echo '</tr>';
echo '<tr>';
echo '<td align="right" class="tabela_master" style="font-weight: bold">Gjinia:</td>';
echo '<td> </td>';
echo '<td class="tabela_master">' . $row['gender'] . '</td>';
echo '</tr>';
echo '<tr>';
echo '<td align="right" class="tabela_master" style="font-weight: bold">Datelindja:</td>';
echo '<td> </td>';
echo '<td class="tabela_master">' . $row['birthday'] . '</td>';
echo '</tr>';
echo '<tr>';
echo '<td align="right" class="tabela_master" style="font-weight: bold">Adresa / Lokacioni:</td>';
echo '<td> </td>';
echo '<td class="tabela_master">' . $row['location'] . '</td>';
echo '</tr>';
echo '<tr>';
echo '<td align="right" class="tabela_master" style="font-weight: bold">Telefoni:</td>';
echo '<td> </td>';
echo '<td class="tabela_master">' . $row['telephone'] . '</td>';
echo '</tr>';
echo '<tr>';
echo '<td align="right" class="tabela_master" style="font-weight: bold">Email adresa:</td>';
echo '<td> </td>';
echo '<td class="tabela_master">' . $row['email'] . '</td>';
echo '</tr>';
echo '<tr>';
echo '<td align="right" class="tabela_master" style="font-weight: bold">Interesi:</td>';
echo '<td> </td>';
echo '<td class="tabela_master">' . $row['occupation'] . '</td>';
echo '</tr>';
echo '<tr>';
echo '<td align="right" class="tabela_master" style="font-weight: bold"> </td>';
echo '<td> </td>';
echo '<td class="tabela_master"> </td>';
echo '</tr>';
echo '<tr>';
echo '<td align="right" class="tabela_master" style="font-weight: bold"> </td>';
echo '<td> </td>';
echo '<td class="tabela_master"> </td>';
echo '</tr>';
echo '<tr>';
echo '<td align="right" class="tabela_master" style="font-weight: bold"> </td>';
echo '<td> </td>';
echo '<td class="tabela_master"> </td>';
echo '</tr>';
echo '<tr>';
echo '<td align="right" class="tabela_master" style="font-weight: bold"> </td>';
echo '<td> </td>';
echo '<td class="tabela_master"> </td>';
echo '</tr>';
echo '<tr>';
echo '<td align="right" class="tabela_master" style="font-weight: bold"> </td>';
echo '<td> </td>';
echo '<td class="tabela_master"> </td>';
}
//$db->sql_freeresult($result_1);
?>
</tr>
</table>
Now what I want is multiple records shown in the page. The web currently looks like this:
And what I want would look like this:
So my table would provide all my results from my MySQL query which looks like this:
$sql_1 = 'SELECT id, firstname, lastname, birthday, location, occupation, gender, telephone, email, picture
FROM pinkmoon_users ORDER BY `id` DESC LIMIT 1';
$result_1 = $db->sql_query($sql_1) or die($db->sql_error());
Remove the LIMIT 1 in your SQL statement
$sql_1 = 'SELECT id, firstname, lastname, birthday, location, occupation, gender, telephone, email, picture
FROM pinkmoon_users ORDER BY `id` DESC ';
$result_1 = $db->sql_query($sql_1) or die($db->sql_error());
Your query retrieves only the last record from the database. If you want more, you should change the LIMIT 1 to (for example) LIMIT 10 or remove the limit contraint entirely.