2011-10-20 76 views
0

我想把我的mysql記錄取到一個表格中,每個單元格只能有一個圖像。使用PHP獲取mysql記錄到一個html表格

問題是,照片​​複製5次,根據我需要填寫每一行的數量。

這裏是我的代碼:

<table width="%" border="1" bordercolor="#F7F7F7" align="center" cellspacing="10" cellpadding="5"> 

     <tr> 

     <? 
     for($i=0;$i<=5;$i++) 
     { 

     echo "<td width=125 height=125><img width=125 height=125 src=images/".$info['photo'] ."></td>"; 
     } }?> 
     </tr> 
    </table> 

我怎樣才能糾正這個代碼,以使每張照片取一次爲每個小區?

*****編輯*****

我把同時在表內,並取現是好的,但它仍然在同一行中取的,我需要做什麼來阻止直到我有5個單元格在同一行中,然後繼續獲取新行。

<table width="%" border="1" bordercolor="#F7F7F7" align="center" cellspacing="10" cellpadding="5"> 

     <tr> 


     <? 

     while($info = mysql_fetch_array($data )) 
{ 
?> 

     <td width=125 height=125 ><img width=125 height=125 src=images/<? echo ($info['photo']); ?>></td> 


    <? } ?> 
     </tr> 
+0

重複的[如何用數據庫中的值創建php 2列表?](http://stackoverflow.com/questions/7299913/how-to-create-php-2-column-table-with-values - 從數據庫) –

+0

可以幫助我這個:) –

回答

1

你不會改變循環內的$info['photo']。這就是爲什麼你五次迴應同一張照片。

根據你的代碼的樣子,你可以修改你這樣的代碼:

$result = mysql_query($your_query); 

while ($row = mysql_fetch_array($result, MYSQL_NUM)) { 
    echo("<td width=\"125\" height=\"125\"><img width=\"125\" height=\"125\" src=\" images/". $row["photo"] ."></td>"); 
} 
+0

它沒有工作! ,所有圖像不可用 –

1

這似乎並沒有將所有的相關的代碼來回答這個問題。你在'for'循環中循環6次,並且所有6次都獲取相同的圖片'$ info ['photo']'。

你想在每次循環時轉到下一個MySQL查詢。所以你的循環裏面有一個

$info = mysql_fetch_row($result) 

1

嘗試在echo for循環內添加$ info = mysql_fetch_row()。還請檢查$ info!= false以確保您有足夠的圖像(5項)來填充單元格。

+0

我試過,但它沒有工作,所有圖像仍然不可用 –

+0

也許,$信息= mysql_fetch_row($數據)將工作?對於你的編輯 - 聲明$ i變量,分配0和內部循環添加代碼'if(++ $ i> = 5){echo「」; $ i = 0; }' – Arny

相關問題