2016-11-19 41 views
0

enter image description here 我是新来的,需要一些帮助。我正在做一个网络开发课程,并且有一份我需要帮助的任务。3x3表随机图像 - PHP

基本上,我想发送一个查询,选择9个随机记录,并显示结果为3x3表。

这里是我到目前为止的代码:

<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 } ?> 

我能得到它来生成随机9倍的图像,但它在一个长柱把他们所有的直属对方。有没有一种方法可以让他们显示3行3列?

我很抱歉,如果这是一个愚蠢的问题,但我只是开始和欣赏的帮助。

谢谢:)

产品图附于样品

回答

0
<?php 
// A counter which is incremented by one for each product row 
// in the loop below. 
$i = 0; 

echo "<table>\n"; 
echo "<tr>\n"; 

while ($product = mysqli_fetch_array($query)) { 
    // Re-open HTML row, if $i is divisible by 3 
    if ($i++ % 3 == 0) { 
    echo "</tr><tr>\n"; 
    } 

    // 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>\n"; 
} 

echo "</tr>\n"; 
echo "</table>\n"; 
?> 

P.S .:我推荐使用模板引擎,如Smarty或Twig。借助模板引擎,您可以使标记更具可读性和可维护性。

+0

我喜欢这样的外观,只需要遵循它lol –

+0

我设法让这个工作,除了它选择相同的图像,并重复它 - 好像它忘记了ORDER BY RAND()?有任何想法吗? –

+0

@NevilleDean,我测试了我的本地机器上的代码。它按预期工作。您应该对新代码有问题。在没有看到代码的情况下,我只能使用'var_dump($ product);'_'在loop_中建议进行调试。 –

0

你应该改变你的表出来并投入使用3个<td>标签,而不是只有一个,像这样......

<table> 
<tr> 
<?php 
    $counter = 0; // Needs to be outside of the loop... 
    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']; 
?> 

    <td class="bigred"> 
     <?php echo '<b>'. "$product_price".'</b>'; ?> 
     <img src="<?php echo $file ?>" height = "150px"  width="150px"/><br /> 
     <?php echo '<b>' . "$desc".'</b>'; ?>  
    </td> 

<?php 
    // Check to see if you have looped through three times and close the <tr> tag. 
    if($counter % 3 === 0) { 
     echo '</tr><tr>'; 
    } 
    $counter++ 
?> 
</table> 

如果有超过9个图像,那么它将继续创建表格行和单元格,并且需要更多逻辑来处理该图像,但是,这应该适用于9个图像。

更好的处理方法是使用<div>标签和浮标,但由于您已经在使用表格,因此我将其格式化为如此。

+0

谢谢。数据库中会有9个以上的图像 - 可能有数千个图像 - 我希望它能做的就是从数据库中选取9张随机图像并展示它们。我会放弃这一点。 –