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.
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
I used to pass value from one page to another using session. E.g
page1.php
<?php
session_start();
$_SESSION["id"] = $id;
?>
page2.php
<?php
session_start();
echo $_SESSION["id"];
?>
But for the following case i don't know how can i pass the id to the next page. This code is used to display group of people and the action column have an option to view the people details.
<table>
<thead>
<tr>
<th>Name</th>
<th>Status</th>
<th>Created</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$DBSystemAccess = dbSelectByWhere("SystemAccess", "", "ORDER By Timestamp");
while ($SystemAccessDB = dbFetchArray($DBSystemAccess)) {
?>
<tr>
<?php
echo '<td>' . dbGroupNamebyId($_SESSION['PI_ID']) . '</td>';
echo '<td>' . $Status . '</td>';
echo '<td>' . $SystemAccessDB['timeStamp'] . '</td>';
echo '<td>' . '<a href="adetails.php?ID=' . $SystemAccessDB['id'] . '" >View</a> </td>'
?>
</tr>
<?php } ?>
</tbody>
How can i replace <a href="adetails.php?ID=' . $SystemAccessDB['id'] . '" >View</a> to not showing ID in the URL?
Please try this with the hyper link it is not possible to hide the GET data.We need to use POST method.
<table>
<thead>
<tr>
<th>Name</th>
<th>Status</th>
<th>Created</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$DBSystemAccess = dbSelectByWhere("SystemAccess", "", "ORDER By Timestamp");
while ($SystemAccessDB = dbFetchArray($DBSystemAccess)) {
?>
<tr>
<?php
echo '<td>' . dbGroupNamebyId($_SESSION['PI_ID']) . '</td>';
echo '<td>' . $Status . '</td>';
echo '<td>' . $SystemAccessDB['timeStamp'] . '</td>';
echo '<form name="test" method="post" action="testing.php">';
echo '<input type="hidden" name="hidden_name" value="'.$SystemAccessDB['id'].'"/>';
echo '<button type="submit">View</button>';
echo '</form>';
?>
</tr>
<?php } ?>
</tbody>
When you will click it will redirect to testing.php with hidden value 'hidden_name' on which you can further query according to that value.
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
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.
I'm using php to create a table from the data extracted from MySQL database. I'm using SELECT * From table_name and all data is appearing as I wish. But I want to create a Drop Down menu in the last column for that I'm using the function displayDMenu. But the drop down menu is still not appearing in the table. Can you please suggest what is causing the problem and solution? Or alternate solutions? Here's the code:
<div id="main">
<?php
include("config.php");
$result = mysql_query("SELECT * FROM schedule");
echo "<table border='1'>
<tr>
<th>Appoint No.</th>
<th>CR number</th>
<th>Time</th>
<th>Date</th>
<th>Month</th>
<th>Year</th>
<th>Department</th>
<th>Status</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td style='padding:3px'>" . $row['S_ID'] . "</td>";
echo "<td style='padding:3px'>" . $row['crnumber'] . "</td>";
echo "<td style='padding:3px'>" . $row['ScdTime'] . "</td>";
echo "<td style='padding:3px'>" . $row['ScdDate'] . "</td>";
echo "<td style='padding:3px'>" . $row['ScdMonth'] . "</td>";
echo "<td style='padding:3px'>" . $row['ScdYear'] . "</td>";
echo "<td style='padding:3px'>" . $row['DName'] . "</td>";
echo "<td 'style='padding:3px'>" . displayDMenu() . "</td>";
echo "</tr>";
}
echo "</table>";
function displayDMenu() {
$r = '';
$r .='<form method="post" action="ScdApproval.php">';
$r .='<select name="Status">';
$r .='<option value="approved" selected>Approve</option>';
$r .='<option value="disapproved">Disapprove</option>';
$r .='</select>';
$r .='</form>';
}
?>
</div> <!--main ends here -->
Not sure why you need the user function its not doing anything logical its just spitting back html which there is no reason not to put that html in the while loop.
<?php
include("config.php");
$result = mysql_query("SELECT * FROM schedule");
?>
<style>#main table tr td{padding:3px;}</style>
<div id="main">
<table border="1">
<tr>
<th>Appoint No.</th>
<th>CR number</th>
<th>Time</th>
<th>Date</th>
<th>Month</th>
<th>Year</th>
<th>Department</th>
<th>Status</th>
</tr>
<?php
if(mysql_num_rows($result) > 0):
while($row = mysql_fetch_array($result)): ?>
<tr>
<td><?php echo $row['S_ID']?></td>
<td><?php echo $row['crnumber']?></td>
<td><?php echo $row['ScdTime']?></td>
<td><?php echo $row['ScdDate']?></td>
<td><?php echo $row['ScdMonth']?></td>
<td><?php echo $row['ScdYear']?></td>
<td><?php echo $row['DName']?></td>
<td><form method="post" action="ScdApproval.php">
<select name="Status">
<option value="approved" selected>Approve</option>
<option value="disapproved">Disapprove</option>
</select>
</form>
</td>
</tr>
<?php
endwhile;
else: ?>
<tr>
<td rowspan="8">No results</td>
</tr>
<?php endif;?>
</table>
</div> <!--main ends here -->
Also:
Really you should also switch to using PDO or mysqli prepared querys for new code.
You need to return the value $r in the function displayDMenu()
ie
function displayDMenu()
{
.........
return $r;
}