dongzengzai4567 2016-11-19 11:44
浏览 68
已采纳

随机图像的3x3表 - PHP

enter image description here I'm new here and need some help. I'm doing a web development course and have an assignment I need a hand with.

Basically, I want to send a query that picks 9 random records, and show the result as a 3x3 table.

Here's my code so far:

<div id="productgrid">
<?php
$query = mysqli_query($conn, "SELECT * FROM products
    ORDER BY RAND()
    LIMIT 9");
?>
<h2>Featured Products</h2>


<table>
    <?php
    while($products = mysqli_fetch_array($query)){
    $file = $products['prod_img'];
    $pid = $products['prod_id'];
    $product_price = $products['prod_price'];
    $image_id = $products['prod_id'];
    $desc = $products['prod_desc'];
    ?>
    <tr>
        <div class="bigred">
            <td class="bigred">
                <?php echo '<b>'. "$product_price".'</b>'; ?>
            </td>
        </div>
    </tr>
    <tr>
        <td>
            <img src="<?php echo $file ?>" height = "150px"     width="150px"/><br />
        </td>
        <div class="smallblack"
    </tr>
    <tr>
        <td class = "smallblack">
        <?php echo '<b>' . "$desc".'</b>'; ?>
        </td>
    </tr>
</div>
<?php } ?>

I can get it to generate the 9 random images but it puts them all directly under each other in one long column. Is there a way I can get them to display in 3 rows of 3?

I apologise if this is a dumb question, but I'm only starting out and appreciate the help.

Thank you :)

Pic attached as sample

  • 写回答

2条回答 默认 最新

  • duanjie5570 2016-11-19 11:56
    关注
    <?php
    // A counter which is incremented by one for each product row
    // in the loop below.
    $i = 0;
    
    echo "<table>
    ";
    echo "<tr>
    ";
    
    while ($product = mysqli_fetch_array($query)) {
      // Re-open HTML row, if $i is divisible by 3
      if ($i++ % 3 == 0) {
        echo "</tr><tr>
    ";
      }
    
      // Escape the `src` attribute value as appropriate, according to the
      // logic of your app. This is just a sample.
      $img_src = htmlspecialchars($product['prod_img']);
    
      echo <<<HTML
      <td>
        <div><img src="{$img_src}"/></div>
        <div>{$product['prod_desc']}</div>
        <!-- Put the rest of the fields here -->
      </td>
    HTML;
    }
    
    // Add cells for the rest of the columns (if any)
    while ($i++ % 3 != 0) {
      echo "<td></td>
    ";
    }
    
    echo "</tr>
    ";
    echo "</table>
    ";
    ?>
    

    P.S.: I recommend using a template engine such as Smarty, or Twig. With the help of a template engine you can make the markup much more readable and maintainable.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用