我如何将行从mysql数据循环到唯一的divs

问题描述 投票:0回答:2

我正在尝试从mysql Db打印50多个行到具有不同颜色和尺寸的网格视图。这是用于获取行的代码,如何将每行分配给唯一的Anchor标签。

   <?php 
       $product = mysqli_query($conn, "SELECT * FROM table WHERE dui='22' ORDER BY dui";


        while ($data = mysqli_fetch_assoc($product)) { ?>

       $summary = $data ['name'];


        <a href="javascript://" class='wide blue'>
            <i class="icon-home"></i>
            <h2><?php echo  $summary[0]; ?></h2>
        </a>
        <a href="javascript://" class='box redgay'>
            <i class="icon-camera"></i>
            <h2><?php echo  $summary[1]; ?></h2>
        </a>
        <a href="javascript://" class='box lime'>
            <i class="icon-heart"></i>
            <h2><?php echo  $summary[2]; ?></h2>
        </a>
        <a href="javascript://" class='box bluefish'>
            <i class="icon-twitter"></i>
            <h2><?php echo  $summary[3]; ?></h2>
        </a>
        <a href="javascript://" class='box yellow'>
            <i class="icon-map-marker"></i>
            <h2><?php echo  $summary[4]; ?></h2>
        </a>
        <a href="javascript://" class='box redgay'>
            <i class="icon-globe"></i>
            <h2><?php echo  $summary[5]; ?></h2>
        </a>
        <a href="javascript://" class='box orange'>
            <i class="icon-envelope-alt"></i>
            <h2><?php echo  $summary[6]; ?></h2>
        </a>





    <?php } ?> 
php html css mysql
2个回答
-1
投票

仍然不清楚你到底是什么问题。这是可以迭代到50个值的代码,但是由于每个锚标记都使用了差异图标,因此我想必须全部键入。

 <?php 
       $product = mysqli_query($conn, "SELECT * FROM table WHERE dui='22' ORDER BY dui";


        while ($data = mysql_fetch_assoc($product)) { ?>

       $summary = $data ['name'];
       $classArray= array();
       $classArray= ['wide blue','box redgay','box lime','box bluefish']; // used all the custom class here
      for($i=0;$i<sizeof($classArray);;$i++){

       <a href="javascript://" class='<?=$classArray[i] ?'>
            <i class="icon-home"></i>
            <h2><?php echo  $summary[$i]; ?></h2>
        </a>

      }


  <?php } ?>

以上代码只是为了阐明概念。如果没有测试过语法错误,请忽略它。


-3
投票

我想您有一个id列。如果不是,则可以使用该名称作为锚点(如果它是唯一的)。

   <?php 
       $product = mysqli_query($conn, "SELECT * FROM table WHERE dui='22' ORDER BY dui";


        while ($data = mysql_fetch_assoc($product)) { ?>
           <div id="<?php echo $data['id']; ?>">
           $summary = $data ['name'];
           [...]
           </div>
        }
© www.soinside.com 2019 - 2024. All rights reserved.