php
2015-02-06 17 views 0 likes 
0

我想讀取數據庫的5個屬性。這5個屬性的名稱分別爲post_image_1,post_image_2,post_image_3,post_image_4post_image_5。現在我想在我的頁面上顯示5個圖像,並帶有for循環。帶有變量的PHP閱讀數據庫

這裏是循環:

for($i = 1; $i <= 5; $i++){ 
    echo "<img src='image/$row[post_image_$i].png' height='250px' width='250px'>"; 
} 

現在,我得到一個錯誤:

Parse error: syntax error, unexpected '$i' (T_VARIABLE), expecting ']' in 

我希望這是足夠的信息來幫助我。 :P

+1

你試圖在你的數組索引鍵儘可能PHP是混合在一起一個常量和變量名關心。 – Crackertastic 2015-02-06 21:18:16

回答

4

由於您使用的數組,這樣做:

for($i = 1; $i <= 5; $i++){ 
    echo '<img src="image/'.$row['post_image_'.$i].'png" height="250px" width="250px">'; 
} 
+0

以秒擊敗我...... :) – 2015-02-06 21:20:29

+1

'A'努力;) - 事實上,如果OP抓住這一點,你可以採取答案的點 - 上漲已經讓我回來超過2000 - – 2015-02-06 21:21:37

+0

感謝您的答案!它的完美 – Skeptar 2015-02-06 21:47:27

2

試試這個,使用字符串連接,它在陣列中能夠在正確的領域。我假設$行已聲明與包含關鍵字「post_image_1」,「post_image_2」 ......

for($i = 1; $i <= 5; $i++){ 
    echo "<img src='image/" . $row['post_image_' . $i] . ".png' height='250px' width='250px'>"; 
} 
+0

感謝您的答案!這是完美的 :) – Skeptar 2015-02-06 21:47:51

相關問題