Transform mysql query into list

3

I'm doing a query on a database that returns a list of users, which I'm printing to a table.

I would like to turn this query into a list and then insert those users into another table

I make the query as follows:

 $sql= mysqli_query($conect,"select u.username as username,c.fullname from
  mdl_course c left outer join mdl_context cx on c.id = cx.instanceid
  left outer join mdl_role_assignments ra on cx.id = ra.contextid
  left outer join mdl_user u on ra.userid = u.id 
  where ra.roleid in (5) and u.username like '$usuario'
  and cx.contextlevel = 50 and c.id = 5 order by u.username") 
  or die(mysqli_error());

  while($row = mysqli_fetch_array($sql)){  

  }

I would like to save username , firstname , lastname and email .

How can I do this?

    
asked by anonymous 29.01.2016 / 13:26

1 answer

3

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

    
29.01.2016 / 13:59