2013-10-27 3 views
1

데이터베이스에서 이미지 테이블을 만드는 PHP 스크립트를 만들려고합니다. 나는 6x (db가 얼마나 많은지) 테이블을 만들기 위해 while 루프를 적절히 중첩시키는 법을 알아내는 데 어려움을 겪고있다. 나는 개념을 이해하지만 PHP에 익숙하지 않고 여기에서 나의 머리를 감쌀 수 없다.PHP while 루프를 사용하여 이미지 테이블 만들기

<?php 

mysql_connect("localhost","root",""); 
mysql_select_db("images"); 

$res=mysql_query("SELECT * FROM table1"); 
echo "<table>"; 
while($row=mysql_fetch_array($res)){ 

echo "<tr>"; 
echo "<td>";?> 

<img src="<?php echo $row["images1"]; ?>" height="150" width="150"> 

<?php echo "</td>"; 
echo "</tr>";} 
echo "</table>"; 
?> 
+0

mysql_ * 함수는 더 이상 사용되지 않습니다 ([red box] (http://php.net/mysql_query) 참조). 새 코드에서 사용하지 마십시오. –

답변

1

당신은 당신이 당신이 목록에서 여섯 번째 항목에 있는지 테스트하는 if($count % 6 == 0)를 사용하여 처리 얼마나 많은 이미지의 수를 유지하는 경우. 따라서 루프는 다음과 같이 보일 것입니다 :

$c = 0; //our count 
while($row = mysql_fetch_array($res)){ 
    if(($count % 6) == 0){ // will return true when count is divisible by 6 
    echo "<tr>"; 
    } 
    echo "<td>"; 
    ?> 
    <img src="<?php echo $row["images1"]; ?>" height="150" width="150"> 
    <?php 
    echo "</td>"; 
    $c++; // Note we are incrementing the count before the check to close the row 
    if(($count % 6) == 0){ 
    echo "</tr>"; 
    } 
} 
관련 문제