Reputation: 31
I done my SQL request like this in my model.php :
public function getUserForAdmin() {
$arrayUser = $this->bdd->getArray('SELECT prenom AS prenomUFA, nom AS nomUFA, email AS emailUFA, cp AS cpUFA FROM user');
return $arrayUser['prenomUFA'];
}
In my index.php I call it like this
$userForAdmin = $user->getUserForAdmin();
echo $userForAdmin['prenomUFA'];
With this I have just the first letter of my prenom fields result...
Output of my array :
Array (
[0] => Array ( [prenom] => Coline [nom] => [email] => hj [userForAdmin] => 69570 )
[1] => Array ( [prenom] => Coline [nom] => [email] => [email protected] [userForAdmin] => 69570 )
[2] => Array ( [prenom] => Coline [nom] => [email] => [email protected] [userForAdmin] => 69570 )
[3] => Array ( [prenom] => Coline [nom] => [email] => hjkjk [userForAdmin] => 69570 )
[4] => Array ( [prenom] => Coline [nom] => [email] => [email protected] [userForAdmin] => )
[5] => Array ( [prenom] => Coline [nom] => [email] => hjkhjk [userForAdmin] => 69570 )
[6] => Array ( [prenom] => Coline [nom] => jkljl [email] => [email protected] [userForAdmin] => )
[7] => Array ( [prenom] => Coline [nom] => fgdfg [email] => [email protected] [userForAdmin] => )
[8] => Array ( [prenom] => Coline [nom] => ghjg [email] => [email protected] [userForAdmin] => ghj )
[9] => Array ( [prenom] => ghj [nom] => ghjghj [email] => [email protected] [userForAdmin] => )
[10] => Array ( [prenom] => hjkhk [nom] => hjkhjk [email] => [email protected] [userForAdmin] => )
[11] => Array ( [prenom] => cvbcb [nom] => cvbcvb [email] => [email protected] [userForAdmin] => )
[12] => Array ( [prenom] => jhk [nom] => hjkhjk [email] => [email protected] [userForAdmin] => )
[13] => Array ( [prenom] => Coline [nom] => [email] => cghjghj [userForAdmin] => 45645 )
[14] => Array ( [prenom] => Coline [nom] => [email] => ghj [userForAdmin] => 44545 )
)
Upvotes: 0
Views: 110
Reputation: 743
public function getUserForAdmin() {
$arrayUser = $this->bdd->getArray('SELECT prenom AS prenomUFA, nom AS nomUFA, email AS emailUFA, cp AS cpUFA FROM user');
return $arrayUser;
}
In index.php call it like this -
$userForAdmin = $user->getUserForAdmin();
foreach($userForAdmin as $rows)
{
echo '<p>'.$rows['prenomUFA'].'</p><br>';
echo '<p>'.$rows['nomUFA'].'</p><br>';
echo '<p>'.$rows['emailUFA'].'</p><br>';
echo '<p>'.$rows['cpUFA'].'</p><br>';
}
Above code outputs all the values.
Upvotes: 1
Reputation: 34416
In order for to return the entire array from the function do this -
public function getUserForAdmin() {
$arrayUser = $this->bdd->getArray('SELECT prenom, nom, email, cp AS userForAdmin FROM user');
return $arrayUser;
}
Call it like so -
$userForAdmin = $user->getUserForAdmin();
print_r($userForAdmin); // testing to see what is in the array
It appears that getArray()
returns an array of arrays and that your AS
alias' are returned properly. To access an individual item you have to do it this way:
echo $userForAdmin[0]['userForAdmin']; // note the specification used in multidimensional arrays
echo $userForAdmin[0]['prenom']; // has no 'AS' alias
echo $userForAdmin[0]['nom'];
echo $userForAdmin[0]['email'];
You may want to look at getArray()
so that it returns something more convenient to work with.
Upvotes: 2