i'm designing a Gym Administration System, each member has a unique number stored in a database, each member should type his number when he entered the gym to make sure if his subscription is ended or not, i already design that page, but i need that page to ( add the last 3 visits for each member ) ? how can i do it ? i need to overwrite the last 3 visits everytime . ID's 1,2 and 3 are the last visit for member
$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>
First create a query to fetch all users than loop through them.
$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'];
}
}
You mentioned that each user have unique number. For example:- User A have 123456 unique number.
In Header.php check the condition that user login or not. like:-
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."')");
}
}
The above code is for inert the user log into the database now you can fetch the last three visit by using this query.
mysqli_query($con, "SELECT Id,viewpage FROM USER_LOGS WHERE User_unique_id = 12345 ORDER BY Id DESC LIMIT 0,3");