如何使用php在一行中显示有限数量的搜索项目..(我必须在下一行中显示单行n中的6项)

时间:2015-03-05 07:40:19

标签: php mysql

 <table class="coll activities" align="left" style="margin-top:15px;">

                    <tr>


<?php

$split = 0;
$result1=$conn->query("select * from subcategory where subcategory.maincat_id='$id'") or die(mysql_error());






while($row1 = $result1->fetch_assoc())

    {               

 $subid=$row1['subcat_id'];

 $sname=$row1['subcat_name'];

  $image=$row1['image'];

 $rowHTML = ' <a href="showresult.php?mid= <?php echo $id;?> &sid= <?php echo $subid;?> " title=" <?php echo $sname;?> ">';


  $rowHTML .= "< ?php echo '<img src="data:image/jpg;base64, '.  base64_encode($image)  .' "/>' ;?> <br />";


   $rowHTML .= "<?php echo $sname;?>";

  $rowHTML .= " </a> <br>";

echo $rowHTML;

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



                               }
                            ?>
                </tr>
                </table>

3 个答案:

答案 0 :(得分:0)

创建一个迭代变量,在循环内的每个步骤中增加该变量并检查% 6 == 1。如果是这样,请开始新行。

$i = 1;

echo '<table>';
while (...) {
    echo $i % 6 == 1 ? '<tr>' : ''; // 1st, 7th, 13th loop, create new table row

    // rest of your code
    $i++;
}
echo '</table>';

答案 1 :(得分:0)

使用此

<?php
                    $result1=$conn->query("select * from subcategory where subcategory.maincat_id='$id'") or die(mysql_error());
                    $i = 1;
                                            while($row1 = $result1->fetch_assoc())
                        {               
                               $subid=$row1['subcat_id'];
                               $sname=$row1['subcat_name'];
                               $image=$row1['image'];

                            if( $i % 6 == 0) {          
                            echo "<table><tr><td>";
                            ?>

                            <div  class="Column" style="background-color:#eee; border:1px solid #ccc" id="sl">
                            <a href="showresult.php?mid=<?php echo $id;?>&sid=<?php echo $subid;?>" title="<?php echo $sname;?>">
                            <center> 
                            <?php echo'<img src="data:image/jpg;base64,' .  base64_encode($image)  . '"/>';?><br />
                            <?php echo $sname;?>
                            </center>
                            </a>
                            </div>

                           <?php echo "</td></tr>"; } else { ?>

                           <div  class="Column" style="background-color:#eee; border:1px solid #ccc" id="sl">
                            <a href="showresult.php?mid=<?php echo $id;?>&sid=<?php echo $subid;?>" title="<?php echo $sname;?>">
                            <center> 
                            <?php echo'<img src="data:image/jpg;base64,' .  base64_encode($image)  . '"/>';?><br />
                            <?php echo $sname;?>
                            </center>
                            </a>
                            </div>




                          <?php $i++;  } echo "</table>" }  ?>

答案 2 :(得分:0)

'

                    <?php
                    $result1=$conn->query("select * from subcategory where subcategory.maincat_id='$id'") or die(mysql_error());
                    while($row1 = $result1->fetch_assoc())
                        {               
                               $subid=$row1['subcat_id'];
                               $sname=$row1['subcat_name'];
                               $image=$row1['image'];



                            ?>

                            <div  class="Column" style="background-color:#eee; border:1px solid #ccc" id="sl">
                            <a href="showresult.php?mid=<?php echo $id;?>&sid=<?php echo $subid;?>" title="<?php echo $sname;?>">
                            <center> 
                            <?php echo'<img src="data:image/jpg;base64,' .  base64_encode($image)  . '"/>';?><br />
                            <?php echo $sname;?>
                            </center>
                            </a>
                            </div>

                           <?php
                        }


                           ?>           

      

  </div>'