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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化