I have to use a foreach loop inside a variable in php but Im not sure of the right syntax. This is what Ive tried but it shows an error(Parse error: syntax error, unexpected 'foreach' (T_FOREACH))
$doctor = foreach($doctor->find_all() as $d){
echo '<td>'.$d->getName().'</td>';
echo '<td>'.$d->getEmail().'</td>';
echo '<td>'.$d->getPhone().'</td>';
echo '<td>'.$d->getGender().'</td>';
echo '<td>'.$d->getSpecialist().'</td>';
echo '<td><a href="../doctor/updatedoc.php"><i ></i></a></td>';
echo '<td><a href=""><i ></i></a></td>';
};
CodePudding user response:
You can't do $doctor = foreach(...
That's not correct PHP syntax.
It's not completely clear to me what you're trying to achieve (aside from outputting some HTML), but start by removing the variable assignment:
foreach($doctor->find_all() as $d) {
echo '<td>'.$d->getName().'</td>';
echo '<td>'.$d->getEmail().'</td>';
echo '<td>'.$d->getPhone().'</td>';
echo '<td>'.$d->getGender().'</td>';
echo '<td>'.$d->getSpecialist().'</td>';
echo '<td><a href="../doctor/updatedoc.php"><i ></i></a></td>';
echo '<td><a href=""><i ></i></a></td>';
};
CodePudding user response:
You cannot assign loop on variable or with the echo instead you can direct save the data to variable inside the loop
$doctor = "";
foreach($doctor->find_all() as $d){
$doctor .='<td>'.$d->getName().'</td>';
$doctor .='<td>'.$d->getEmail().'</td>';
$doctor .='<td>'.$d->getPhone().'</td>';
$doctor .='<td>'.$d->getGender().'</td>';
$doctor .='<td>'.$d->getSpecialist().'</td>';
$doctor .='<td><a href="../doctor/updatedoc.php"><i ></i></a></td>';
$doctor .='<td><a href=""><i ></i></a></td>';
};
CodePudding user response:
use anonymous function (lambda, etc.) to assign into variable