I'm learning PHP
in order to develop some modules into my software Dolibarr
and I would like to know How I can loop over my query result and display each row in my array.
This is my script :
/*
* View
*/
$query = "SELECT u.lastname as nom, u.firstname as prenom, u.datelastlogin as lastlogin";
$query.= " FROM ".MAIN_DB_PREFIX."user as u";
$query.= " ORDER BY lastlogin DESC LIMIT 2";
$resql = $db ->query($query);
$num = $db->fetch_row($resql);
print '<table class="noborder" width="100%">';
print '<tr class="liste_titre"><th colspan="2">'.$langs->trans("Affichage des 2 dernières connexions").'</th></tr>';
if (! empty($conf->user->enabled))
{
$LastLogin = '<tr class="oddeven">';
$LastLogin.= '<td><a href="index.php">'.$langs->trans("Utilisateur").'</a></td><td align="right">'.$num[0]." ".$num[1]." ".$num[2].'</td>';
$LastLogin.= "</tr>";
}
print $LastLogin;
print '</table>';
Up to now, I return only the first row from my query. I have never programmed in PHP before and I would like to know How I can display each line from my query into my array ?
I have to change fetch_row
to fetch_array
?
Or, maybe better, split my array in 3 columns (column name, column firstname, column datelastlogin)
Something like this :
I don't really understand how I can do that.
Thank you for your help
EDIT :
I edited my script :
$query = "SELECT u.lastname as nom, u.firstname as prenom, u.datelastlogin as lastlogin";
$query.= " FROM ".MAIN_DB_PREFIX."user as u";
$query.= " ORDER BY lastlogin DESC LIMIT 2";
$resql = $db ->query($query);
//$num = $db->fetch_array($resql);
while($num = $resql->fetch_assoc())
{
$nums[] = $num;
}
print '<table class="noborder" width="100%">';
print '<tr class="liste_titre"><th colspan="2">'.$langs->trans("Dernières connexions").'</th></tr>';
if (! empty($conf->user->enabled))
{
foreach($nums as $num)
{
$LastLogin = '<tr class="oddeven">';
$LastLogin.= '<td><a href="index.php">'.$num['nom'].'</a></td><td>'.$num['prenom'].'</td><td>'.$num['lastlogin'].'</td>';
$LastLogin.= "</tr>";
}
}
print $LastLogin;
print '</table>';
But the array is not well-displayed :