Mimi
Mimi

Reputation: 125

echo css classes in a while loop

I have a button inside a while loop, which I would like to use a bootstrap style on. I actually thought I just could replace my current HTML button code, but I get the error: syntax error, unexpected 'form' (T_STRING), expecting ',' or ';'. Is there a smart way to do that somehow?

while($row = $res->fetch_assoc()) {
        echo "Id: " . $row["id"]. "<br>" . 
             "<button>Read More</button><br><br>";         
}

This is the button I would like to set in my while loop:

<div class="form-group">
<label class="col-md-4 control-label"></label>
   <div class="col-md-4">
      <button type="submit" class="btn btn-warning" >Send <span class="glyphicon glyphicon-send"></span></button>
   </div>
</div>

I tried to set the bootstrap button code in my while loop like this:

while($row = $res->fetch_assoc()) {
            echo "Id: " . $row["id"]. "<br>" . 
                 "
                  <div class="form-group">
                  <label class="col-md-4 control-label"></label>
                     <div class="col-md-4">
                        <button type="submit" class="btn btn-warning">Send   <span class="glyphicon glyphicon-send"></span></button>
                     </div>
                  </div>
                 ";
}

Upvotes: 0

Views: 325

Answers (2)

Marios Nikolaou
Marios Nikolaou

Reputation: 1336

You can use a variable to add all the text and then print it.

$output = "";
while($row = $res->fetch_assoc()) {
            $output  = "Id: " . $row["id"] . "<br>";
            $output .="<div class='form-group'>";
            $output .="<label class='col-md-4 control-label'></label>";
            $output .="<div class='col-md-4'>";
            $output .="<button type='submit' class='btn btn-warning'>Send";   
            $output .="<span class='glyphicon glyphicon-send'></span></button>";
            $output .="</div></div>";
            echo $output;
}

Upvotes: 0

Simos Fasouliotis
Simos Fasouliotis

Reputation: 1390

You need to escape the " that you put inside the echo.

while($row = $res->fetch_assoc()) {
            echo "Id: " . $row["id"]. "<br>" . 
                 "
                  <div class=\"form-group\">
                  <label class=\"col-md-4 control-label\"></label>
                     <div class=\"col-md-4\">
                        <button type=\"submit\" class=\"btn btn-warning\">Send   <span class=\"glyphicon glyphicon-send\"></span></button>
                     </div>
                  </div>
                 ";
}

Upvotes: 2

Related Questions