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) {
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 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 :
I am trying to show mysql_fetch_array() results in a table.
I want to show guests name,their country and their agreed time who are traveling on a same date.
Following code works fine. The code fetches the row values and prints it.
$select_guests = mysql_query('SELECT name FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error()); // query for getting guests for the same date
while($row = mysql_fetch_array($select_guests, MYSQL_ASSOC)) { //visitor / guest loop starts here
echo $row['name'].'<br/>';
}
$select_country = mysql_query('SELECT country FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error()); // query for getting guests for the same date
while($row = mysql_fetch_array($select_country, MYSQL_ASSOC)) { //country of visitor / guest loop starts here
echo $row['country'].'<br/>';
}
$select_agreed_time = mysql_query('SELECT agreed_time FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error()); // query for getting guests for the same date
while($row = mysql_fetch_array($select_agreed_time, MYSQL_ASSOC)) { //visitor / guest agreed time loop starts here
echo $row['agreed_time'].'<br/>';
}
if there are 5 guests for a same date I am getting all of their names one below another when I execute the above code. Same I am getting there countries and agreed time too.
Now I want to show those results in a HTML table.
I tried several line of code but nothing works.
My HTML table should be as following:
<table class="table-fill">
<thead>
<tr>
<th class="text-left">Name</th>
<th class="text-left">From</th>
<th class="text-left">Agreed Time</th>
</tr>
</thead>
<tbody class="table-hover">
<tr>
<td class="text-left">Name 1</td>
<td class="text-left">Country 1</td>
<td class="text-left">Ag Time 1</td>
</tr>
<tr>
<td class="text-left">Name 2</td>
<td class="text-left">Country 2</td>
<td class="text-left">Ag Time 2</td>
</tr>
<tr>
<td class="text-left">Name 3</td>
<td class="text-left">Country 3</td>
<td class="text-left">Ag Time 3</td>
</tr>
<tr>
<td class="text-left">Name 4</td>
<td class="text-left">Country 4</td>
<td class="text-left">Ag Time 4</td>
</tr>
<tr>
<td class="text-left">Name 5</td>
<td class="text-left">Country 5</td>
<td class="text-left">Ag Time 5</td>
</tr>
</tbody>
</table>
How can create that table td s according to my mysql_fetch_array() ?
The above table structure is for 5 guests found or resulted by mysql_fetch_array()
First of all I think you dont need 3 different queries for your solution..
<table class="table-fill">
<thead>
<tr>
<th class="text-left">Name</th>
<th class="text-left">From</th>
<th class="text-left">Agreed Time</th>
</tr>
</thead>
<?php
$result = mysql_query('SELECT name,country,agreed_time FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error());
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
<tr>
<td>
<?php echo $row['name']; ?>
</td>
<td>
<?php echo $row['country'];?>
</td>
<td>
<?php echo $row['agreed_time']; ?>
</td>
</tr>
<?php
}
?>
</table>
Firstly, you should use mysqli.
Secondly, shouldn't be sending so many queries to the database for information you can get in one query;
$select_guests = $mysqli->query('SELECT * FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error());
Next, you want to fetch the number of rows.
$rows = $mysqli
You should also look into the PHP for function;
for ($i = 1; $i < $rows; $i++) {
$thisRow = $select_guests->fetch_row()
echo
' <tr>
<td class="text-left">'.$select_guests['name'].'</td>
<td class="text-left">'.$select_guests['country'].'</td>
<td class="text-left">'.$select_guests['time'].'</td>
</tr>
'; //This last line is to insert a line break and indent (for tidy HTML)
}
Give this a go, hopefully I've helped you.
I haven't completely solved it for you though, in order to change over to mysqli, you will need to make a few small changes which you can find in the mysqli link I sent you. The benefits are worth it.
$select_all = mysql_query('SELECT * FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error()); // query for getting all details for the same date
while($row = mysql_fetch_array($select_all , MYSQL_ASSOC)) {//loop starts here
<tr>
<td>
<?php echo $row['name']; ?>
</td>
<td>
<?php echo $row['country'];?>
</td>
<td>
<?php echo $row['agreed_time']; ?>
</td>
</tr>
}
I have this code:
<?php
$platform_id = 7;
$query="SELECT * FROM game WHERE game_id IN (SELECT game_id FROM game_platform WHERE platform_id=".$platform_id.") AND game_release BETWEEN '2015-08-01' AND '2015-08-31' ORDER BY game_release";
$result=mysqli_query($link,$query) or die (mysqli_error());
echo"<table border=1>
<tr>
<th width=40px>Day</th>
<th width=270px>Title</th>
<th width=203px>Genre</th>
<th width=203px>Developer</th>
<th width=205px>Publisher</th>
<th width=61px>Retail?</th>
<th width=30px height=30px></th>
<th width=104px>Note</th>
</tr>";
while($row=mysqli_fetch_assoc($result))
{
echo"
<tr>
<td> ". (new DateTime($row['game_release']))->format("j") ." </td>
<td>{$row['game_name']}</td>
<td>";
$query="SELECT * FROM genre WHERE genre_id=".$row['game_genre'];
$genreresult=mysqli_query($link,$query); $genrerow=mysqli_fetch_assoc($genreresult);
echo $genrerow['genre_name'];
echo "</td>
<td>{$row['game_dev']}</td>
<td>{$row['game_pub']}</td>
<td>{$row['game_type']}</td>
<td><img src=\"images/officialwebsite.png\" title=\"Official website\"/>
<img src=\"images/youtube.png\" title=\"Trailer\"/></td>
<td>{$row['game_note']}</td>
</tr>";
}
echo"</table>";
?>
I want to show a message when there are no releases in said month, because right now I get an empty table which is not really user-friendly nor visually attractive.
I found this code on this site, but I get a syntax error due to .isEmpty.
<?php
$platform_id = 7;
$query="SELECT * FROM game WHERE game_id IN (SELECT game_id FROM game_platform WHERE platform_id=".$platform_id.") AND game_release BETWEEN '2015-08-01' AND '2015-08-31' ORDER BY game_release";
$result=mysqli_query($link,$query) or die (mysqli_error());
echo"<table border=1>
<tr>
<th width=40px>Day</th>
<th width=270px>Title</th>
<th width=203px>Genre</th>
<th width=203px>Developer</th>
<th width=205px>Publisher</th>
<th width=61px>Retail?</th>
<th width=30px height=30px></th>
<th width=104px>Note</th>
</tr>";
if($result.isEmpty())
echo "Unfortunately there are no releases this month!";
else
{
while($row=mysqli_fetch_assoc($result))
{
echo"
<tr>
<td> ". (new DateTime($row['game_release']))->format("j") ." </td>
<td>{$row['game_name']}</td>
<td>";
$query="SELECT * FROM genre WHERE genre_id=".$row['game_genre'];
$genreresult=mysqli_query($link,$query); $genrerow=mysqli_fetch_assoc($genreresult);
echo $genrerow['genre_name'];
echo "</td>
<td>{$row['game_dev']}</td>
<td>{$row['game_pub']}</td>
<td>{$row['game_type']}</td>
<td><img src=\"images/officialwebsite.png\" title=\"Official website\"/>
<img src=\"images/youtube.png\" title=\"Trailer\"/></td>
<td>{$row['game_note']}</td>
</tr>";
}
}
echo"</table>";
?>
I kept reading and there doesn't seem to be another way, because while-loops don't have 'else's?
How can I solve this? Thanks!
You seem to be mixing javascript and php as .isEmpty() is not a php function.
You need to check the row count before you display your table, so something like:
$result=mysqli_query($link,$query) or die (mysqli_error());
if (mysqli_num_rows($result) > 0) {
// your table generation code
} else {
echo "Unfortunately there are no releases this month!";
}
I would like to show the results of one database table by the use of a variable fetched from another database table like this:
mysql_select_db($database_Connection, $Connection);
$query_Recordset_bids = "SELECT * FROM bids WHERE bidder = '$userName'";
$Recordset_bids = mysql_query($query_Recordset_bids, $Connection) or die(mysql_error());
while ($row_Recordset_bids = mysql_fetch_array($Recordset_bids)) {
$totalRows_Recordset_bids = mysql_num_rows($Recordset_bids);
mysql_select_db($database_Connection, $Connection);
$query_Recordset_jobs = "SELECT * FROM jobs WHERE userID = '".$row_Recordset_bids['jobID']."'";
$Recordset_jobs = mysql_query($query_Recordset_jobs, $Connection) or die(mysql_error());
$row_Recordset_jobs = mysql_fetch_assoc($Recordset_jobs);
$totalRows_Recordset_jobs = mysql_num_rows($Recordset_jobs);
}
And then I want the output showed in the following table:
<?php if($totalRows_Recordset_jobs == 0)
echo "You have never submitted a job offer!";
else {
?>
<table width="440" border="0" cellpadding="1" cellspacing="1" id="tablejobs">
<tr>
<th width="40" bgcolor="#779BDC" scope="col">ID</th>
<th width="90" bgcolor="#779BDC" scope="col">Destination</th>
<th width="85" bgcolor="#779BDC" scope="col">Cargo</th>
<th width="85" bgcolor="#779BDC" scope="col">Due Date</th>
<th width="75" bgcolor="#779BDC" scope="col">Bid</th>
<th width="65" bgcolor="#779BDC" scope="col">Status</th>
</tr>
<?php do { ?>
<tr>
<td height="22" bgcolor="#798890" scope="col"> <?php echo $row_Recordset_jobs['userID']; ?></td>
<td bgcolor="#798890" scope="col"> <?php echo $row_Recordset_jobs['destination']; ?></td>
<td bgcolor="#798890" scope="col"> <?php echo $row_Recordset_jobs['cargo']; ?></td>
<td bgcolor="#798890" scope="col"> <?php echo $row_Recordset_jobs['due_date']; ?></td>
<td bgcolor="#798890" scope="col"> <?php echo $row_Recordset_jobs['bids']; ?> kr.</td>
<td bgcolor="#798890" scope="col"> <?php echo $row_Recordset_jobs['status']; ?></td>
</tr>
<?php } while ($row_Recordset_jobs = mysql_fetch_assoc($Recordset_jobs)); ?>
</table>
<?php
}
?>
But there is only one row shown in the table even though there are 2 or more results that match the select query.
So how do I loop through the first database table to get multiple matching variables (jobID) that I can use to my select statement to the second database table, which should show multiple results?
I suggest you to simply learn about joins. :)
You can also use a join on different databases aslong as both databases are available with the same connection/credentials.
I am not sure if I got you right that you have two databases for your tables.
If you have only one database its simple:
$query = 'SELECT *
FROM bids b
LEFT JOIN jobs j ON b.jobID = j.UserID
WHERE b.bidder = "$userName"';
Incase you have two databases use this and insert names of your two databases for <namedb1> and <namedb2>.
However, note that this is not the smartest thing to do because you cannot use any indizes, transactions, constraints or table-locks over different databases.
(As mentioned by Jay Blanchard in the comments)
$query = 'SELECT *
FROM <namedb1>.bids b
LEFT JOIN <namedb2>.jobs j ON b.jobID = j.UserID
WHERE b.bidder = "$userName"';
http://dev.mysql.com/doc/refman/5.1/en/join.html