2017-04-18 21 views
0

我在while循環中有一個按鈕,我想在其上使用引導樣式。我實際上認爲我可以替換當前的HTML按鈕代碼,但是出現錯誤:syntax error, unexpected 'form' (T_STRING), expecting ',' or ';'。有什麼聰明的方法來做到這一點?在while循環中echo css類

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

這是按鈕,我想在我的while循環設置:

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

我試圖設置引導按鈕的代碼在我的while循環是這樣的:

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> 
       "; 
} 
+0

啊好吧我剛剛發現。也許我不熟悉,但如果我在我的html按鈕周圍設置'''',我會打印引導按鈕。那是對的嗎? – Mimi

+0

正如我在下面的回答,你或者需要正確地轉義「或者你可以使用'作爲起點和終點,與'to'的不同之處在於」如果需要可以解析變量,所以最好使用 –

+0

由於你使用雙引號在你的PHP中,你將需要在你的輸出中轉義你的html引號(比如'\「'),或者你可以選擇在html或php中使用單引號,否則你會嚴重混淆解析器,並隨處開始打開和關閉字符串;) – nomistic

回答

1

你需要逃避「你把回聲放在裏面。」

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

您可以使用一個變量來添加所有文本,然後打印它。

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

不要混合PHP和HTML/CSS。您可以執行以下操作:

<?php 
// PHP code here. 
$output = ""; 
while($row = $res->fetch_assoc()): 
?> 

Id: <?php echo $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> 

<?php 
// More PHP code. 
endwhile; 
?> 

現在,您可以更快地對HTML和CSS應用任何更改。保持你的代碼整潔。