Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to execute while loop inside single quote string in php

I am a PHP beginner and working on a simple project for learning purpose and at this point am trying to execute a while loop inside another while loop. But the second loop is inside a single quote string. The outputs of both loops are stored in a normal html/bootstrap table cells. At first I was able to get the output of the first loop but the problem came in when I added the second loop inside the string. I have already researched about using quotations in PHP but I still can’t figure out the problem. Below is the code.
Someone help please.

  $output .='<div class="table-responsive">
  <table class="table table-bordered"><form>';
  

  while($row = $result->fetch_assoc()){
      $output .='


     <tr>
     <td width="30%"><label>Price</label></td>
     <td>
     <div class="form-group">
     <input type="text" class="form-control"  placeholder="'.$row['price'].'">
   </div>
   </td>
    </tr>

   <tr>
   <td width="30%"><label>Writer</label></td>
   <td>
   <div class="form-group">
   
     <select class="form-control" id="">
     
       '.while($col = $res->fetch_assoc()){.' 
     <option value="" disabled selected>'.$row['name'].'</option>
     <option value="" >'. $col['username'].'</option>
       '.} .'
     </select>
   
 </div>
 </td>
  </tr>
  

   ';
  }
  $output .="</table></div></form>";
  echo $output;
}

The error am getting is:
Parse error: syntax error, unexpected ‘while’ (T_WHILE) in C:\xampp\htdocs\Writers_SM\select.php on line 61(the second while loop )

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You cannot concatenate loop like you are doing here. What you could do, is store the initial part of the string into a variable, then loop for a second time and use the .= to append a string to your original string, and then finish with the rest of the string. Something like that:

$output .='<div class="table-responsive">
  <table class="table table-bordered"><form>';
  

  while($row = $result->fetch_assoc()){
      $output .='
     <tr>
     <td width="30%"><label>Price</label></td>
     <td>
     <div class="form-group">
     <input type="text" class="form-control"  placeholder="'.$row['price'].'">
   </div>
   </td>
    </tr>

   <tr>
   <td width="30%"><label>Writer</label></td>
   <td>
   <div class="form-group">
   
     <select class="form-control" id="">';


     while($col = $res->fetch_assoc()) {
          $output .= '   <option value="" disabled selected>'.$row['name'].'</option>
     <option value="" >'. $col['username'].'</option>'
      }

    $output .= '</select>
   
 </div>
 </td>
  </tr>
  

   ';
  }
  $output .="</table></div></form>";
  echo $output;
}

But at this point, it gets really confusing. You need to keep in mind that PHP files are just html files with benefits. So you could simply write your HTML normally, and then use php tag inside for your loop and other php related things:

// rest of the php file
// we are closing the php for now
?> 
<div class="table-responsive">
   <table class="table table-bordered">
      <form>
         <!-- The first loop starts here -->
         <?php while($row = $result->fetch_assoc()): ?>
         <tr>
            <td width="30%"><label>Price</label></td>
            <td>
               <div class="form-group">
                  <! -- note here, that <?= ?> is an alias for <?php echo ?> -->
                  <input type="text" class="form-control"  placeholder="<?= $row['price'] ?>">
               </div>
            </td>
         </tr>
         <tr>
            <td width="30%"><label>Writer</label></td>
            <td>
               <div class="form-group">
                  <select class="form-control" id="">
                     <!-- the second loop starts here -->
                     <?php while($col = $res->fetch_assoc()): ?>
                       <option value="" disabled selected><?= $row['name'] ?></option>
                       <option value="" ><?= $col['username'] ?></option>
                     <?php endwhile; ?>
                     <!-- the second loop ends here -->
                  </select>
               </div>
            </td>
         </tr>
         <?php endwhile; ?>
         <!-- the first loop ends here -->
      </form>
   </table>
</div>

Notices how the PHP is not inside the HTML, and we’ve replace the brackets ( { ) with a combinaison of colon and endwhile;.

While this method might be debated, I prefer it to what you had before.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading