Im trying to display the total for each project group. The Problem is that all the values for that field are recorded like so:
0h 06m
The sum is displaying as:
0
with the following code. How do i display the total result in proper HH:MM format
<?
$query = "SELECT taskname, SUM(tasktime) FROM tictoc WHERE uid = '6' GROUP BY taskname";
$result = mysql_query($query) or die(mysql_error());
// Print out result
while($row = mysql_fetch_array($result)) {
echo "<br />";
echo "Total ". $row['taskname']. " = <strong>". $row['SUM(tasktime)']."</strong>";
echo "<br />";
}
?>
If tasktime is a timestamp data-type, then you can use DATE_FORMAT(tasktime,'%Hh %im').
Related
$query="SELECT * FROM cse_routine WHERE batch ='$batch' and section='$section' order by 'day'";
$post = $db->select($query);
?>
<?php
if ($post) {
while ($result = $post->fetch_assoc()) {
$var = "select * from cse_routine where day = '".$result['day'] ."'";
print $result['day']."<br>";
if ($results=$db->select($var))
{
while ($rows = mysqli_fetch_assoc($results))
{
print $rows['t_slot']. " " . $rows['room']. " " . $rows['day']. " " . $rows['c_code'];
echo "<br>";
}
}
echo "<br>";
}
}
echo "<br>";
?>
Running this code; I found this:
It's ok. But actually, I want output that not print the same date repeatedly. See here Saturday, Sunday print 3 each of them but I want that Saturday print 1 and Sunday print for 1.
You just change you query to
$query="SELECT * FROM cse_routine WHERE batch ='$batch' and section='$section'
GROUP by 'day'";
You could try using DISTINCT constraint (http://www.mysqltutorial.org/mysql-distinct.aspx) in first SQL:
$query="SELECT DISTINCT day FROM cse_routine WHERE batch ='$batch' and section='$section' order by 'day'";
So you will have Saturday and Sunday only once each.
Using legacy code which uses mysql instead of mysqli/pdo so don't worry about this, I'll update the queries for this later.
Even though my current method works, I'm positive there is a cleaner way of doing this rather than a query and 3 subqueries. I mainly want to learn how to better enhance my queries and minimizing the amount of them.
What I'm trying to do is
echo out all the data for each date with the date displayed on top
Display the count of entries for each user on that particular day next to the user
For each date, at the bottom of the above 2 bits of data, display the user/s with the highest number of entries
$query = mysql_query('SELECT * FROM entries GROUP BY DATE(dt)');
$g = 0;
while ($row = #mysql_fetch_array($query))
{
$group[$g] = date('y-m-d', strtotime($row['dt']));
echo $group[$g] . "<br />";
//display the person's name for today with their count
$dayquery = mysql_query('SELECT *, COUNT(username) as total FROM entries WHERE DATE(dt) = "'.$group[$g].'" GROUP BY username ORDER BY COUNT(username) DESC');
while ($today = #mysql_fetch_array($dayquery))
{
echo $today['first_name'] . " | " . $today['total'] . "<br />";
}
//display the highest count for today
$topquery = mysql_query('SELECT COUNT(username) as highest FROM entries WHERE DATE(dt) = "'.$group[$g].'" GROUP BY username ORDER BY COUNT(username) DESC LIMIT 1');
while ($toptoday = #mysql_fetch_array($topquery))
{
echo "Highest today: " . $toptoday['highest'] . "<br /><br />" ;
}
//display the users with the highest count for today
echo "Highest users: ";
$userstopquery = mysql_query('SELECT *, COUNT(username) as total FROM entries WHERE DATE(dt) = "'.$group[$g].'" AND COUNT(username) = "' . $toptoday['highest'] . '" AND GROUP BY username');
while ($topusers = #mysql_fetch_array($userstopquery))
{
echo $topusers['first_name'] . "<br />" ;
}
$g++;
}
The trouble I'm having is that when I try and reduce these subqueries and use MAX it will only output the highest count but not all the data for each date, which is what I need, including output of the user/s with the most amount of entries for that given day.
You could start with something like this. Note that I'm not using PHP's mysql_ API as it was deprecated 3 or 4 years ago...
require('path/to/mysqli/connection/stateme.nts');
$array = array();
$query = "
SELECT e.dt
, e.username
, COUNT(*) ttl
FROM entries e
GROUP
BY e.dt
, e.username
ORDER
BY e.dt, ttl DESC;
";
$result = mysqli_query($conn,$query);
while ($row = mysqli_fetch_assoc($result))
{
$array[] = $row;
}
print_r($array);
Whats Wrong with my code its Returning 0
$query = "SELECT cid, COUNT(cid) FROM topic_reply WHERE cid='$forum_id'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo "There are ". $row['COUNT(cid)'] ." ". $row['cid'] ." items.";
echo "<br />";
}
I try all possible codes to display the results by its returng 0.. but if i remove the WHERE filter its returns all rows what happened? hehe
I'm not sure what you are trying to accomplish but I think it might be a count of a particular cid:
$query = "SELECT cid, COUNT(cid) FROM topic_reply WHERE cid='$forum_id' GROUP BY cid";
I'm having trouble adding a where clause to this query. I would like it to select from where the column 'SAT AM/SUN PM' is 'yes' AND where the column 'confirmed' is 'yes'.
This works without the 'WHERE shift_times='SAT AM/SUN PM'-- it's not outputting anything with this:
$query = "SELECT position, COUNT(confirmed) FROM volConfirm WHERE shift_times='SAT AM/SUN PM' GROUP BY position";
$result = mysql_query($query) or die(mysql_error());
// Print out result
while($row = mysql_fetch_array($result)){
echo "There are ". $row['COUNT(confirmed)'] ." ". $row['position'] ." " . $row['shift_times'] . " volunteers.";
echo "<br />";
A LITTLE MORE INFO:
The table contains records that are either confirmed with 'yes' or '0'(for no) and the shift_times are either SAT AM/SUN PM or SAT PM/SUN AM. There are several different positions. I am trying to display the final results like so-ish:
There are: "30" "Art" volunteers for "SAT AM/SUN PM"
There are: "30" "Art" volunteers for "SAT PM/SUN AM"
Ideally the rows would rotate so the echo under that would be the inverse data for "SAT PM/SUN AM"- but that seems a bit trickier...
I changed your select statement to select and group by the shift times, so one row would be selected per position, per shift time.
I added an alias of 'cnt' to your count() and updated the php to use cnt in the echo statement
No closing bracket at the end of your while loop (could be a copy and paste issue)
$query = "SELECT COUNT(confirmed) as cnt
, position
, shift_times
FROM volConfirm
WHERE confirmed='yes'
GROUP BY shift_times, position
order by shift_times, position";
$result = mysql_query($query) or die(mysql_error());
// Print out result
while($row = mysql_fetch_array($result))
{
echo "There are ". $row['cnt'] ." ". $row['position'] ." " . $row['shift_times'] . " volunteers.";
echo "<br />";
}
How can I make those dates line up with start date and end date or selectively echo the dates rather than have the while loop echo them?
For example if I can echo "Start Date" then the actual date then the "end date" and then the date.
Basically how may I echo the dates from mySQL selectively rather than using the loop to echo them?
for example if I want to echo row 2 can I do echo $row'date' (to echo the 2nd row)
On HTML Page PHP CODE:
Start Date:
End Date:
<?php
mysql_connect("host","username","password") or die(mysql_error());
mysql_select_db("UserLogins") or die(mysql_error());
$query = "SELECT * FROM Date1";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo "<li>".$row['date']."</li>";
echo "<br />";
}
?>
It sounds like to you need to constrain your query results. If you only want certain records from your database then you should filter out the ones you don't want right in the SQL.
$query = "SELECT * FROM Date1 WHERE date >= ? AND date <= ?";
If you use prepared statements you can plug in values for the two questions marks.
first solution
$arr = Array();
while($row = mysql_fetch_array($result)){
$arr[] = $row;
}
echo $arr[0]['date']; // first
echo $arr[3]['date'];
echo $arr[7]['date'];
echo $arr[count($arr)-1]['date']; // last
second solution
$first = #mysql_fetch_assoc(mysql_query("SELECT * FROM Date1 ORDER BY date ASC LIMIT1"));
$last = #mysql_fetch_assoc(mysql_query("SELECT * FROM Date1 ORDER BY date DESC LIMIT1"));
echo $first['date'].'<br>';
echo $last['date'];
if you want to use second solution remember to put index on date column
also...
<?php
mysql_connect("host","username","password") or die(mysql_error());
mysql_select_db("UserLogins") or die(mysql_error());
$query = "SELECT min(date) as startdate, max(date) as enddate FROM Date1";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);
if($row) {
echo "Start Date: " . $row['startdate'] . "<br />\n";
echo "End Date: " . $row['enddate'] . "<br />\n";
}
?>
???
err...
<?php
mysql_connect("host","username","password") or die(mysql_error());
mysql_select_db("UserLogins") or die(mysql_error());
$query = "SELECT * FROM Date1";
$result = mysql_query($query) or die(mysql_error());
echo "<table border=\"0\">\n<tr><th>Start Date</th><th>End Date</th></tr>";
$startRow = true;
while($row = mysql_fetch_array($result)) {
if($startRow) {
echo "\n<tr><td>";
} else {
echo "<td>";
}
echo $row['date'];
if($startRow) {
echo "</td>";
} else {
echo "</td></tr>";
}
$startRow = !$startRow;
}
if(!$startRow) {
echo "</tr>";
}
echo "\n</table>\n";
?>
???