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.
Related
I have this line in my syntax if (query) which I thought would check if the query was null, however my query returns 0 rows and the header row is still displayed.
What is the proper way to ensure the query returns rows before you display the table?
<?php
$option = array();
$option['driver'] = 'mssql';
$option['host'] = 'ServerName';
$option['user'] = 'username';
$option['password'] = 'password';
$option['database'] = 'database';
$option['prefix'] = '';
$db = JDatabase::getInstance($option);
$query = $db->getQuery(true);
$query = "SELECT blue, green, red, purple, pink from colors";
$db->setQuery($query);
$query = $db->loadObjectList();
if (query)
{
?>
<div id="dvData">
<table id="example" border="1">
<caption><strong style="font-size:25px;"><?php echo $startdate ?> - <?php echo $enddate ?></strong></caption>
<thead>
<tr>
<th bgcolor="#00FF00">Blue </th>
<th bgcolor="#00FF00">Green </th>
<th bgcolor="#00FF00">Red </th>
<th bgcolor="#00FF00">Purple </th>
<th bgcolor="#00FF00">Pink </th>
</tr>
</thead>
<?php
foreach ($query as $res)
{
print "<tr>";
print "<td>" . $res->Blue . "</td>";
print "<td>" . $res->Green . "</td>";
print "<td>" . $res->Red . "</td>";
print "<td>" . $res->Purple . "</td>";
print "<td>" . $res->Pink . "</td>";
print "</tr>";
}
}
?>
</table>
</div>
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.
I have a MySQL query that I know to be working in Workbench/Sequel Pro that appears to return empty in my PHP code:
$SQL = "SELECT * FROM staff, invigilation, exam, occupation, room, module
WHERE staffID='$inputID'
AND invigilation.examID=exam.exam_ID
AND exam.exam_ID=occupation.examID
AND room.room_ID=occupation.roomID
AND exam.module_ID=module.module_ID
AND staff.password='$inputPassword'";
$result = mysql_query($SQL);
if($db_field = mysql_fetch_assoc($result)) {
echo "
<table border='1'>
<tr>
<th>Exam ID</th>
<th>Module ID</th>
<th>Module name</th>
<th>Duration</th>
<th>Start time</th>
<th>Room</th>
</tr>";
while ($db_field = mysql_fetch_assoc($result)) {
echo "
<tr>
";
echo "<td>" . $db_field['exam_ID'] . "</td>";
echo "<td>" . $db_field['module_ID'] . "</td>";
echo "<td>" . $db_field['module_name'] . "</td>";
echo "<td>" . $db_field['duration'] . "</td>";
echo "<td>" . $db_field['start_datetime'] . "</td>";
echo "<td>" . $db_field['room_name'] . "</td>";
echo "
</tr>";
}
} else {
echo "Incorrect login details. Please try again";
die();
}
The result of this code is the first table row (Exam ID, Module ID etc.) is displayed but the table content is not.
I cannot work out why this is happening. Other than the SQL query, the code is identical to code used elsewhere (where it is working fine).
You are doing mysql_fetch_assoc twice. The first time it reads the record and prints the table header. The second time there are no more records so it doesn't print a row.
Change
if($db_field = mysql_fetch_assoc($result)) {
to
if(mysql_num_rows($result) > 0) {
Try this. You are using mysql_fetch_assoc($result); two times, First time it get result and second time there is no result.
$SQL = "SELECT * FROM staff, invigilation, exam, occupation, room, module
WHERE staffID='$inputID'
AND invigilation.examID=exam.exam_ID
AND exam.exam_ID=occupation.examID
AND room.room_ID=occupation.roomID
AND exam.module_ID=module.module_ID
AND staff.password='$inputPassword'";
$result = mysql_query($SQL);
$db_field = mysql_fetch_assoc($result)
if($db_field != "" ) {
echo "
<table border='1'>
<tr>
<th>Exam ID</th>
<th>Module ID</th>
<th>Module name</th>
<th>Duration</th>
<th>Start time</th>
<th>Room</th>
</tr>";
while ($db_field) {
echo "
<tr>
";
echo "<td>" . $db_field['exam_ID'] . "</td>";
echo "<td>" . $db_field['module_ID'] . "</td>";
echo "<td>" . $db_field['module_name'] . "</td>";
echo "<td>" . $db_field['duration'] . "</td>";
echo "<td>" . $db_field['start_datetime'] . "</td>";
echo "<td>" . $db_field['room_name'] . "</td>";
echo "
</tr>";
}
} else {
echo "Incorrect login details. Please try again";
die();
}
I am making a project on proposal management system and I am stuck on the pricing module. Here is my HTML DEMO. I have shown only required field in the output. INSERT queries below are correct.
I am trying to AUTOMATICALLY display the sum of the 'amount' in the 'total' each time the user presses 'save' button . The value of 'total' should increment when user presses 'save' either from 'add services' table or from 'add products' table. I am unsuccessful so far. HOW CAN I DO IT?
I have tried this: (Any other solution will be appreciated).
<td> <input type="text" name="amount" maxlength="15" id="amount" readonly="true" value=" <?php
if(isset($_POST['save'])|| isset($_POST['psave']))
{
$con= mysqli_connect("localhost","root","","my_db")or die("cannot connect");
$sql = "SELECT proposal_services.service_amount AMOUNT,proposal_products.product_amount AMOUNT1, FROM proposal_services INNER JOIN proposal_products ON proposal_products.proposal_id=proposal_services.proposal_id";
$result= mysqli_query($con,$sql);
$row = mysqli_fetch_array($result);
foreach ( $row as $records )
{
$sum = str_replace(",", "", $records['AMOUNT']) + str_replace(",", "", $records['AMOUNT1']);
}
echo number_format( $sum, 2 );
}
?> "/></td>
my php code where I insert and display results is:
insert_services.php
<?php
session_start();
$prop_id = $_SESSION['propl_id'];
$user_id = $_SESSION['sign_user_id'];
$con = mysqli_connect("localhost","root","","my_db");
$name = $_POST['ser_name'];
$desc = $_POST['txt'];
$price = $_POST['price'];
$per = $_POST['per'];
$quantity = $_POST['quantity'];
$amount = $_POST['amount'];
if(mysqli_query($con,"INSERT INTO proposal_services(proposal_id,user_id,service_name,service_description,service_price,per,service_quantity,service_amount) VALUES('$prop_id','$user_id','$name','$desc','$price','$per','$quantity','$amount')"))
$sql= "select * FROM proposal_services WHERE proposal_id='$prop_id' ";
$result= mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th align='center'>Service Name</th>
<th align='center'>Description</th>
<th align='center'>Price</th>
<th align='center'>per</th>
<th align='center'>Quantity</th>
<th align='center'>Amount</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<div id='change'>";
echo "<tr>";
echo "<td align='center'>" . $row['service_name'] . "</td>";
echo "<td align='center'>" . $row['service_description']. "</td>";
echo "<td align='center'>" . $row['service_price']. "</td>";
echo "<td align='center'>" . $row['per']. "</td>";
echo "<td align='center'>" . $row['service_quantity']. "</td>";
echo "<td align='center'>" . $row['service_amount']. "</td>";
echo "</tr>";
echo "</div>";
}
echo "</table>";
?>
insert_products.php
<?php
session_start();
$prop_id = $_SESSION['propl_id'];
$user_id = $_SESSION['sign_user_id'];
$con = mysqli_connect("localhost","root","","my_db");
$name = $_POST['pro_name'];
$desc = $_POST['txt'];
$price = $_POST['price'];
$per = $_POST['per'];
$quantity = $_POST['quantity'];
$amount = $_POST['amount'];
if(mysqli_query($con,"INSERT INTO proposal_products(proposal_id,user_id,product_name,product_description,product_price,per,product_quantity,product_amount) VALUES('$prop_id','$user_id','$name','$desc','$price','$per','$quantity','$amount')"))
$sql= "select * FROM proposal_products WHERE proposal_id='$prop_id'";
$result= mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Product Name</th>
<th>Description</th>
<th>Price</th>
<th>per</th>
<th>Quantity</th>
<th>Amount</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['product_name'] . "</td>";
echo "<td>" . $row['product_description'] . "</td>";
echo "<td>" . $row['product_price'] . "</td>";
echo "<td>" . $row['per'] . "</td>";
echo "<td>" . $row['product_quantity'] . "</td>";
echo "<td>" . $row['product_amount'] . "</td>";
echo "<td align='center'>" . "<input type='button' value= 'edit'>" . "</td>";
echo "</tr>";
}
echo "</table>";
?>
I have a web-based sign out sheet that displays a list of currently signed out assets (it does this by pulling the variables from an SQL table) I wan to take the same output and send it in an email but every time I try I either get resource id#4 or a blank output. For reasons of confidentiality I can't put up the source code for the mail aspect as its for a client. The below works fine at displaying on the main site. The environment is using WAMP any info would be appropriated.
mysql_connect("127.0.0.1", "helpdesk", "Password" )or die("cannot connect");
mysql_select_db("loaner")or die("cannot select DB");
$table=mysql_query("SELECT * FROM signout");
echo "
<div id='status'> <table cellspacing='0' cellpadding='0' border=''>
<tr>
<th>Equipment Currently Out</th>
<th>By Who</th>
<th>Return Date</th>
</tr>";
while ($row = mysql_fetch_array($table))
{
echo "<tr>";
echo "<td>" . $row['Equip'] . "</td>";
echo "<td>" . $row['User'] . "</td>";
echo "<td>" . $row['Return Date'] . "</td>";
}
echo "</tr>";
echo "</table></div>";
echo "</table></div>";
Note that the line breaks used with $table aren't necessary, I simply left them there to make it easier for you to see what's going on.
mysql_connect('127.0.0.1', 'helpdesk', 'Password' ) or die('cannot connect');
mysql_select_db('loaner')or die('cannot select DB');
$results = mysql_query('SELECT * FROM signout');
/**
* Store the HTML in a variable
*/
$table = "<div id='status'>
<table cellspacing='0' cellpadding='0' border=''>
<tr>
<th>Equipment Currently Out</th>
<th>By Who</th>
<th>Return Date</th>
</tr>";
while ( $row = mysql_fetch_array($results) ) {
$table .= "<tr>";
$table .= "<td>" . $row['Equip'] . "</td>";
$table .= "<td>" . $row['User'] . "</td>";
$table .= "<td>" . $row['Return Date'] . "</td>";
}
$table .= "
</tr>
</table>
</div>";
/**
* Mail the HTML
*/
$body = 'Blah blah blah '.$table.' blah blah blah';
mail($to, $from, $subject, $body);