我目前正在尝试建立一个考勤系统来跟踪我的游戏社区中的用户。截至目前,我想根据数据库中给出的日期显示真实或错误,以及本月当前的日期(日期1-11)。随着月内的进展,应用程序也会这样做。
先谢谢你,现在试图解决这个问题。
我当前的代码
include('../../config/protection.php');
include('../../config/db.php');
date_default_timezone_set("America/Los_Angeles");
//Date formating
$dateFormat = "Y-m-d H:i:s";
$begin = new DateTime( 'first day of this month' );
$end = new DateTime( 'last day of this month' );
$end = $end->modify( '+1 day' );
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
$daterange2 = new DatePeriod($begin, $interval ,$end);
$date = new DateTime();
$date1 = new DateTime();
$dateString = $date1->format($dateFormat);
$timestamp2 = strtotime($dateString);
$month2 = date('j', $timestamp2);
//generating days of the month
$month = date('m', $timestamp2);
$year = date('Y', $timestamp2);
//select username ane id
$sql = "SELECT * from rosters,ranks
inner join user_ranks on user_ranks.rank_id=ranks.id
where rosters.ruser_id = user_ranks.user_id
GROUP BY rosters.rname
ORDER BY ranks.id";
$results = mysqli_query($con, $sql);
if(!$results and $mysqliDebug) {
echo "<p>There was an error in query: $results</p>";
echo $con->error;
}
?>
<table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th class="center">Rank/Name</th>
<th class="center">Promotion Date</th>
<?php
foreach($daterange as $date)
{
echo '<th class="center">'.$date->format("d") . "</th>";
}
?>
</tr>
</thead>
<tbody>
<?php
// $rows = array();
while( $row = mysqli_fetch_assoc($results) )
{
echo "<tr>";
echo "<td>" . $row['rname'] . "</td>";
echo "<td>N/A</td>";
$userid1 = $row['ruser_id'];
// $rows[] = $row;
$attendsql = "SELECT * from attendances where user_id=$userid1";
$attendresults = mysqli_query($con, $attendsql);
$arows = array();
while( $arow = mysqli_fetch_array($attendresults) )
{
$arows[] = $arow;
}
foreach($daterange as $date1)
{
foreach ($arows as $arow )
{
$date1String = $date1->format('Y-m-d');
$createdOnDate = $arow['created_on'];
$createdDate = new DateTime( $createdOnDate );
$attendanceDateString = $createdDate->format('Y-m-d');
if ( $attendanceDateString == $date1String )
{
echo '<td>True</td>';
}
else
{
echo '<td>False</td>';
}
}
}
echo "</tr>";
}
?>
</tbody>
</table>
答案 0 :(得分:3)
尝试将循环更改为:
foreach( $daterange as $date1 )
{
$attended = FALSE;
foreach ( $arows as $arow )
{
$date1String = $date1->format('Y-m-d');
$createdOnDate = $arow['created_on'];
$createdDate = new DateTime( $createdOnDate );
$attendanceDateString = $createdDate->format('Y-m-d');
if ( $attendanceDateString == $date1String )
{
$attended = TRUE;
}
}
if ( $attended )
{
echo '<td>True</td>';
}
else
{
echo '<td>False</td>';
}
}
基本上,您似乎为每个日期打印了数据库中每一行的响应,而不是每个日期只打印一个值。
答案 1 :(得分:2)
你应该写这个。这将解决您的问题。
include('../../config/protection.php');
include('../../config/db.php');
date_default_timezone_set("America/Los_Angeles");
//Date formating
$dateFormat = "Y-m-d H:i:s";
$begin = new DateTime( 'first day of this month' );
$end = new DateTime( 'last day of this month' );
$end = $end->modify( '+1 day' );
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
$daterange2 = new DatePeriod($begin, $interval ,$end);
$date = new DateTime();
$date1 = new DateTime();
$dateString = $date1->format($dateFormat);
$timestamp2 = strtotime($dateString);
$month2 = date('j', $timestamp2);
//generating days of the month
$month = date('m', $timestamp2);
$year = date('Y', $timestamp2);
//select username ane id
$sql = "SELECT * from rosters,ranks
inner join user_ranks on user_ranks.rank_id=ranks.id
where rosters.ruser_id = user_ranks.user_id
GROUP BY rosters.rname
ORDER BY ranks.id";
$results = mysqli_query($con, $sql);
if(!$results and $mysqliDebug) {
echo "<p>There was an error in query: $results</p>";
echo $con->error;
}
?>
<table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th class="center">Rank/Name</th>
<th class="center">Promotion Date</th>
<?php
foreach($daterange as $date)
{
echo '<th class="center">'.$date->format("d") . "</th>";
}
?>
</tr>
</thead>
<tbody>
<?php
// $rows = array();
while( $row = mysqli_fetch_assoc($results) )
{
echo "<tr>";
echo "<td>" . $row['rname'] . "</td>";
echo "<td>N/A</td>";
$userid1 = $row['ruser_id'];
// $rows[] = $row;
$attendsql = "SELECT * from attendances where user_id=$userid1";
$attendresults = mysqli_query($con, $attendsql);
$arows = array();
while( $arow = mysqli_fetch_array($attendresults) )
{
$arows[] = $arow;
}
foreach($daterange as $date1)
{
$attended = FALSE;
foreach ($arows as $arow )
{
$date1String = $date1->format('Y-m-d');
$createdOnDate = $arow['created_on'];
$createdDate = new DateTime( $createdOnDate );
$attendanceDateString = $createdDate->format('Y-m-d');
if ( $attendanceDateString == $date1String )
{
$attended = TRUE;
}
}
if ($attended)
{
echo '<td>True</td>';
}
else
{
echo '<td>False</td>';
}
}
echo "</tr>";
}
?>
</tbody>
</table>
这将解决您的问题