I have a system where I want to limit the foreach to a designated user:
My output shows every date Enrolled, I could limit it to only one, but if I do, the single thing shown will be shown to every user as well.
This should only show one Enrolled Since and Not Enrolled Since, but I don't know if I should use an if statement or use a row query. I tried it but it only outputs error "Uncaught Error: Call to a member function result_array() on array in . . ."
(below is the code I tried to use row for, but outputs the said error above.)
<?php
$result_array = $res->result_array();
$results = array();
foreach ($res->result_array() as $row)
{
$results[] = $row;
}
?>
Here is my codes latest code called viewlogs.php
<?php
session_start();
if (!isset($_SESSION['username']))
{
header('location: login.php');
die();
}
?>
<?php
require"config.php";
require"sqlsrv.php";
$id=$_GET['id'];
$sql= 'SELECT * FROM students WHERE id= :id';
$stmt = $db-> prepare($sql);
$stmt->execute([':id' => $id ]);
$data= $stmt->fetch(PDO::FETCH_OBJ);
$query= 'SELECT EmployeeNo, FirstName, MiddleName, LastName, DateHired, ResignationDate FROM TA3.dbo.Employees';
$statement = $conn->query($query);
$statement->execute();
$res=$statement->fetchAll(PDO::FETCH_OBJ);
$q= 'SELECT TimeLogID, EmployeeID, RecordDate, RecordTime, Type, ActualTime FROM TA3.dbo.TimeLogs';
$st = $conn->query($q);
$st->execute();
$re=$st->fetchAll(PDO::FETCH_OBJ);
if (isset ($_POST['fname']) && (isset($_POST['lname']) && (isset($_POST['email']) && (isset($_POST['usn']) && (isset($_POST['schedule']) && (isset($_POST['year']) && (isset($_POST['strand']))))))))
{
$fname = $_POST['fname'];
$mname = $_POST['mname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$usn = $_POST['usn'];
$schedule = $_POST['schedule'];
$year = $_POST['year'];
$strand = $_POST['strand'];
}
if (isset ($_POST['DateHired']) && (isset($_POST['ResignationDate']) && (isset($_POST['EmployeeNo']))))
{
$DateHired = $_POST['DateHired'];
$ResignationDate = $_POST['ResignationDate'];
$EmployeeNo = $_POST['EmployeeNo'];
}
if (isset ($_POST['TimeLogID']) && (isset($_POST['EmployeeID']) && (isset($_POST['RecordDate']) && (isset($_POST['RecordTime']) && (isset($_POST['Type']) && (isset($_POST['ActualTime'])))))))
{
$TimeLogID = $_POST['TimeLogID'];
$EmployeeID = $_POST['EmployeeID'];
$RecordDate = $_POST['RecordDate'];
$RecordTime = $_POST['RecordTime'];
$Type = $_POST['Type'];
$ActualTime = $_POST['ActualTime'];
}
?>
<!DOCTYPE html>
<html>
<head>
<title>View Logs</title>
<link rel="icon" type="image/png" sizes="16x16" href="image/favicon-16x16.png">
<link rel="icon" type="image/png" sizes="32x32" href="image/favicon-32x32.png">
<meta name= "viewport" content="width= device-width, initial-scale=1">
<script defer src="js/fontawesome-all.js"></script>
<link rel="stylesheet" type="text/css" href="css/style.css">
<link rel="stylesheet" type="text/css" href="css/viewlogs.css">
</head>
<body>
<div class="back"><a class="henny" href="students.php"><i class="fas fa-backward"></i> Back </a></div>
<div class="title"> <h1>Student Logs<span class="blink">_</span></h1></div>
<div class="grid">
<div class="student-info">
<label>First name</label>
<input value="<?= $data->fname; ?>" type="text" name="fname" readonly/>
<label>Middle name</label>
<input value="<?= $data->mname; ?>" type="text" name="mname" readonly/>
<label>Last name</label>
<input value="<?= $data->lname; ?>" type="text" name="lname" readonly/>
<label>USN</label>
<input value="<?= $data->usn; ?>" type="text" name="usn" readonly/>
<label>Strand</label>
<input value="<?= $data->strand; ?>" type="text" name="strand" readonly/>
<label>Schedule</label>
<input value="<?= $data->schedule; ?>" type="text" name="schedule" readonly/>
</div>
<div class="device-info">
<?php
$i = 0;
foreach ($res as $outcome): ?>
<label>Enrolled since</label>
<input value="<?= date('Y-m-d', strtotime($outcome->DateHired)); ?>" type="text" name="enrolled" readonly/>
<label>Not Enrolled since</label>
<input value="<?= $outcome->ResignationDate; ?>" type="text" name="notenrolled" readonly/>
<?php endforeach; ?>
</div>
<div class="logs">
<table style="width:100%" class="logs-table">
<tr>
<th style="width:25%">Date</th>
<th style="width:25%">Time</th>
<th style="width:12%">In</th>
<th style="width:12%">Break in </th>
<th style="width:12%">Break out</th>
<th style="width:12%">Out</th>
</tr>
</table>
</div>
</div>
<?php
$r = $db->prepare("SELECT * FROM students WHERE id LIKE :id");
$r->execute(array( ':id'=>'%'.$id. '%'));
if ($data=$r->fetch())
{
?>
<a class="print" target="_blank" href="print.php?id=<?= $data['id']; ?>"> Print <i class="fas fa-print"></i></a>
<?php
}
?>
</body>
</html>
and my sqlsrv.php
<?php
/*
Connection for sqlsrv
*/
try {
$conn = new PDO( 'sqlsrv:server=(localdb)\\v11.0;'
. 'AttachDBFileName=C:\\PROGRAMDATA\\TOUCHLINK TIME RECORDER 3\\TA3.mdf;Database=TA3');
$query = 'SELECT EmployeeNo, FirstName, MiddleName, LastName, DateHired FROM TA3.dbo.Employees ORDER BY EmployeeNo ASC';
$stmt = $conn->query($query);
$stmt->execute();
}
catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
IIUC - simply filter your query by the specific EmployeeNo (or whatever identifier of web user) by passing as a parameter the value returned from posted HTML form. This requires you to change code order where you run your query after assigning $_POST
values.
...
if (isset ($_POST['DateHired']) && (isset($_POST['ResignationDate']) && (isset($_POST['EmployeeNo']))))
{
$DateHired = $_POST['DateHired'];
$ResignationDate = $_POST['ResignationDate'];
$EmployeeNo = $_POST['EmployeeNo'];
}
...
$query= 'SELECT EmployeeNo, FirstName, MiddleName, LastName, DateHired, ResignationDate
FROM TA3.dbo.Employees
WHERE EmployeeNo = ?';
$statement = $conn->prepare($query);
$statement->bind_param(1, $EmployeeNo);
$statement->execute();
$res = $statement->fetchAll(PDO::FETCH_OBJ);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.