0

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 )

  • `'.while($col = $res->fetch_assoc()){.'` is the issue, break out of php, you dont need to concatenate everything then echo – Lawrence Cherone Dec 25 '21 at 18:24
  • @Lawrence Cherone how do I break out of php? Note that the loops must remain where they are – philip nyankena Dec 25 '21 at 18:28
  • 1
    You're attempting to concatenate `while` to the `$output` string, this: `'.while(`, whereas control structures like that can't be simply concatenated with `.` to strings. Simply close up the string, and append again separately in the inner loop: `'; while($col = $res->fetch_assoc()){ $output .= '....'; }` – Markus AO Dec 25 '21 at 18:32
  • The underlying problem here is the way you're querying your database. You are trying to implement nested loops that iterate over different result sets. This is almost always the wrong way to do things. You haven't shown the details of your database code, but I'd bet that improving your queries will eliminate the nested loops and solve most of this at a stroke. – Tangentially Perpendicular Dec 25 '21 at 20:31

1 Answers1

1

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.

Nicolas
  • 8,077
  • 4
  • 21
  • 51
  • While this would work, I personally get a headache out of both HTML/PHP splicing and long chunks of HTML defined as strings. It's not that complicated to put together a rudimentary templating system and keep HTML in separate files. With anything a notch more complex, will make the code inifnitely more readable and manageable. – Markus AO Dec 25 '21 at 18:42
  • @MarkusAO I agree, but given the fact that OP stated they are PHP beginner, i feel it's the easiest way to get a clean results. I would also prefer a templating engine, such as twig, to keep the HTML seperated from the PHP. – Nicolas Dec 25 '21 at 18:55
  • @Nicolas yes indeed, your answer does fit the OP's bill, so +1 for that. I'm uneasy over _"PHP files are just html files with benefits."_, though! I find that even quick-and-dirty projects, with some processing logic, are mostly PHP, not HTML, inline or otherwise. – Markus AO Dec 25 '21 at 19:00
  • @MarkusAO It might not be the best analogy, since PHP can do a lot, and I mean _a lot!_, more than HTML, but it's easy enough to understand – Nicolas Dec 25 '21 at 19:20