【问题标题】:Table layout for a php product cataloguephp 产品目录的表格布局
【发布时间】:2017-12-19 07:14:16
【问题描述】:

我在 PHP 中创建了一个产品目录,我希望产品以表格的形式出现,就像在网格中一样,所以它在一行中并排有 3 个产品,然后在下面继续,依此类推。我知道了,所以产品水平重复,我只是不知道如何让它在三个产品之后自动开始一个新的线..

这是我目前的代码:

<?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);

?>

【问题讨论】:

    标签: php html-table shopping-cart


    【解决方案1】:

    在循环内使用索引变量:

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

    【讨论】:

      【解决方案2】:

      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>';
      

      【讨论】:

        【解决方案3】:

        使用它

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

        输出

        Product - 0     Product - 1     Product - 2 
        Product - 3     Product - 4     Product - 5 
        Product - 6     Product - 7     Product - 8 
        Product - 9     Product - 10        Product - 11
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-04-20
          • 1970-01-01
          • 2012-03-22
          • 2013-03-06
          相关资源
          最近更新 更多