Sorting HTML Table With TableSorter - php

I am attempting to add sortable columns to my html table and I thought I would give the jquery tablesorter a try. This is my syntax sans the actual DB call, and I think I have it set-up properly, however my table is not allowing me to sort. Why am I not able to sort?
<head>
<script type="text/javascript" src="/path/to/jquery-latest.js"></script>
<script type="text/javascript" src="/path/to/jquery.tablesorter.js"></script>
<script>
$(document).ready(function()
{
$("#SaleDistro").tablesorter();
}
);
</script>
</head>
<table id="SaleDistro" class="tablesorter" border="1">
<thead>
<tr>
<th>Sales Name </th>
<th>Sales Region </th>
<th>Sales Count </th>
<th>Sales Supervisor </th>
</tr>
</thead>
<?php
foreach ($query as $res)
{
print "<tbody>";
print "<tr>";
print "<td>" . $res->sn . "</td>";
print "<td>" . $res->sr . "</td>";
print "<td>" . $res->sc . "</td>";
print "<td>" . $res->ss . "</td>";
print "</tr>";
print "</tbody>";
}
?>
</table>
</html>
EDIT --->
I edited my syntax to read like this, but still have the issue
</thead>
<tbody>
<?php
foreach ($query as $res)
{
print "<tr>";
print "<td>" . $res->sn . "</td>";
print "<td>" . $res->sr . "</td>";
print "<td>" . $res->sc . "</td>";
print "<td>" . $res->ss . "</td>";
print "</tr>";
}
?>
</tbody>
</table>
</html>
EDIT 2
Below is update to show how $query get's it's value
<head>
<script type="text/javascript" src="/path/to/jquery-latest.js"></script>
<script type="text/javascript" src="/path/to/jquery.tablesorter.js"></script>
<script>
$(document).ready(function()
{
$("#SaleDistro").tablesorter();
}
);
</script>
</head>
<?php
$option = array();
$option['driver'] = 'mssql';
$option['host'] = 'IP Address';
$option['user'] = 'username';
$option['password'] = 'password';
$option['database'] = 'database';
$option['prefix'] = '';
$db = JDatabase::getInstance($option);
$query = $db->getQuery(true);
$query = "Select SalesName, SalesRegion, SalesCount, SalesSupervisor from salesdata;";
$db->setQuery($query);
$query = $db->loadObjectList();
if ($query)
{
?>
<table id="SaleDistro" class="tablesorter" border="1">
<thead>
<tr>
<th>Sales Name </th>
<th>Sales Region </th>
<th>Sales Count </th>
<th>Sales Supervisor </th>
</tr>
</thead>
<?php
foreach ($query as $res)
{
print "<tbody>";
print "<tr>";
print "<td>" . $res->sn . "</td>";
print "<td>" . $res->sr . "</td>";
print "<td>" . $res->sc . "</td>";
print "<td>" . $res->ss . "</td>";
print "</tr>";
print "</tbody>";
}
?>
</table>
</html>

You only want one <tbody> opening and closing tag each, so you need to move them out of the foreach loop.

Related

table class not working after inserting sql database inside

I firstly did the table layout and made sure everything is working, after connecting the table to a database and trying to put the records inside it, everything worked perfectly, but the class didnt, i have now the boring table without the layout made.
<body>
<h1>Employees</h1>
<table class="responstable">
<?php
require 'connection.php';
$conn = Connect();
$result = mysqli_query($conn,"SELECT * FROM employee");
echo "<table border='1'>
<tr>
<th>Id</th>
<th>First name</th>
<th>Last name</th>
<th>Salary</th>
<th>Start Date</th>
<th>Department</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['salary'] . "</td>";
echo "<td>" . $row['startdate'] . "</td>";
echo "<td>" . $row['department'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
<script src='http://cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.js'></script>
</body>
</html>
the table class is "responstable"
table tag is defined in 2 places.try removing this.
echo "<table border='1'>
or remove the table tag at the top after h1 tag and add the class to the table tag defined in the echo as,
complete code
<body>
<h1>Employees</h1>
<?php
require 'connection.php';
$conn = Connect();
$result = mysqli_query($conn,"SELECT * FROM employee");
echo "<table border='1' class='responstable'>
<tr>
<th>Id</th>
<th>First name</th>
<th>Last name</th>
<th>Salary</th>
<th>Start Date</th>
<th>Department</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['salary'] . "</td>";
echo "<td>" . $row['startdate'] . "</td>";
echo "<td>" . $row['department'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
<script src='http://cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.js'></script>
</body>
</html>

PHP To Show 2 Tables On One Page

I am using this syntax to display a PHP Table on my page. I now need to add in a second table directly above this one, but all the syntax I try throws a 500 error. How can I with 1 connection to MSSQL run 2 Select statements and populate 2 individual html tables?
$option = array();
$option['driver'] = 'mssql';
$option['host'] = 'IP Address';
$option['user'] = 'UserName';
$option['password'] = 'Password';
$option['database'] = 'DB';
$option['prefix'] = '';
$db = JDatabase::getInstance($option);
$query = $db->getQuery(true);
$query = "SELECT name, hiredate, bday, payrate, hourlypay from HRData ORDER BY name ASC";
$db->setQuery($query);
$query = $db->loadObjectList();
if ($query)
{
?>
<table border="1">
<thead>
<tr>
<th>Name </th>
<th>Hire Date </th>
<th>Birthday </th>
<th>Pay Rate </th>
<th>hourlypay </th>
</tr>
</thead>
<?php
foreach ($query as $res)
{
print "<tr>";
print "<td>" . $res->name . "</td>";
print "<td>" . $res->hiredate . "</td>";
print "<td>" . $res->bday . "</td>";
print "<td>" . $res->payrate . "</td>";
print "<td>" . $res->hourlypay . "</td>";
print "</tr>";
}
}
EDIT
This is the syntax I am trying to adapt, but I keep getting a 500 Error
$option = array();
$option['driver'] = 'mssql';
$option['host'] = 'IP Address';
$option['user'] = 'UserName';
$option['password'] = 'Password';
$option['database'] = 'DB';
$option['prefix'] = '';
$db = JDatabase::getInstance($option);
$query = $db->getQuery(true);
$query = "SELECT name, MAX(Pay) As PayYTD FROM HRINFO";
$db->setQuery($query);
$query = $db->loadObjectList();
if ($query)
{
?>
<table border="1">
<thead>
<tr>
<th>Name </th>
<th>YTD Pay </th>
</tr>
</thead>
<?php
foreach ($query as $res)
{
print "<tr>";
print "<td>" . $res->name . "</td>";
print "<td>" . "$" . round($res->PayYTD) . "</td>";
print "</tr>";
}
}
<br><br><br>
//Query
$query = $db->getQuery(true);
$query = "SELECT name, hiredate, bday, payrate, hourlypay from HRData ORDER BY name ASC";
$db->setQuery($query);
$query = $db->loadObjectList();
if ($query)
{
?>
<table border="1">
<thead>
<tr>
<th>Name </th>
<th>Hire Date </th>
<th>Birthday </th>
<th>Pay Rate </th>
<th>hourlypay </th>
</tr>
</thead>
<?php
foreach ($query as $res)
{
print "<tr>";
print "<td>" . $res->name . "</td>";
print "<td>" . $res->hiredate . "</td>";
print "<td>" . $res->bday . "</td>";
print "<td>" . $res->payrate . "</td>";
print "<td>" . $res->hourlypay . "</td>";
print "</tr>";
}
}
The problem you're having is that you are using the calls incorrectly.
$query = $db->getQuery(true);
$query = "SELECT name, MAX(Pay) As PayYTD FROM HRINFO";
$db->setQuery($query);
The first line will create an object, it doesn't matter which. The object will be in $query.
The second line will immediately destroy the object and assign a string to $query (this is incorrect).
The third line expects an object as a parameter to setQuery, but unfortunately it is a string! Error.
If you want this to work correctly, then you need to use the object in $query correctly.
I'm not a Joomla expert, so I link you to a page for how to do this correctly: https://docs.joomla.org/Selecting_data_using_JDatabase
Put the output in a variable.
$StringOut = '';
$StringOut .= '<table border="1">
<thead>
<tr>
<th>Name </th>
<th>Hire Date </th>
<th>Birthday </th>
<th>Pay Rate </th>
<th>hourlypay </th>
</tr>
</thead>';
foreach ($query as $res)
{
$StringOut .= "<tr>";
$StringOut .= "<td>" . $res->name . "</td>";
$StringOut .= "<td>" . $res->hiredate . "</td>";
$StringOut .= "<td>" . $res->bday . "</td>";
$StringOut .= "<td>" . $res->payrate . "</td>";
$StringOut .= "<td>" . $res->hourlypay . "</td>";
$StringOut .= "</tr>";
}
$StringOut .= '</table>';
#Do other logic to retrieve first table.
#You could put it in a different variable if you like. And print when and whereever you wish.
echo $StringOut;
#Optionally close connection, done.

Having issue showing image returning from database with $.post() jquery and php

I'm retrieving a table from database as result of a search action, but when I try to display the result I see the table and the data, but the image returned in each row is not render, I think my problem is in the $('#search').html(data), I'm not sure please someone knows what is the problem?
this is the result
http://s9.postimg.org/mro5qn46n/search_result.jpg
****This is the search page, where result table is displayed****
<table align="center">
<tr>
<td>
<label for="criteria">Select Criteria</label>
</td>
<td>
<select name="select" id="criteria">
<option selected="true" style="display:none;"></option>
<option value="value1">name</option>
<option value="value2">apartment</option>
</select>
</td>
<td>
<input type="text" name="value" size="40" maxlength="60" id="value"\>
</td>
</tr>
<tr>
<td>
<input name="name-submit" type="button" id="submit" value="Search"\>
</td>
</tr>
<tr>
<td >
<div id="search"></div>
</td>
</tr>
</table>
<script type="text/javascript" src="../js/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$('#submit').click(function(){
var criteria = $("#criteria option:selected").text();
var value = $("#value").val();
$.post("search_r.php",{criteria:criteria,value:value},function(data){
$('#search').html(data);
});
});
</script>
****This is the Page that calls $.post() in the main seach page ****
<?php
$criteria = $_POST['criteria'];
$value = $_POST['value'];
$con = mysqli_connect("localhost","root","");
mysqli_select_db($con,"gables");
$query = "SELECT * FROM residents WHERE $criteria = '$value'";
$result = mysqli_query($con,$query);
echo "<table border='1'>
<tr>
<th>Id</th>
<th>Name</th>
<th>Last Name</th>
<th>Apartment</th>
<th>Parking</th>
<th>Phone1</th>
<th>Phone2</th>
<th>image</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['0'] . "</td>";
echo "<td>" . $row['1'] . "</td>";
echo "<td>" . $row['2'] . "</td>";
echo "<td>" . $row['3'] . "</td>";
echo "<td>" . $row['4'] . "</td>";
echo "<td>" . $row['5'] . "</td>";
echo "<td>" . $row['6'] . "</td>";
echo "<td><img src=get_image.php?id=".$row['0']." width=160 height=120/></td>";
echo "</tr>";
}
echo "</table>";
?>
***Here the get_image.php****
<?php
$con = mysqli_connect("localhost","root","");
mysqli_select_db($con,"gables");
$id = $_GET['id'];
$query = "SELECT * FROM residents WHERE id='$id'";
$result = mysqli_query($con,$query);
if($result)
$picture = mysqli_fetch_array($result);
header('Content-Type: image/jpg');
echo $picture['11'];
?>
You can chage the get_image.php file as this. Then this work will work.
<?php
function get_image($id){
$con = mysqli_connect("localhost","root","");
mysqli_select_db($con,"gables");
$query = "SELECT * FROM residents WHERE id='$id'";
$result = mysqli_query($con,$query);
if($result)
$picture = mysqli_fetch_array($result);
return $picture['11'];
}
?>
Then use require_once(); function and in image src like this.echo "<td><img src='".get_image($row['0'])."' width=160 height=120/></td>";. In your code check how the src path will print and it will be print like as echo string, not as a execute the file.
echo
"<table border='1'>
<tr>
<th>Id</th>
<th>Name</th>
<th>Last Name</th>
<th>Apartment</th>
<th>Parking</th>
<th>Phone1</th>
<th>Phone2</th>
<th>image</th>
</tr>";
require_once('search_r.php');
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['0'] . "</td>";
echo "<td>" . $row['1'] . "</td>";
echo "<td>" . $row['2'] . "</td>";
echo "<td>" . $row['3'] . "</td>";
echo "<td>" . $row['4'] . "</td>";
echo "<td>" . $row['5'] . "</td>";
echo "<td>" . $row['6'] . "</td>";
echo "<td><img src='".get_image($row['0'])."' width=160 height=120/></td>";
echo "</tr>";
}
echo "</table>";

SQL drop-down select display into PHP table with headers only on top

I am pulling stuff from a DB and populating it into a drop-down select. When the user selects the query, I want it to be displayed in a table (which it is now). But the problem is with the format. I want the headers to be displayed just on the top of the table rather than for every single row. What is wrong with my code? Any ideas/suggestions? Thanks in advance.
Resulting page of code:
Desired format (table headings just on top):
<?php
session_start();
if(!isset($_SESSION['EmployeeID'])){
$URL="Logon.php";
header ("Location: $URL");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Orders Page</title>
</head>
<body>
<h1>Orders Information Page</h1>
<form action="Orders.php" method="post">
<?php
require "Information.php";
try{
$database="Northwind";
$conn = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
} catch(PDOException $e){
$conn->setArribute(PDO::ATT_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Could not open database.";
}
$lname= $_POST['txtFirstName'];
$extension = $_POST['txtLastName'];
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$_SESSION['OrderID']=$_POST['selOrder'];
} //end if is a post
echo "<select name='selOrder'>";
$sql = $conn->prepare("SELECT * FROM orders WHERE EmployeeID=? ORDER BY CustomerID ASC");
if ($sql->execute(array($_SESSION['EmployeeID']))) {
while ($row = $sql->fetch()) {
echo "\n\t<option value='" . $row['OrderID'] . "'";
if(isset($_SESSION['OrderID']) && $_SESSION['OrderID']== $row['OrderID']){
echo " selected='selected' ";
}
echo ">";
echo "Cust:" . $row['CustomerID'] . " ordered on " . $row['OrderDate'];
echo "</option>";
}
}
echo "</select>";
?>
<input name="btnSubmit" type="submit" value="Submit" />
<?php
if(isset($_SESSION['OrderID'])){
echo "<h3>Order Details</h3><p>";
$sql = $conn->prepare("SELECT * FROM orders, orderdetails, products WHERE orders.OrderID=orderdetails.OrderID AND products.ProductID=orderdetails.ProductID AND orders.OrderID=? ");
if ($sql->execute(array($_SESSION['OrderID']))) {
while ($row = $sql->fetch()) {
echo "<table border = '1'>
<tr>
<th>Order ID</th>
<th>Order Date</th>
<th>Product Name</th>
</tr>";
{
echo "<tr>";
echo "<td>" . $row['OrderID'] . "</td>";
echo "<td>" . $row['OrderDate'] . "</td>";
echo "<td>" . $row['ProductName'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
}
echo "</p>";
}
$conn =null;
?>
</form>
</body>
</html>
Maybe I understand the problem very poorly, or do not understand at all, but is
...
echo "<table border = '1'>
<tr>
<th>Order ID</th>
<th>Order Date</th>
<th>Product Name</th>
</tr>";
while ($row = $sql->fetch()) {
echo "<tr>";
echo "<td>" . $row['OrderID'] . "</td>";
echo "<td>" . $row['OrderDate'] . "</td>";
echo "<td>" . $row['ProductName'] . "</td>";
echo "</tr>";
}
echo "</table>";
...
not what you are looking for? Eg displaying the <th>'s only once?
This is not very difficult, you just made a little mistake.
Get the
<tr>
<th>Order ID</th>
<th>Order Date</th>
<th>Product Name</th>
</tr>";
part out of the while loop, just before the while loop, and it will work like how you want it to.
Hope this helps.
move
"<table border = '1'><tr>
<th>Order ID</th>
<th>Order Date</th>
<th>Product Name</th>
</tr>";
(and while you're at it the echo "</table>";)
out of the while loop:
echo "<table border = '1'><tr>
<th>Order ID</th>
<th>Order Date</th>
<th>Product Name</th>
</tr>";
while ($row = $sql->fetch()) {
echo "<tr>";
echo "<td>" . $row['OrderID'] . "</td>";
echo "<td>" . $row['OrderDate'] . "</td>";
echo "<td>" . $row['ProductName'] . "</td>";
echo "</tr>";
}
echo "</table>";
note that you had two extra curly braces that weren't doing anything.

PHP page with embedded HTML displaying extra column

I've a php page with embedded HTML and I'm displaying data from a MySQL database. PHP is echoing the html inside the php page. All of the data is being returned; however, the data table is being displayed with an extra column and the data that should be in the last column is displayed in the extra column (e.g. my last name is 'Last Name,' but there is an extra column after 'Last Name' with the 'last name' data).
What am I doing wrong here?
Thanks.
get_records.php
//make connection
$conn = mysql_connect('localhost', 'root', '');
//select db
mysql_select_db('kis');
if (!$conn) {
die("Can not connect: " . mysql_error());
}
//select db and run query
mysql_select_db('kis');
$sql = "SELECT * FROM users";
$records = mysql_query($sql);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="css/TableCSSCode.css" media="all"/>
<title>Volunteer Data</title>
</head>
<body>
<div class="CSSTableGenerator">
<h1>Volunteer Records</h1>
<table>
<tr>
<th>First Name</th>
<th>Middle Name</th>
<th>Last Name</th>
</tr>
<tr>
<?php
//loop through the records and display in page
while ($users = mysql_fetch_assoc($records)) {
echo "<tr>";
echo "<td>" . $users['firstname'] . "</td>";
echo "<td>" . $users['middlename'] . "<td>";
echo "<td>" . $users['lastname'] . "<td>";
echo "</tr>";
}//end while
?>
</tr>
</table>
</div>
<!--end #dr_container-->
</body>
</html>
You need to close the td's
echo "<td>" . $users['middlename'] . "</td>";
echo "<td>" . $users['lastname'] . "</td>";
You have not properly closed your td tags:
echo "<td>" . $users['middlename'] . "<td>";
echo "<td>" . $users['lastname'] . "<td>";
It should be </td> at the end.
You're echoing the row tags (<tr>). So, don't add additional ones in the plain html (just around your PHP code).
Close "td" tag.
Remove "tr" tags before and after where php code start because there are already tr tags inside the php code.
Following is the updated HTML of table:
<table>
<tr>
<th>First Name</th>
<th>Middle Name</th>
<th>Last Name</th>
</tr>
<?php
//loop through the records and display in page
while ($users = mysql_fetch_assoc($records)) {
echo "<tr>";
echo "<td>" . $users['firstname'] . "</td>";
echo "<td>" . $users['middlename'] . "</td>";
echo "<td>" . $users['lastname'] . "</td>";
echo "</tr>";
}//end while
?>

Categories