0

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>
                 ";
}
3
  • ah okay I just found out. Maybe I am incorrct, but if I set ' ' around my html button I get printed the bootstrap button. Is that correct? Commented Apr 18, 2017 at 17:07
  • As my answer below, you either need to properly escape " or you can use ' as starting and ending points. Difference from ' to " is that " can parse variables if needed, so it's better to use. Commented Apr 18, 2017 at 17:09
  • Since you are using double quotes in your php, you will either need to escape your html quotes in your output (like so \") OR you can choose to use single quotes in either your html or in your php, other wise you will severely confuse the parser, and start opening and closing strings everywhere ;) Commented Apr 18, 2017 at 17:17

2 Answers 2

2

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>
                 ";
}
Sign up to request clarification or add additional context in comments.

Comments

0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.