2013-04-27 65 views
0

我已經使用了使用類似的代碼迴路和here在PHP嘗試:我需要用php/javascript循環一段HTML代碼,建議嗎?

$options = ''; 

for($Year = date("Y"); $Year <= date("Y") + 5; $Year++) 
{ $options .= "<option>$Year</option>\n"; } 

$Select = <<<QQxQQ 
<select> 
$options 
</select> 
QQxQQ; 


print "$Select"; 

,但沒有運氣...

編輯 這些例子都是偉大的,謝謝你們。 這就是我想要重複

<li><a href="#"><span>$looped</span></a></li> 

$循環是牽強的mysql列的值。正如你可能看到的,我試圖遍歷一個列表x的元素(其中x = sql查詢的行數)。

我以爲試圖把結果放入一個數組然後循環訪問數組,但是我仍然無法讓HTML代碼相應地通過解析器而不被視爲字符串。

+0

是否有必要的,你想用定界符? – 2013-04-27 10:38:54

+2

解釋器在你的heredoc聲明後抱怨空白,否則它沒事 - [測試](http://codepad.org/sAkIjWPV) – Emissary 2013-04-27 10:40:00

+0

根據你發佈的更新,試試這個然後[回聲mysqli查詢](http: //stackoverflow.com/questions/8806998/echoing-mysqli-queries) – Tigger 2013-04-27 11:29:32

回答

4

這個怎麼樣?

<select> 
<?php 
    for($Year = date("Y"); $Year <= date("Y") + 5; $Year++) { 
     echo "<option>".$Year."</option>"; 
    } 
?> 
</select> 
0

你可以嘗試這樣的事:

<?php 

$year = date('Y'); 
$end = ($year + 5); 
do { 
    $option .= '<option>'.$year.'</option>'; 
} while (++$year <= $end); 

echo '<select>'.$option.'</select>'; 

?> 
0

here文檔有自己的位置,但國際海事組織他們是被高估,並在錯誤的情況下經常使用。

試試這個:

$year = date("Y"); 
for($Year = $year; $Year <= ($year+5); $Year++) { 
    $options .= "<option value=\"$Year\">$Year</option>\n"; 
} 

echo "<select name='years' id='years'>"; 
echo $options; 
echo "</select>"; 
0
<select> 
<?php 
for($Year = date("Y"); $Year <= date("Y") + 5; $Year++) 
{ echo "<option>$Year</option>\n"; } 
?> 
</select>