为每个成员插入最后3次访问

时间:2014-05-05 09:17:10

标签: php mysqli

我正在设计一个健身房管理系统,每个成员都有一个存储在数据库中的唯一编号,每个成员在进入健身房时应该键入他的编号以确定他的订阅是否已经结束,我已经设计了该页面,但我需要该页面(为每个成员添加最后3次访问)?我该怎么做 ? 我需要每次覆盖最后3次访问。 ID 1,2和3是会员的最后一次访问

$con = mysqli_connect('localhost', 'root', '', 'test') or die('error' . mysqli_errno($con));

$query1 = mysqli_query($con, "SELECT * FROM checkin WHERE id = 1");
while ($row = mysqli_fetch_array($query1)){
?>
<div id="checkIn">
<table>
<tr>
<td>Check-In</td>
<td><?php echo $row['timeIn']; ?></td>
<td><?php echo $row['dateIn']; } ?></td>
</tr>
<?php
$query2 = mysqli_query($con, "SELECT * FROM checkin WHERE id = 2");
while ($row = mysqli_fetch_array($query2)){
?>
<tr>
<td>Check-In</td>
<td><?php echo $row['timeIn']; ?></td>
<td><?php echo $row['dateIn']; } ?></td>
</tr>
<?php 
$query3 = mysqli_query($con, "SELECT * FROM checkin WHERE id = 3");
while ($row = mysqli_fetch_array($query3)){
?>
<tr>
<td>Check-In</td>
<td><?php echo $row['timeIn']; ?></td>
<td><?php echo $row['dateIn']; } ?></td>
</tr>
</table>

2 个答案:

答案 0 :(得分:1)

首先创建一个查询来获取所有用户而不是循环访问它们。

$userQuery = $con->prepare("SELECT user_id FROM users");
$userQuery->execute();
$users = mysqli_fetch_array(userQuery);

foreach($users as $user) {
     $visitedQuery = $con->prepare("SELECT timeIn, dateIn FROM checkin WHERE user_id = ?  ORDER BY id DESC LIMIT 3");
     $visitedQuery->execute(array($user['user_id']));

     $lastCheckIn = mysqli_fetch_array($visitedQuery);
     foreach($lastCheckIn as $lastCheck) {
         echo $lastCheckIn['timeIn']." ".$lastCheckIn['dateIn'];
     } 
}

答案 1 :(得分:0)

您提到每个用户都有唯一的号码。 例如: - 用户 A 具有 123456 唯一编号。

Header.php 中检查用户是否登录的情况。如: -

if($isset($_SESSION['user_login'])) //you can set your condition here according to your code
{
  // first create a new table where user's visit will save.
  // Say user database table name is USER_LOGS.which have 3 column.
  // ID,User_unique_id,viewpage 
  $query1 = mysqli_query($con, "SELECT Id,viewpage  FROM USER_LOGS WHERE User_unique_id = 12345 ORDER BY Id DESC LIMIT 0"); // that will give you the user's last log. 
 $total_rows = mysqli_num_rows($query1);
 $current_page = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
 if($total_rows > 0)
 {

   $row = mysqli_fetch_array($query1);
   if($row['viewpage'] != $current_page)
   {
     mysqli_query($con,"INSERT INTO USER_LOGS (Id,User_unique_id,viewpage)
     VALUES ('','123456','".$current_page."')");
   }
 }
 else
 {
    mysqli_query($con,"INSERT INTO USER_LOGS (Id,User_unique_id,viewpage)
    VALUES ('','123456','".$current_page."')");
 }
}

以上代码是惰性的,用户登录数据库,现在您可以使用此查询获取最后三次访问。

 mysqli_query($con, "SELECT Id,viewpage  FROM USER_LOGS WHERE User_unique_id = 12345 ORDER BY Id DESC LIMIT 0,3");