doubi9255 2016-02-13 04:13
浏览 90
已采纳

如何使用PHP在循环内动态添加类

I need one help.I am displaying some data which are coming from DB but i need to set the class name dynamically in each loop iteration using PHP.I am explaining my code below.

<?php
     $class=array('classname'=>'pic1-caption bottom-to-top','classname'=>'pic1-caption top-to-bottom','classname'=>'pic1-caption left-to-right','classname'=>'pic1-caption right-to-left','classname'=>'pic1-caption rotate-in','classname'=>'pic1-caption rotate-out','classname'=>'pic1-caption open-up','classname'=>'pic1-caption open-down','classname'=>'pic1-caption open-left','classname'=>'pic1-caption open-right','classname'=>'pic1-caption come-left','classname'=>'pic1-caption come-right');
     $sql=mysql_query("select * from phr_health_care where status=1  order by health_id desc");
     $key=0;
     while($row=mysql_fetch_array($sql)){
 ?>
<div class="pic1">
 <img src="backend/uploads/<?php echo $row['image'] ?>" class="pic1-image" alt="pic1"/>
 <span class="pic1-caption bottom-to-top"> //here need to set class name dynamically.
 <h1 class="pic1-title">DIABETICS</h1>
 <p class="newp"><?php echo $row['title'] ?></p>
  <a href="health.html" class="detall">Go for Details</a>
</span>

Here All data are coming from database and i need to set class name in span element dynamically which are declared in $class variable .Here also i need once all class declared inside $class variable will finish while loop continuing ,again it will start from beginning until the db value fetch has not finished.Please help me.

  • 写回答

3条回答 默认 最新

  • dqwh1209 2016-02-13 04:28
    关注

    You need to do some change in your code which are stated by comment itself:-

    You need to convert your $class array into indexed array instead of associative array because all the keys are same and its wrong.

    <?php
     $class=array('pic1-caption bottom-to-top','pic1-caption top-to-bottom','pic1-caption left-to-right','pic1-caption right-to-left','pic1-caption rotate-in','pic1-caption rotate-out','pic1-caption open-up','pic1-caption open-down','pic1-caption open-left','pic1-caption open-right','pic1-caption come-left','pic1-caption come-right'); //I make it an indexed array because associative array with same key is wrong and give you error
     $sql=mysql_query("select * from phr_health_care where status=1  order by health_id desc");
     $key=0;// start counter 
     $class_count = count($class); // take count of class array
     while($row=mysql_fetch_array($sql)){
     ?>
       <div class="pic1">
       <img src="backend/uploads/<?php echo $row['image'] ?>" class="pic1-image" alt="pic1"/>
       <span class="<?php echo $class[$key];?>"><!--  get classes dynamically-->
         <h1 class="pic1-title">DIABETICS</h1>
         <p class="newp"><?php echo $row['title'] ?></p>
         <a href="health.html" class="detall">Go for Details</a>
       </span>
     <?php 
     if($key == $class_count-1){ // check when classes array values ends start count from 0 again
         $key = 0;
     }else{
        $key++;
     }
    }?>
    

    Note:- since mysql_* is deprecated now use mysqli_* or PDO. Thanks

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘