I am generating a dynamic table, from the databasw (SQL Server 2012) using php. Using the code pasted below :
<table class="table table-bordered" id="tb">
<tr style="BACKGROUND-COLOR: DarkGrey ">
<th colspan="4" style="text-align:Center;">Objectives and Targets </th>
</tr>
<tr class="tr-header">
<th>Objectives / Measures</th>
<th colspan="2">Targets / Achievements / Timelines</th>
<th style="text-align:Center;width:1%;" rowspan="2">Weightage %</th>
</tr>
<tr>
<th> </th>
<th> </th>
<th style="width:10%;"> Date </th>
</tr>
<tbody>
<?PHP
$myquery=" select replace(Objectives, '||<==', '') as Objectives, replace(Targets, '==', '') as Targets, replace(Weightage, '==', '') as Weightage, Corporate_Objective, Corporate_Objective_Weightage from Appraisal_Objectives WHERE Serial_Number='$Serial_Number' ORDER BY Row_Number asc";
$fetched=sqlsrv_query($conn,$myquery) ;
if( $fetched === false ) { die( print_r( sqlsrv_errors(), true ));}
while($res=sqlsrv_fetch_array($fetched,SQLSRV_FETCH_ASSOC))
{
$Corporate_Objective=$res['Corporate_Objective'];
$Weightage=$res['Weightage'];
$Objectives=$res['Objectives'];
$Targets=$res['Targets'];
$Corporate_Objective_Weightage=$res['Corporate_Objective_Weightage'];
echo "<tr><td>".$Corporate_Objective."</td>";
echo "<td></td>";
echo "<td></td>";
echo "<td><b>".$Corporate_Objective_Weightage."</b></td></tr>";
echo "<tr><td>".$Objectives."</td>";
echo "<td>".$Targets."</td>";
echo "<td></td>";
echo "<td>".$Weightage."</td></tr>";
}
?>
</tbody>
</table>
Issue
The output is creating many unwanted, blank rows inbetween each row! I haven't added any <br/> or and extra row in the code. Also, the rows in the databse do not have any spaces.
Where could I have gone wrong. Appreciate any help/suggestion. Thanks in advance.
ScreenShot of the dynamic table output
I changed my SQL Query and used COALESCE. The code is as below :
select Targets,target_date,ROW_NUMBER,
COALESCE(Corporate_Objective,Objectives) AS Objectives,
COALESCE(Corporate_Objective_Weightage,Weightage) AS Weightage
FROM Appraisal_Objectives WHERE Serial_Number like '%1153';
This removed all the spaces in the table :
Related
I have a query which select the last entered data into the database month wise and i have assigned it into an array and tried to display it inside a table which works fine but the data is combined togeather as shown below
Example - the first numeric data you see on the table 1245871 should be displayed as follows
Month - 12
Closing Mileage - 45871
But as you can see on the image both the data are combined togeather how can i seperate these two values and display them accordingingly. The code for this is as below,
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$querymain = "SELECT * FROM users";
$resultsmain = mysqli_query($connect,$querymain);
if ($resultsmain->num_rows>0) {
while ($userid = mysqli_fetch_assoc($resultsmain)) {
?>
<table class="table" class="mt-3 mb-3">
<thead class="bg-dark text-light">
<tr>
<th colspan="3"><?php echo $userid['id']; ?></th>
<th colspan="3"><?php echo $userid['name']; ?></th>
<th colspan="3"><?php echo $userid['approved_kmpl'];?> KM</th>
</tr>
</thead>
<thead>
<tr>
<th>Month</th>
<th>Closing Mileage</th>
<th>Usage For Month</th>
<th>Required Per Month</th>
<th>Excess Used</th>
<th>(%)</th>
<th>KM/L</th>
<th>Consumed Liters</th>
<th>Cost</th>
</tr>
</thead>
<tbody>
<?php
$closingmileage = "SELECT extract(MONTH from date) as Month,
MAX(mileage) FROM mileagesnew
WHERE user_id='".$userid['id']."'
AND extract(YEAR FROM date) ='2020'
group by Month
ORDER BY month desc";
$closingres = mysqli_query($connect,$closingmileage);
if ($closingres->num_rows>0) {
while ($closingrow = mysqli_fetch_assoc($closingres)) {
$data =array($closingrow);
foreach($data as $value){
print_r($value);
?>
<tr>
<td> <?php echo implode($value); ?> </td>
</tr>
<tr>
<td> <?php echo implode($value); ?> </td>
</tr>
<?php
}
}
}
else{
echo "No Data Found";
}
?>
</tbody>
</table>
<?php
}
}
You have 2 columns returned in the resultset of the query Month and mileagesnew. So $closingrow will be an array with 2 members, named as the columns names from your table, or the alias you give the column name in the query. So all you need to do is output $closingrow['Month'] and $closingrow['mileagesnew'] seperately in the 2 table cells
Also I assume you want the 2 values on the same row of the tabel to output both cells within the same <tr>...</tr> along with the empty cells you have not filled yet
<?php
$closingmileage = "SELECT extract(MONTH from date) as Month,
MAX(mileage) FROM mileagesnew
WHERE user_id='".$userid['id']."'
AND extract(YEAR FROM date) ='2020'
group by Month
ORDER BY month desc";
$closingres = mysqli_query($connect,$closingmileage);
if ($closingres->num_rows>0) {
while ($closingrow = mysqli_fetch_assoc($closingres)) {
echo "<tr>
<td>$closingrow[Month]}</td>
<td>$closingrow[mileagesnew ]</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>";
}
} else{
echo "No Data Found";
}
?>
I am Using Data table jQuery plugin for filtring my data.
this is my sql query
$sql = "SELECT * FROM project_task where emp_id ='$empid' AND task_type ='random' ORDER BY task_id DESC";
code of table is
<table id="example" class="table table-hover" style="width:100%">
<thead>
<tr style="background: #064769;color: #FFF;">
<th>id</th>
<th>Task</th>
<th > Description </th>
<th> Hours </th>
<th> Date </th>
<th> Rating </th>
</tr>
</thead>
<tbody id="tbody">
<tr>
<td> <?php echo $row["task_id"] ?></td>
<td> <?php echo $row["task"] ?></td>
<td> <?php echo $row["description"]; ?> </td>
<td> <?php echo $row["hours"]; ?> </td>
<td> <?php echo $row["submit_date"];?></td>
<td> <?php echo $row["submit_date"];?></td>
</tr>
</tbody>
</table>
as per I am Using DESC in sql query. task has been shown in DESC order but not when i remove data table plugin it is working fine. I think there is filter which is working on page load. I was using cdn now i installed it into local i am finding that there is any function which is sorting it but i have not pro in JS if you know how to solve it please tell me.
I am attaching screen shot also: which is showing tasks are sorted automatically ASC order.
this is my jQuery function for data table:
$(document).ready(function() {
$('#example').DataTable();
});
in your jquery code you have pass arguments like:
$(document).ready(function() {
$('#example').DataTable( {
"paging": true,
"ordering": false,
"info": true
} );
});
you can also see documentation for the more clarity .
<table class="table table-bordered table-hover table-responsive bgwhite clgray">
<thead>
<tr>
<th class="text-center text-capitalize" width="20">
No
</th>
<th class="text-center text-capitalize">
idbarang
</th>
<th class="text-center text-capitalize">
Kategori
</th>
<th class="text-center text-capitalize">
Merk
</th>
<th class="text-center text-capitalize">
Foto
</th>
<th class="text-center text-capitalize">
Stok
</th>
</tr>
</thead>
<?php
$queryStok = "SELECT SUM(stok) AS jumlahbarang FROM tabelstok WHERE idjenisusaha=1 GROUP BY idbarang";
$resultStok = mysqli_query($link,$queryStok);
$queryStok2 = "SELECT * FROM tabelbarang,tabelmerk WHERE tabelbarang.idmerk=tabelmerk.idmerk";
$resultStok2= mysqli_query($link,$queryStok2);
while ($datastok=mysqli_fetch_assoc($resultStok,$resultStok2)) {
?>
<tbody>
<tr>
<td class="text-center text-capitalize">
<?php echo $datastok['merk'] ?>
</td>
<td class="text-center text-capitalize">
No
</td>
<td class="text-center text-capitalize">
No
</td>
<td class="text-center text-capitalize">
No
</td>
<td class="text-center text-capitalize">
No
</td>
<td class="text-center text-capitalize">
<?php echo $datastok['jumlahbarang'];?>
</td>
</tbody>
<?php } ?>
</table>
How can I show the result of 2 tables with 2 queries, in A SINGLE loop.
If you want to show the result of two queries without writting two while loops, you should use mysqli_multi_query() function to execute multiple queries at once. Each statement should have a semicollon at the end (the last statement should not contain the semicollon). You can add as many queries as you want.
$query = "YOUR FIRST STATEMENT;";
$query .= "YOUR SECOND STATEMENT";
$result = mysqli_multi_query($link, $query);
do {
$result = mysqli_store_result($link);
while ($row = mysqli_fetch_row($result)) {
// DO YOUR STUFF HERE WITH $row
}
mysqli_free_result($result);
} while (mysqli_next_result($link));
If you want to merge the queries into one, then if there is a column which connects/associates both tables, then you should use JOIN when you build the query. Example:
SELECT customers.id, customers.name FROM customers
INNER JOIN details ON customers.id=details.user_id;
This merges the elements from details table with elements from customers table, linked by a common value (the customers ids).
As you didn't show the structure of the database, and tables I can't show an example based on your code, just a general one.
I have a database full of store information and personal transactions. I have two webpages. One takes user input for the store name and runs a query pulling all transaction info for that store. I have another page almost identical that takes input for a start date and an end date and then runs a query for transactions between that date. The one for the store name works perfectly, but the one for the date doesn't display anything. I am still learning html/php/sql but I know exactly how the first page works so I cannot see why the same doesn't work for the second. If someone could help me, I'd appreciate it.
Here is my first page for the store name search that works.
<?php
$transaction = $_REQUEST["StoreName"];
require_once 'login.php';
$connection = mysqli_connect(
$db_hostname, $db_username,
$db_password, $db_database);
$sql = "SELECT * FROM PURCHASE WHERE StoreName LIKE '%".$transaction."%'";
$result = $connection->query($sql);
?>
Purchases Made From <?php echo $transaction ?>
<table border="2" style="width:100%">
<tr>
<th width="15%">Item Name</th>
<th width="15%">Item Price</th>
<th width="15%">Purchase Time</th>
<th width="15%">Purchase Date</th>
<th width="15%">Category</th>
<th width="15%">Rating</th>
</tr>
</table>
<?php
if($result->num_rows > 0){
// output data of each row
while($rows = $result->fetch_assoc()){ ?>
<table border="2" style="width:100%">
<tr>
<td width="15%"><?php echo $rows['ItemName']; ?></td>
<td width="15%"><?php echo $rows['ItemPrice']; ?></td>
<td width="15%"><?php echo $rows['PurchaseTime']; ?></td>
<td width="15%"><?php echo $rows['PurchaseDate']; ?></td>
<td width="15%"><?php echo $rows['PurchaseCategory']; ?></td>
<td width="15%"><?php echo $rows['Rating']; ?></td>
</tr>
<?php
}
}
?>
And here is the page for the date search that returns no data.
<?php
$startDate = $_REQUEST["StartDate"];
$endDate = $_REQUEST["EndDate"];
require_once 'login.php';
$connection = mysqli_connect(
$db_hostname, $db_username,
$db_password, $db_database);
$sql = "SELECT * FROM PURCHASE WHERE PurchaseDate BETWEEN '%".$startDate."%' and '%".$endDate."%'";
$result = $connection->query($sql);
?>
Purchases Made Between <?php echo $startDate ?> and <?php echo $endDate ?>
<table border="2" style="width:100%">
<tr>
<th width="15%">Item Name</th>
<th width="15%">Item Price</th>
<th width="15%">Purchase Time</th>
<th width="15%">Purchase Date</th>
<th width="15%">Category</th>
<th width="15%">Rating</th>
</tr>
</table>
<?php
if($result->num_rows > 0){
// output data of each row
while($rows = $result->fetch_assoc()){ ?>
<table border="2" style="width:100%">
<tr>
<td width="15%"><?php echo $rows['ItemName']; ?></td>
<td width="15%"><?php echo $rows['ItemPrice']; ?></td>
<td width="15%"><?php echo $rows['PurchaseTime']; ?></td>
<td width="15%"><?php echo $rows['PurchaseDate']; ?></td>
<td width="15%"><?php echo $rows['PurchaseCategory']; ?></td>
<td width="15%"><?php echo $rows['Rating']; ?></td>
</tr>
<?php
}
}
?>
Obviously the initial search pages that link to both of these output pages differ a bit more but I can confirm that the date output page is receiving the correct dates from the search page.
EDIT: Here is the search page for the date.
<!DOCTYPE html>
<html>
<head>
<title>Output 2</title>
</head>
<body>
<h1>Required Output 2</h1>
<h2>Transaction Search By Date</h2>
<br/>
<br/>
<form action="outputout2.php" method="get">
Start Date: <input type="date" name="StartDate">
End Date: <input type="date" name="EndDate">
<input name="Add Merchant" type="submit" value="Search">
</form>;
</body>
</html>
This sort of problem is usually the result of misformatted dates.
Your query, if I'm reading it right, says this when you've finished assembling it in php and sent it to the SQL server:
SELECT *
FROM PURCHASE --wrong!
WHERE PurchaseDate BETWEEN '%5/1/2016%' and '%5/8/2016%'
This is not going to work.
For one thing, the % signs only work with the LIKE operator, not on equality and inequality operators.
For another thing, different makes and models of database server software use differently formatted date strings. MySQL and SQL Server will work fine with string formats like 2016-05-01. But you have to look up the appropriate ways of doing this for the kind of SQL software product you are using. This kind of stuff is not very vendor-portable.
Third: your PurchaseDate column in your table needs to have a date-like datatype for this kind of thing to work.
Fourth: if your PurchaseDate items are timestamps -- that is, if that have both dates and times -- you can't reliably use the BETWEEN operator. Here's why: Suppose you have a value of 2016-05-08 11:50:00 When you compare that to an end date 2016-05-08, it lies beyond the end date. So, what you really need is this:
WHERE PurchaseDate >= '2016-05-01'
AND PurchaseDate < '2016-05-09' -- the day AFTER your end date
That construct performs the same as BETWEEN and doesn't screw up the last days' data.
Edit so it's MySQL you're using.
Workbench is a client program. So is PHP. The server is MySQL. When you do an operation like
DateColumn < 'string constant'
MySQL implicitly converts the string constant to a date. If the format of the string constant is close enough to ISO 8601 for the server to figure out what it means, it will convert it. 2016-12-13 is exactly right. 2016-5-31 is close enough for MySQL, and so is 2016-5-5. 2016/12/31 doesn't work. Neither does 5/5/16.
Germane to your question is this: MySQL cannot convert %2016-5-5% (with the percent signs) to a date.
When MySQL can't convert a date, it comes up with NULL. Comparisons with NULL values are weird. And, in the immortal words of Hunter S. Thompson, "when the going gets weird, the weird turn pro". NULL weirdness is probably the root cause of your empty result set in the query you showed.
I am using the following code to generate tables from data from my database. it groups the data by the employee. What I need is to display the totals for the last four columns at the end of each employees table. The Hours, OT, Travel and TOT columns.
Code:
$current_user_name = false;
while($row = mysql_fetch_array($result)) {
// listing a new employee? Output the heading, start the table
if ($row['user_name'] != $current_user_name) {
if ($current_user_name !== false)
echo '</table>'; echo '[divider_padding]';// if we're changing employee, close the table
echo '
<h5>'.$row['last_name'].', '.$row['first_name'].'</h5>[minimal_table]
<table>
<tr>
<th>Date</th>
<th class="tableleft">Description</th>
<th class="tableleft">Job</th>
<th class="tableleft">Activity</th>
<th class="tableleft">Comments</th>
<th class="tableright">Hours</th>
<th class="tableright">OT</th>
<th class="tableright">Travel</th>
<th class="tableright">TOT</th>
</tr>';
$current_user_name = $row['user_name'];
}
// output the row of data
echo '<tr>
<td style="width:75px">'.$row['labor_date'].'</td>
<td class="tableleft">'.$row['description'].'</td>
<td class="tableleft">'.strtoupper($row['job']).'</td>
<td class="tableleft">'.$row['activity'].'</td>
<td class="tableleft">'.$row['comments'].'</td>
<td class="tableright">'.$row['rthours'].'</td>
<td class="tableright">'.$row['othours'].'</td>
<td class="tableright">'.$row['trthours'].'</td>
<td class="tableright">'.$row['tothours'].'</td>
</tr>
';}
echo '</table>[/minimal_table]'; // close the final table
}
?>
I am stuck after trying some tests and can not figure this out.
This is the query gathering the data:
$result = mysql_query("SELECT * FROM data WHERE labor_date BETWEEN '$start' AND '$end' Order by last_name, labor_date ASC");
Any help would be appreciated.
This line is missing an open bracket:
if ($current_user_name !== false)
Should be:
if ($current_user_name !== false) {