如何使用php while循环为div提供不同的颜色?

时间:2017-01-12 09:56:30

标签: php loops colors while-loop

<div class="vc_row wpb_row vc_inner vc_row-fluid">
    <?php
        $facilitiessql="select * from ".TABLE_ADM_FACILITIES;
        $facilitiesrun=mysql_query($facilitiessql);
        while($facilitiesfetch=mysql_fetch_array($facilitiesrun))
        {
    ?>
    <div class="wpb_column vc_column_container vc_col-sm-3">
      <div class="vc_column-inner vc_custom_1458192848305">
            <div class="wpb_wrapper">
              <div class='dt-sc-icon-box type10'>
                <div class="icon-wrapper">  
                    <img width="215" height="215" src="upload/facilities/<?php echo $facilitiesfetch['bimg'];?>" class="attachment-full" alt="service-image-1" sizes="(max-width: 215px) 100vw, 215px"/>
                </div>
                <div class="icon-content">
                    <h4><?php echo $facilitiesfetch['title']; ?></h4>
                    <?php echo $facilitiesfetch['content']; ?>
                </div>
              </div>
            </div>
       </div>
     </div>
    <?php } ?>
</div>

如何使用while循环赋予这些div不同的颜色,它们具有相同的类。

2 个答案:

答案 0 :(得分:0)

如果想为每个div提供不同的颜色,可能会低于解决方案帮助你。

<div class="vc_row wpb_row vc_inner vc_row-fluid">
<?php

    $arrColorCode = array('#7FFFD4','#F0FFFF','#F5F5DC','#F5F5DC','#A52A2A', '#DEB887');

    $facilitiessql="select * from ".TABLE_ADM_FACILITIES;
    $facilitiesrun=mysql_query($facilitiessql);
    while($facilitiesfetch=mysql_fetch_array($facilitiesrun))
    {
      $i = 0;
      $styleCode = '';
      if(isset($arrColorCode[$i]))
        $styleCode = "style='background-color:$arrColorCode[$i]'";
      ?>
      <div class="wpb_column vc_column_container vc_col-sm-3" <?php echo $styleCode; ?>>
        <?php 
          $i++;
          if(isset($arrColorCode[$i]))
            $styleCode = "style='background-color:$arrColorCode[$i]'";
        ?>
        <div class="vc_column-inner vc_custom_1458192848305" <?php echo $styleCode; ?>>
              <?php 
                $i++;
                if(isset($arrColorCode[$i]))
                  $styleCode = "style='background-color:$arrColorCode[$i]'";
              ?>
              <div class="wpb_wrapper" <?php echo $styleCode; ?>>
                <?php 
                  $i++;
                  if(isset($arrColorCode[$i]))
                    $styleCode = "style='background-color:$arrColorCode[$i]'";
                ?>
                <div class='dt-sc-icon-box type10' <?php echo $styleCode; ?>>
                  <?php 
                    $i++;
                    if(isset($arrColorCode[$i]))
                      $styleCode = "style='background-color:$arrColorCode[$i]'";
                  ?>
                  <div class="icon-wrapper" <?php echo $styleCode; ?>>  
                      <img width="215" height="215" src="upload/facilities/<?php echo $facilitiesfetch['bimg'];?>" class="attachment-full" alt="service-image-1" sizes="(max-width: 215px) 100vw, 215px"/>
                  </div>
                  <?php 
                    $i++;
                    if(isset($arrColorCode[$i]))
                      $styleCode = "style='background-color:$arrColorCode[$i]'";
                  ?>
                  <div class="icon-content" <?php echo $styleCode; ?>>
                      <h4><?php echo $facilitiesfetch['title']; ?></h4>
                      <?php echo $facilitiesfetch['content']; ?>
                  </div>
                </div>
              </div>
         </div>
       </div>
      <?php 
    } 
?>

答案 1 :(得分:0)

您可以使用所需的颜色创建颜色数组$colors。在while迭代之后,您可以从该数组中获取一个颜色值并应用于div样式(在我的示例中,我将其放在颜色属性上,但它可以使用背景颜色或其他属性)。

<div class="vc_row wpb_row vc_inner vc_row-fluid">
    <?php
        // Your php code here
        $colors = array('#112233', '#445566', '#778899');
        $cIndex = 0;
        while($facilitiesfetch=mysql_fetch_array($facilitiesrun))
        {
            $color = $colors[$cIndex++ % count($colors)];
    ?>
    <div class="wpb_column vc_column_container vc_col-sm-3" style="color: <?php echo $color; ?>;">
      <!-- The rest of your html here -->
    </div>
    <?php } ?>
</div>