A good way to do this is to create a Usuario
class, so you save "Users" into a list and you do not have to create two-dimensional array so you do not mix the returned users.
class Usuario {
private $username;
private $firstname;
private $lastname;
private $email;
public function getUsername() {
return $this->username;
}
public function setUsername($username) {
this->username = $username;
}
public function getFirstname() {
return $this->firstname;
}
public function setFirstname($firstname) {
this->firstname = $firstname;
}
public function getLastname() {
return $this->lastname;
}
public function setLastname($lastname) {
$this->lastname = $lastname;
}
public function getEmail() {
return $this->email;
}
public function setEmail($email) {
$this->email = $email;
}
}
Save this to a different php
file and name it as Usuario.php
, and make the file include in your bank code.
Then, just start an object, retrieve the data from the select and save it in the array:
include_once(Usuarios.php);//altere de acordo com o caminho onde salvou
...
$arr = Array();
while($row = mysqli_fetch_array($sql)){
$user = new Usuario();
$user->setUsername($row['username']);
$user->setFirstname($row['firstname']);
$user->setLastname($row['lastname']);
$user->setEmail($row['email']);
$arr[] = $user;
}
And to display / retrieve users from array
:
foreach($arr as $user){
echo $user->getUsername();
echo $user->getFirstname();
echo$user->getLastname();
echo $user->getEmail();
}
If you do not want or do not know how to work with classes and objects, another solution to not messing up multiple users in a list would be to use a two-dimensional%
$usuarios = Array();
while($row = mysqli_fetch_array($sql)){
$user = Array();
$user['username'] = $row['username'];
$user['firstname'] = $row['firstname'];
$user['lastname'] = $row['lastname'];
$user['email'] = $row['email'];
$usuarios[] = $user;
}
To display a user, just use array
like this:
foreach($usuarios as $user){
echo $user['username'];
echo $user['firstname'];
echo$user['lastname'];
echo $user['email'];
}
References:
link
link