I'm trying to have the ID of the current logged user. But I only manage to get the ID of all users
I've got this in my model.php
public function listUsers() {
$this->open();
$stmt = $this->pdo->prepare("SELECT * FROM users");
$stmt->execute();
return $stmt->fetchAll();
}
I have this in my controller: users.php
<?php
class Users {
private $model;
public function loadModel($model) {
$this->model=$model;
}
public function view() {
global $users;
$users=$this->model->listUsers();
require "../app/view/_templates/header.php";
require "../app/view/listUsers.php";
require "../app/view/_templates/footer.php";
}
}
And on my page I have this:
<?php
global $users;
foreach ($users as $user)
echo $user["id"]; ?>
So it echo all users but not only the one logged. I've tried just "echo $user[id]" but doesnt work. $_SESSION["id"] doesnt work either
Any ideas?
Thank you:)
Is the logged in user's ID saved in the $_SESSION["id"]
? You should have a if statement where you filter the user to the logged in ID if you only want the logged in user ID.
Example:
<?PHP
global $users;
foreach($users as $user) {
if($_SESSION["id"] == $user["id"]) {
echo $user["id"]
}
}
Depending on the usage, you could also implement a WHERE statement in your SQL statement where you specify a user ID (example: SELECT * FROM users WHERE id = '$userid'
).
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.