2012-03-10 5 views
0

PHP로 제품 카탈로그를 만들었습니다. 제품을 그리드처럼 표로 만들어서 한 줄에 3 개의 제품이 나란히 놓여 지도록하고 싶습니다. 밑에 등등.php 제품 카탈로그 테이블 레이아웃

<?php # Script 17.5 - browse_prints.php 
// This page displays the available prints (products). 

// Set the page title and include the HTML header: 
$page_title = 'Browse the Films'; 

require_once ('mysqli_connect.php'); 

// Default query for this page: 
$q = "SELECT image_name, genre.genre_id, CONCAT_WS(' ', genre_name) AS genre, film_name, price, platform, description, film_id, image_name FROM genre, film WHERE genre.genre_id = film.genre_id AND film.platform = 'DVD' ORDER BY genre.genre_name ASC, film.film_name ASC "; 

// Are we looking at a particular genre? 
if (isset($_GET['gid']) && is_numeric($_GET['gid'])) { 
$gid = (int) $_GET['gid']; 
if ($gid > 0) { // Overwrite the query: 
    $q = "SELECT image_name, genre.genre_id, CONCAT_WS(' ', genre_name) AS genre, film_name, price, description, film_id, image_name FROM genre, film WHERE genre.genre_id = film.genre_id AND film.genre_id = $gid AND film.platform = 'DVD' ORDER BY film.film_name"; 
} 
} 



// Create the table head: 
echo '<table border="0" width="90%" cellspacing="3" cellpadding="3" align="center"> 
<tr> 
    <td align="left" width="50%"><b>Film Name</b></td> 
    <td align="right" width="50%"><b>Price</b></td> 
</tr><tr>'; 


// Display all the prints, linked to URLs: 
$r = mysqli_query ($dbc, $q); 
while ($row = mysqli_fetch_array ($r, MYSQLI_ASSOC)) { 

// Display each record: 
//image 
$image_name = $row['image_name']; 
echo " 
    <td><a href=\"view_print.php?fid={$row['film_id']}\">{$row['film_name']}<br><br> 
    <img src=uploads/$image_name.jpg><br> 
    <br>&#163;{$row['price']}</td> 
"; 


} // End of while loop. 

echo '</tr></table>'; 
mysqli_close($dbc); 

?> 

답변

1

div는 테이블보다 좋은 해결책입니다. 하지만 그렇게하고 싶다면 카운터를 추가하십시오.

// Create the table head: 
echo '<table border="0" width="90%" cellspacing="3" cellpadding="3" align="center"> 
<tr> 
    <td align="left" width="50%"><b>Film Name</b></td> 
    <td align="right" width="50%"><b>Price</b></td> 
</tr>'; 

$row_count = 0: 
// Display all the prints, linked to URLs: 
$r = mysqli_query ($dbc, $q); 
while ($row = mysqli_fetch_array ($r, MYSQLI_ASSOC)) { 
$row_count++; 
if ($row_count==1) echo "<tr>"; 
// Display each record: 
//image 
$image_name = $row['image_name']; 
echo " 
    <td><a href=\"view_print.php?fid={$row['film_id']}\">{$row['film_name']}<br><br> 
    <img src=uploads/$image_name.jpg><br> 
    <br>&#163;{$row['price']}</td> 
"; 
    if ($row_count==3) { 
     echo "</tr>"; 
     $row_count=0; 
    } 
} // End of while loop. 

if ($row_count>0) echo "</tr>"; 
echo '</table>'; 
2

사용 인덱스 : 나는

다음은 현재 내 코드입니다 .. 제품이 그냥 자동으로 세 가지 제품 후 새 줄을 시작하는 방법을 모르는 수평으로 반복 그래서있어 루프 내부 변수 :

$i = 0; 
while ... 
{ 
    // other code... 

    $i++; 
    if ($i % 3 == 0) { echo '</tr><tr>'; } 
} 
0

는 사용이

<table> 
<?php 
    for($i=0; $i<12; $i++) { 
     if($i%3==0){ 
      echo "<tr>"; 
     } 
     echo "<td>Product - ".$i."<td/>"; 
    } 
?> 
</table> 

Outpu t

Product - 0  Product - 1  Product - 2 
Product - 3  Product - 4  Product - 5 
Product - 6  Product - 7  Product - 8 
Product - 9  Product - 10  Product - 11 
관련 문제