Home > Net >  How to execute while loop inside single quote string in php
How to execute while loop inside single quote string in php

Time:12-26

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 >
  <table ><form>';
  

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


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

   <tr>
   <td width="30%"><label>Writer</label></td>
   <td>
   <div >
   
     <select  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 )

CodePudding user response:

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 >
  <table ><form>';
  

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

   <tr>
   <td width="30%"><label>Writer</label></td>
   <td>
   <div >
   
     <select  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 >
   <table >
      <form>
         <!-- The first loop starts here -->
         <?php while($row = $result->fetch_assoc()): ?>
         <tr>
            <td width="30%"><label>Price</label></td>
            <td>
               <div >
                  <! -- note here, that <?= ?> is an alias for <?php echo ?> -->
                  <input type="text"   placeholder="<?= $row['price'] ?>">
               </div>
            </td>
         </tr>
         <tr>
            <td width="30%"><label>Writer</label></td>
            <td>
               <div >
                  <select  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.

  • Related