简体   繁体   English

无论查询多少,每行显示4个结果

[英]Display 4 result per row whatever number of queries

echo '<table class="table-condensed"  style="width:700px; margin-left:25%;margin-top:-270px;" id="table1">';

$i = 0;

while($row = mysqli_fetch_array($query)) { 
    $image = $row['name']; 
    $product_id = $row['product_id'];
    $withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $image);

    if($i == 0){
        echo '<tr>';
    }

    echo '<td style=text-align:center; font-size:12px;>'.'<img src="admin/image/'.$image.'" style="height:100px; width:auto;"/>'.'<br>';

    echo $row['brand'] ."&nbsp;". 
         $row['model'] ."<br>".
         $row['color'] ."<br>".
         $row['storage'] ."<br>" ,"<br>".
         number_format($row['price']) ."&nbsp;" ."Php". "<br>";

    echo '<a href="mobile.php?product_id=<?php echo $product_id?>"><input type="submit" value="View this item" class="btn-primary"></a>'.'</td>';   

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

    $i++;   

}   

echo '</table>';

I have 9 queries now on my database, and it shows 我的数据库中现在有9个查询,它显示

img1 img2 img3 img4 img5 img6
Img7 img8 img9   //which is wrong

I want it: 我要它:

Img1 img2 img3 img4
img5 img6 img7 img8
img9 img10 img11 img12

That every time I will add on my query the table row will not be change to 4 image per row. 每次我添加查询时,表格行都不会更改为每行4张图片。 Thankyou you in advance 预先谢谢你

First of all learn Math. 首先学习数学。 You are starting to count at 您开始指望

$i = 0 

and in your condition is 而你的情况是

if ($i > 4)

which is true with exactly 6th option. 对于第6个选项,这是正确的。 Your sequence is: 您的顺序是:

0 1 2 3 4 5

Also, you are incrementing your $i value after you reset it, so next loop will start with $i == 1 next 另外,您在重置$ i值后将其递增,因此下一个循环将从$ i == 1开始

<tr> 

tag will not be printed, because it checks for zero. 标签不会打印,因为它会检查零。

Try this, should work : 试试这个,应该工作:

  echo '<table class="table-condensed"  
  style="width:700px; margin-left:25%;margin-top:-270px;" id="table1">';

  $i = 0;


while($row = mysqli_fetch_array($query)) { 
   $image = $row['name']; 
   $product_id = $row['product_id'];
   $withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $image);

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

echo '<td style=text-align:center; font-size:12px;>'.'<img src="admin/image/'.$image.'" style="height:100px; width:auto;"/>'.'<br>';

echo $row['brand'] ."&nbsp;". 
     $row['model'] ."<br>".
     $row['color'] ."<br>".
     $row['storage'] ."<br>" ,"<br>".
     number_format($row['price']) ."&nbsp;" ."Php". "<br>";

echo '<a href="mobile.php?product_id=<?php echo $product_id?>"><input type="submit" value="View this item" class="btn-primary"></a>'.'</td>';   

  if ($i % 4 == 2) { echo '</tr>'; }

   $i++;   

}   
    if ($i % 4 != 0) { echo '</tr>'; }
  echo '</table>';  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM