I have a PHP loop which draws a table based off a MYSQLi query.
What i would like to do is create this table based on a userid (in my case the column called 'badgeid')
At the moment its creating one table
$sql = "SELECT first_name,last_name,signintime,signouttime FROM `signin_entries` WHERE iscleaner ='YES' AND signindate = curdate()";
$list_visitors_result=mysqli_query($con,$sql);
$count_visitors = mysqli_num_rows($list_visitors_result);
if($count_visitors != 0) {
while($row = mysqli_fetch_array($list_visitors_result)){
$signintime = $row['signintime'];
$signouttime = $row['signouttime'];
$firstname = $row['first_name'];
$lastname = $row['last_name'];
echo " <tr><td>$firstname $lastname</td>
<td>$signintime</td>";
if ($signouttime == ""){
echo "<td>Not Signed Out Yet</td>";
} else {
echo "<td>$signouttime</td>";
}
$timeFirst = strtotime(date("Y/m/d") . " " . $signintime);
$timeSecond = strtotime(date("Y/m/d") ." " . $signouttime);
//below checks if th second time is less than the first time than it must be from the day before so add 24 hours eg (signin time 23:30:00 signout time 07:30:30 would be 08:00:30 difference)
if ($timeSecond < $timeFirst)
{
$timeSecond = $timeSecond + 86400;
}
if ($signouttime == ""){
echo "<td>Cleaner Has Not Signed Out Yet</td>";
} else {
$differenceInSeconds = $timeSecond - $timeFirst;
echo "<td class='rowDataSd'>".converttime($differenceInSeconds)."</td>";
}
echo "</tr>";
}
}
//below function converts the seconds difference into hh:mm:ss format to the nearest second
function converttime($seconds) {
$t = round($seconds);
return sprintf('%02d:%02d:%02d', ($t/3600),($t/60%60), $t%60);
}
echo "<tr><th></th><th></th><th></th><th>Total Time Worked</th><tr><td></td><td></td><td></td><td class='totalCol'>Total:</td></tr>";
?>
</table>
</div>
</div>
</div>
</div>
</div>
</section>
</div>
Which is great but i really need a table per 'badgeid'
You could do it like that:
$sql = "SELECT first_name,last_name,signintime,signouttime FROM `signin_entries` WHERE iscleaner ='YES' AND signindate = curdate() ORDER BY badgeid ASC";
$list_visitors_result=mysqli_query($con, $sql);
$count_visitors = mysqli_num_rows($list_visitors_result);
if ($count_visitors != 0) {
echo '<table>';
$current_badgeid = '';
while ($row = mysqli_fetch_array($list_visitors_result)) {
if ($current_badgeid == '') {
$current_badgeid = $row['badgeid']; //define if empty, for the first table
}
if($row['badgeid'] != $current_badgeid){
echo '</table><table>';
$current_badgeid = $row['badgeid'];
}
$signintime = $row['signintime'];
$signouttime = $row['signouttime'];
$firstname = $row['first_name'];
$lastname = $row['last_name'];
echo " <tr><td>$firstname $lastname</td><td>$signintime</td>";
if ($signouttime == "") {
echo "<td>Not Signed Out Yet</td>";
} else {
echo "<td>$signouttime</td>";
}
$timeFirst = strtotime(date("Y/m/d") . " " . $signintime);
$timeSecond = strtotime(date("Y/m/d") ." " . $signouttime);
//below checks if th second time is less than the first time than it must be from the day before so add 24 hours eg (signin time 23:30:00 signout time 07:30:30 would be 08:00:30 difference)
if ($timeSecond < $timeFirst) {
$timeSecond = $timeSecond + 86400;
}
if ($signouttime == "") {
echo "<td>Cleaner Has Not Signed Out Yet</td>";
} else {
$differenceInSeconds = $timeSecond - $timeFirst;
echo "<td class='rowDataSd'>".converttime($differenceInSeconds)."</td>";
}
echo "</tr>";
}
echo '</table>';
}
First, you add ORDER BY badgeid ASC to your query. Then you open a table before the loop starts, and you define a $current_badgeid var. Each time the badgeid changes, you close and reopen a table.
Related
Database:
Calendar 1:
Calendar 2:
Calendar 3:
I'm trying to getting both events from the database onto Monday 4th and I'm wondering why I am only getting one event for April and May.
$sql = "SELECT title, contact, contact_email, DAYOFMONTH(start_date)
FROM $caltbl WHERE start_date LIKE '$year-$month%'";
$result = mysql_query($sql)or die(mysql_error());
$row_count = mysql_num_rows($result);
$record = mysql_fetch_array($result);
while ($day_num <= $days_in_month) {
echo "<td width=\"42\" valign=\"top\">
<a href='index.php?day=$day_num-$title-$year'>
$day_num</a><p/>";
if ($day_num == $record['DAYOFMONTH(start_date)']){
echo " ".$record['title']. "<br/>";
} else{
echo "<br /> " . "<br/> ";
}
echo "</td>";
$day_num++;
$day_count++;
EDIT:
while ($day_num <= $days_in_month) {
echo "<td width=\"42\" valign=\"top\"> <a href='index.php?day=$day_num-$title-$year'>$day_num</a><p/>";
if ($day_num == $record['DAYOFMONTH(start_date)']){
while ($event = mysql_fetch_assoc($result)) {
$array[] = $event;
}
print_r($array);
echo " ".$event['title']. "<br/>";
} else{
echo "<br /> " . "<br/> ";
}
echo "</td>";
$day_num++;
$day_count++;
// Make sure we start a new row each week
if ($day_count > 7) {
echo "</tr><tr>";
$day_count = 1;
}
}
Open a blank array:
$arr = array();
while ($event = mysql_fetch_assoc($result)) {
$arr[] = $event;//store all the data in the $arr array
}
Run a foreach loop to to print the event detail:
foreach($arr as $k=>$v){
if($v['DATEOFMONTH(start_date)']==$day_num){
echo ''.$v['title'].'<br/>';
}else{
echo "<br /> " . "<br/> ";
}
}
Hope this may help. demo
Basically I am building a calendar page that displays the months, and the days of the month(pulled from my database) and then any days that are inside the "start_date - end_date" variables are displayed with a different cell background color to the days that don't have a start or end date assigned, I have it working to an extent but it's only displaying the earliest of each months record rather than all the results.
ie.
2015-03-12(start) - 2015-03-16(end)
2015-03-03(start) - 2015-03-10(end)
And rather than display like this...
`1 2 [3 4 5 6 7 8 9 10] 11 [12 13 14 15 16] 17 18 19 20 ...`
it's just showing the [3 - 10] record, here is my current code..
<table width="100%" cellspacing="0">
<?php
$cmonth = date('F');
$cyear = date('Y');
$sql = "SELECT * FROM calendar WHERE year = '$cyear' ORDER BY m_order ASC";
$res = mysql_query($sql);
while ($rows = mysql_fetch_array($res)) {
$month_end = $rows['days_in_month'];
$month_name = $rows['month_name'];
$m_order = $rows['m_order'];
$sql2 = "SELECT * FROM trips WHERE start_date LIKE '____-0$m_order-__' ORDER BY start_date ASC";
$res2 = mysql_query($sql2);
$row = mysql_fetch_assoc($res2);
$stdate = $row['start_date'];
$s = date_parse_from_format("Y-m-d", $stdate);
$endate = $row['end_date'];
$e = date_parse_from_format("Y-m-d", $endate);
$start = $s['day'];
$end = $e['day'];
?>
<tr>
<td width="80px"><?php echo $month_name; ?></td>
<?php
foreach(range(1, $month_end) as $days)
{
if(in_array($days, range($start, $end)))
{
echo "<td style=\"background-color: #ccc;\" align=\"center\">" . $days . " </td>";
}
else
echo "<td align=\"center\">" . $days . "</td>";
}
?>
</tr>
<?php } ?>
</table>
Also I am aware of the dangers not using mysqli but I am just learning this on my local machine and plan on researching updated strategies once I get the functions working, so I'll know if my functions are broken or my coding is.
Thanks
try with common function to read data from table
function db_set_recordset($sql) {
$qry = mysql_query($sql);
$row= array();
while($out = mysql_fetch_assoc($qry)) {
$row[] = $out;
}
return $row;
}
$cmonth = date('F');
$cyear = date('Y');
$sql = "SELECT * FROM calendar WHERE year = '$cyear' ORDER BY m_order ASC";
$res = mysql_query($sql);
while ($rows = mysql_fetch_array($res)) {
$month_end = $rows['days_in_month'];
$month_name = $rows['month_name'];
$m_order = $rows['m_order'];
$sql2 = "SELECT * FROM trips WHERE start_date LIKE '____-0$m_order-__' ORDER BY start_date ASC";
$res2 = mysql_query($sql2);
$row = db_set_recordset($res2);
$stdate = $row['start_date'];
$s = date_parse_from_format("Y-m-d", $stdate);
$endate = $row['end_date'];
$e = date_parse_from_format("Y-m-d", $endate);
$start = $s['day'];
$end = $e['day'];
?>
<tr>
<td width="80px"><?php echo $month_name; ?></td>
<?php
foreach(range(1, $month_end) as $days)
{
if(in_array($days, range($start, $end)))
{
echo "<td style=\"background-color: #ccc;\" align=\"center\">" . $days . " </td>";
}
else
echo "<td align=\"center\">" . $days . "</td>";
}
?>
</tr>
<?php } ?>
</table>
You are only fetching one row of your result set from your trips table. You need something like this to get the second one.
while ( $row = mysql_fetch_assoc($res2) ) {
/* process each row */
}
You're doing this right for your other result set. Take a look at the examples here: http://php.net/manual/en/function.mysql-fetch-assoc.php
You'll need to run through the result set rows and accumulate your "hot" days, then render the days just once. Something like this:
$sql2 = "SELECT * FROM trips WHERE start_date LIKE '____-0$m_order-__' ORDER BY start_date ASC";
$res2 = mysql_query($sql2);
while ($row = mysql_fetch_assoc($res2)) {
$stdate = $row['start_date'];
$s = date_parse_from_format("Y-m-d", $stdate);
$endate = $row['end_date'];
$e = date_parse_from_format("Y-m-d", $endate);
$start = $s['day'];
$end = $e['day'];
$hot_days = array();
foreach(range(1, $month_end) as $days) {
$hot_days[$days] = 0;
if(in_array($days, range($start, $end))) {
$hot_days[$days] ++;
}
}
}
/* now you have a $hot_days array with nonzero values for interesting days */
?>
<tr>
<td width="80px"><?php echo $month_name; ?></td>
<?php
foreach(range(1, $month_end) as $day) {
if($hot_days[$day] > 0) {
echo "<td style=\"background-color: #ccc;\" align=\"center\">" . $days . " </td>";
}
else {
echo "<td align=\"center\">" . $days . "</td>";
}
}
?>
</tr>
With respect, I don't have time to debug this.
I want to create a basic time slot booking system. But I've run into some problems.
I have created a calendar where I can see that something is happening on a speific date:
It takes the information from my database table. I'm using PHPMyAdmin, and I've added the date manually in there.
As you can see in the table, I would also like to use a start time and an endtime.
If I click a date in my calendar, I create the add-event link like this:
<?php
if (isset ( $_GET ['v'] )) {
echo "<a href='test.php . ?month=" . $month . "&day=" . $day . "&year=" . $year . "&v=true&f=true'>Add Event</a>";
if (isset ( $_GET ['f'] )) {
include ("test.php");
}
$sqlEvent = "SELECT * FROM calendar WHERE eventDate='" . $month . "/" . $day . "/" . $year . "'";
$resultEvents = mysqli_query ( $mysqli1, $sqlEvent );
echo "<br>";
while ( $events = mysqli_fetch_array ( $resultEvents ) ) {
}
}
?>
Clicking this makes my url look somehting like this: test.php%20.%20?month=04&day=18&year=2014&v=true&f=true#
I've included my whole test.php file:
<?php
$hostname = 'localhost';
$username = 'root';
$password = '';
$dbname = "calendar";
$error = 'Cannot connect to the database';
$mysqli1 = new mysqli ( $hostname, $username, $password, $dbname ) or die ( $error );
if (isset ( $_GET ['day'] )) {
$day = $_GET ['day'];
} else {
$day = date ( "j" );
}
if (isset ( $_GET ['month'] )) {
$month = $_GET ['month'];
} else {
$month = date ( "n" );
}
if (isset ( $_GET ['year'] )) {
$year = $_GET ['year'];
} else {
$year = date ( "Y" );
}
$dateToCompare = $month . '/' . $day . '/' . $year;
echo "<br/><br/><h3>Reservations</h3>";
$timearray = array(8,9,10,11,12,13,14,15,16,17,18,19,20,21,22);
$tablecolor = 1;
echo "<table style='width: 90%;'>";
echo "<tr>";
echo "<th>";
echo "Time";
echo "</th>";
echo "<th>";
echo "Status";
echo "</th>";
echo "</tr>";
foreach ($timearray as $timearrays) {
if($tablecolor %2 == 0) {
echo "<tr>";
}
else {
echo "<tr style='background-color: rgb(0,100,255); background: rgb(0,100,255);'>";
}
echo "<th>";
if ($timearrays == 8) {echo "<h3>8-9am</h3>";}
if ($timearrays == 9) {echo "<h3>9-10am</h3>";}
if ($timearrays == 10) {echo "<h3>10-11am</h3>";}
if ($timearrays == 11) {echo "<h3>11-12am</h3>";}
if ($timearrays == 12) {echo "<h3>12-13am</h3>";}
if ($timearrays == 13) {echo "<h3>13-14am</h3>";}
//Develop your timeslots here to display as required
echo "</th>";
echo "<td>";
$sql = "SELECT * FROM calendar WHERE eventDate='" . $dateToCompare . "'AND timestart >= $timearrays AND endtime <= $timearrays;";
$result = mysqli_query($mysqli1,$sql);
if (mysqli_num_rows($result) == 0) {
echo "<a href='#'><h3 style='color: rgb(255,0,0);'>Reserve</h3></a>";
} else {
echo "<h3>Not Available, taken by someone</h3>";
while($row = mysql_fetch_array($result)) {
echo "<br />";
}
}
echo "</td>";
echo "</tr>";
$tablecolor++;
}
echo "</table>";
?>
As you can see, I try to display my timeslots: $sql = "SELECT * FROM calendar WHERE eventDate='" . $dateToCompare . "'AND timestart >= $timearrays AND endtime <= $timearrays;";
I think that I get my dates correctly from the URL, but I don't see any reservations for a given date.
I hope that some of you can help me out. And please ask, if I need to provide more of my code.
Or if anyone has a better idea on how to do this, please share.
I'm not really sure if I should be conserned about time. I basically just want the ability to click on "Reserve" and then reverse that specific time slot. So maybe a start and an end time isn't nessary?
Try to debug your application. But I guess the problem will probably be the $timearrays in the query you create. I don't think (haven't tested) it works that way.
My suggestion, print out $sql after you've initialized it and see what it returns. Then open PHPmyadmin (which I guess you're using, by the looks of your data) and see what it returns there.
I have these mysql tables:
(players):
(environment_killers):
(killers):
(player_deaths):
And a PHP page that prints this result:
$timenow = time();
$dayago = $timenow - (86400 * 1);
$monstrums= $SQL->query('SELECT * FROM z_monsters GROUP by name;');
$killers_monsters = mysql_query('
SELECT COUNT(name) as liczba
FROM environment_killers,player_deaths,killers
where player_deaths.id = killers.death_id
and killers.id = environment_killers.kill_id
and name LIKE "%'.$monstrums[name].'"
and date > '.$dayago.';');
I need this PHP page to print the $killers_monsters result where player_deaths.player_id = players.id and players.world_id = '.$world_id.'
Let me guess, this is an open Tibia server? XD
Anyway, here you go (assuming your query is correct):
if($killers_monsters == true) {
if(mysql_num_rows($killers_monsters) > 0) {
while($row = mysql_fetch_object($killers_monsters)) {
echo "Total count: " . $row->liczba . "<br />";
}
} else {
echo "Total count: 0.<br />";
}
} else {
echo "An error occured! Total count: 0.<br />";
}
I have two tables that my Query affects. The tables are called flightSched & Alteration. In both tables the arrivalTime column is of type TIME.
The query runs just fine until the $CurrentTimePlus4Hours variable elapses midnight. When this happens, the query doesn't yield any records, although the table has data ranging from all times of the day and night. Find below my code.
$currentDay = date('l');
$CurrentTimeMinus30min = date('H:i:s', strtotime('-30 minutes'));
$CurrentTimePlus4Hours = date('H:i:s', strtotime('+240 minutes'));
$query2 = "SELECT * FROM flightSched WHERE don = '$currentDay'
AND depOrArriv='Arrival'
AND arrivalTime BETWEEN '$CurrentTimeMinus30min' AND '$CurrentTimePlus4Hours'
ORDER BY arrivalTime ASC ;";
$query2 .= "SELECT * FROM Alteration WHERE don = '$currentDay'
AND depOrArriv='Arrival'
AND arrivalTime BETWEEN '$CurrentTimeMinus30min' AND '$CurrentTimePlus4Hours'
ORDER BY arrivalTime ASC ;";
/* execute multi query */
if ($mysqli->multi_query($query2)) {
do
{
if ($result = $mysqli->store_result()) {
while ($row = $result->fetch_array()) {
echo "<tr " . $variable . "><td>";
echo '<img src="php/displayImage.php?id=' . $row['id'] . ' "align="middle" width="110" height="35" >' . " "
. $row['flightNo'] ;
$flightNo = $row['flightNo'];
echo "</td><td>";
echo $row['airline'];
echo "</td><td>";
echo $row['origin'];
echo "</td><td>";
echo $row['arrivalTime'];
echo "</td><td>";
echo $row['status'];
echo "</td></tr>";
if ($variable == 'id=basicBoard')
{
$variable = 'class=alt';
//echo $variable;
}
elseif ($variable == 'class=alt')
{
$variable = 'id=basicBoard';
//echo $variable;
}
}
$result->free();
}
/* print divider */
if ($mysqli->more_results()) {
// printf("-----------------</br>");
}
}
while (#$mysqli->next_result());
echo "</table> <br/> <br/>";
}
I have tried to figure out why this happens but failed to fix the issue. Can someone kindly point out where I am going wrong in the code?
I think you just need to compensate for the time lapse by offsetting what the "current" day is:
$midnight = strtotime('today');
$now = time();
$tomorrow = $midnight + 86400;
$diff = $tomorrow - $now;
$offset = $now;
// Number of seconds in 240 minutes = 14400
if ($diff <= 14400) {
$offset += $diff;
}
$currentDay = date('l', $offset);
$CurrentTimeMinus30min = date('H:i:s', strtotime('-30 minutes'));
$CurrentTimePlus4Hours = date('H:i:s', strtotime('+240 minutes'));