PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, December 14, 2022

[FIXED] How to add a loop inside a variable php

 December 14, 2022     loops, oop, php, syntax     No comments   

Issue

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 class="fa-solid fa-pen-to-square"></i></a></td>';
      echo  '<td><a href=""><i class="fa-solid fa-trash"></i></a></td>';
    };

Solution

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 class="fa-solid fa-pen-to-square"></i></a></td>';
      $doctor .='<td><a href=""><i class="fa-solid fa-trash"></i></a></td>';
    }; 


Answered By - Existed Mocha
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing