如何使用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.

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

If I understood your question, here is what you want to do:

<?php
$class=array('A','B','C');
$length = count($class);
$key=0;
while(...) {
  $index = $key % $length;
  $current_class= $class[$index];
  $key += 1;
}

$current_class will loop on every class value while $key grows.

$key will take: 0, 1, 2, 3, 4, 5, 6, etc.

but $index will take: 0, 1, 2, 0, 1, 2, etc.

And so $class[$index] will be: 'A', 'B', 'C', 'A', 'B', 'C', etc.

(See "modulus" operator at http://php.net/manual/en/language.operators.arithmetic.php)

Yes you can round your array to dynamically use. Here is the example of such things.

Modified Scripts:

Modified Array:

$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');

SQL:

$sql=mysql_query("select * from phr_health_care where status=1  order by health_id desc");

Rest of the code:

$len = count($class);
$key = 0;
while($row=mysql_fetch_array($sql)){
?>
<div class="pic1">
    <img src="backend/uploads/<?php echo $row['image'] ?>" class="pic1-image" alt="picture"/>
    <span class="<?php echo $class[$len%$key];?>"> 
        <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>
</div>
<?php 
$key = ($key == ($len-1)) ? 0 : ($key + 1);
}?>