2014-05-08 33 views
0

我有一些問題產生,將來自PHP數據庫顯示圖像成表上的PHP

顯示圖片的表這是我的代碼:

$query = "SELECT * FROM images ORDER BY name ASC "; 

      $result = $db->query($query); 
      $num_result = $result->num_rows; 

      echo "<h1> Images</h1>"; 

      $array = array(); 
      for ($i = 0; $i < $num_result; $i++){ 

       $row = $result->fetch_assoc(); 
       $name = $row['name']; 
       $URL = $row['imageURL']; 


       $array[] = $URL; 
      } 
//this loop is printing the images correctly in order 

foreach ($array as $image){ 
     echo '<img src="'.$image.'"/>'; 
      } 

我試圖做到的是創建一個2列的列表,將打印圖像那裏,像這樣的東西

echo '<table>'; 
    echo ' <tr>'; 
    echo '  <td>image 1</td>'; 
    echo '  <td>image 2</td>'; 
    echo ' </tr>'; 

    echo ' <tr>'; 
    echo '  <td>image 3</td>'; 
    echo '  <td> image 4</td>'; 
    echo ' </tr>'; 

     // and so on if there is more images 

    echo '</table>'; 

任何建議將有助於,謝謝!

+1

檢查了這一點:http://www.php.net/manual/en/language.operators.arithmetic.php#70424此外,一定要使用' htmlspecialchars()'圍繞在HTML上下文中使用的任意數據。否則,您將冒險生成無效的HTML甚至可能發生注入式攻擊。 'echo'';' – Brad

回答

1

我想你想要像

$i=0; 
echo"<table>"; 
echo"<tr>"; 
foreach ($array as $image) 
{ 
    if($i%2==0 && $i>0) 
    echo"</tr><tr>"; 

    echo"<td>"; 
    echo '<img class="coupons" src="'.$image.'"/>'; 
    echo"</td>"; 
    $i++; 
} 
echo"</tr>"; 
echo"</table>"; 
+0

你真棒! ,這正是我所期待的,謝謝! – Programmer4000

+1

我已編輯答案請再次檢查一遍請 –

+0

是的我看到了,你在if語句中增加了另一個,它仍然可以運行,再次感謝 – Programmer4000