1

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.

8
  • First, you cannot have multiple of the same key in an associative array. Commented Feb 13, 2016 at 4:23
  • @FrayneKonok : Here while loop is continuing.In each iteration it will add a new class name which is defined above.if the set of class name will be finish still db value is fetching again class will start from first. Commented Feb 13, 2016 at 4:25
  • same key in an associative array is not allowed so please remove it. Commented Feb 13, 2016 at 4:26
  • it was my mistake..wrongly i had done this. Commented Feb 13, 2016 at 5:16
  • oye...i have not done that..somebody else but your code is working fine. Commented Feb 13, 2016 at 5:17

3 Answers 3

2

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);
}?>
Sign up to request clarification or add additional context in comments.

Comments

2

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)

Comments

1

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 Comments

$class = array('0'=>'A', '1'=>'B') is not necessary $class = array('A','B') will do the same
(Even better now: $class = ['A','B'] if your version of PHP is fresh enough)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.