0

I need to assign different class names to div elements within a loop.

<?php
    $sql="select * from category order by id desc";
    $catdata=$dbobj->db_get_data($sql);
    foreach ($catdata as $v) {
?>
      <div class="col-sm-3">
       <a href="javascript:void(0)" class="item-exhibitation maploc"><?php echo $v['name'] ?></a><!-- activemap1 -->
      </div>

 <?php } ?>

Here I need to keep the class name dynamic and all are given below:

$classarr = array("item-exhibitation maploc","item-parking maploc","item-offices maploc","item-storage maploc");

The above 4 are my classes and these will be assigned dynamically. After the 4th iteration, it will take from the first element in the array of class names. How can this be achieved?

2
  • Please share expected output as well. Commented Mar 25, 2017 at 7:01
  • @SahilGulati : this class="item-exhibitation maploc" will change accordingly. Commented Mar 25, 2017 at 7:03

4 Answers 4

1

Is this the output you are looking for? Or i understood wrong.

<?php
$counter=0;
$classarr = array("item-exhibitation maploc", "item-parking maploc", "item-offices maploc", "item-storage maploc");
$sql = "select * from category order by id desc";
$catdata = $dbobj->db_get_data($sql);
foreach ($catdata as $v)
{        
    ?>
    <div class="col-sm-3">
        <a href="javascript:void(0)" class="<?php echo $classarr[$counter];?><?php echo $v['name'] ?></a><!-- activemap1 -->
    </div>
    <?php
    $counter++;
    if ($counter == 4) $counter = 0;
}
?>
Sign up to request clarification or add additional context in comments.

6 Comments

But $classarr has limited no of value.The fetched value from db may greater than $classarr value.so it may throw error.
what you want if all values will be exhausted?
Those class names will repet.
@PraveenKumar It will be 4 because while iteration of last time after increment i will get 4. Please do a dry run over code.
But its not reflecting as per expected.
|
0
    <?php
$classarr = array("item-exhibitation maploc", "item-parking maploc", "item-offices maploc", "item-storage maploc");
$sql = "select * from category order by id desc";
$catdata = $dbobj->db_get_data($sql);
$i = 0;
foreach ($catdata as $v) {
    $class_name = '';
    ?>
    <div class="col-sm-3">
        <?php
        if (array_key_exists($i, $classarr)) {
            $class_name = $classarr[$i];
        } else {
            $class_name = $classarr[0];
            $i=0;
        }
        ?>
        <a href="javascript:void(0)" class="<?= $class_name; ?>"><?php echo $v['name'] ?></a>
    </div>
    <?php
    $i++;
}
?>

4 Comments

Your answer will not work as subhra expected. It will return first class name for all iteration greater than 3
@MayaShah Now the $i is set 0 so it will take from 0 when greather than 3.
Now that's fine
@subhra: You can consider this answer for your solution.
0

You will need to set a class value based on the modulo, using an index, like this:

<?php
    $sql="select * from category order by id desc";
    $catdata=$dbobj->db_get_data($sql);
    $itemCount = count($classarr); //We need the count, but we calculate it once
    $index = $itemCount - 1; //We start it from last, so we will go to first at the first iteration
     foreach ($catdata as $v) {

     ?>
 <div class="col-sm-3">
 <a href="javascript:void(0)" class="<?php echo $classarr[$index = ($index + 1) % $itemCount]; ?>"><?php echo $v['name'] ?></a><!-- activemap1 -->
 </div>
 <?php
     }
 ?>

Comments

0

Per the php.net documentation for foreach:

There are two syntaxes:

foreach (array_expression as $value)
    statement

foreach (array_expression as $key => $value)
    statement

1

Use the second syntax to have the index assigned automatically to $key. Then use the modulo arithmetic operator to compute the remainder of the value of $index divided by the length of the array of class names (i.e. $index % count($classarr)).

0 % 4 => 0
1 % 4 => 1
2 % 4 => 2
3 % 4 => 3
4 % 4 => 0
5 % 4 => 1
...

That way the index into $classarr will be sequential and repeat, resulting in the class names being repeated.

<?php
$sql="select * from category order by id desc";
$catdata=$dbobj->db_get_data($sql);
$classarr = array("item-exhibitation maploc", "item-parking maploc", "item-offices maploc", "item-storage maploc");
 foreach ($catdata as $index => $v) {
    $class = $classarr[$index % count($classarr)];
 ?>
<div class="col-sm-3">
    <a href="javascript:void(0)" class="<?php echo $class;?>"><?php echo $v['name'] ?></a><!-- activemap1 -->
</div>
<?php
 }
?>

See a demonstration of this in this PHPfiddle. The output can be seen in the snippet below:

#Legend {
  float: right;
}

.item-exhibitation {
  color: #ff0000;
}

.item-parking {
  color: #00f;
}

.item-offices {
  color: #0f0;
}

.item-storage {
  color: #888;
}
<div id="Legend">
  <h3>Legend</h3>
  <div class="item-exhibitation">.item-exhibitation</div>
  <div class="item-parking">.item-parking</div>
  <div class="item-offices">.item-offices</div>
  <div class="item-storage">.item-storage</div>
</div>
<h3>Items</h3>
<div class="col-sm-3"><a href="javascript:void(0)" class="item-exhibitation maploc">Exposition Center</a>
  <!-- activemap1 -->
</div>
<div class="col-sm-3"><a href="javascript:void(0)" class="item-parking maploc">Parking Lot A</a>
  <!-- activemap1 -->
</div>
<div class="col-sm-3"><a href="javascript:void(0)" class="item-offices maploc">Insurance Agency A</a>
  <!-- activemap1 -->
</div>
<div class="col-sm-3"><a href="javascript:void(0)" class="item-storage maploc">Personal Storage A</a>
  <!-- activemap1 -->
</div>
<div class="col-sm-3"><a href="javascript:void(0)" class="item-exhibitation maploc">Convention Center</a>
  <!-- activemap1 -->
</div>
<div class="col-sm-3"><a href="javascript:void(0)" class="item-parking maploc">Parking Lot B</a>
  <!-- activemap1 -->
</div>


1http://php.net/foreach

Comments

Your Answer

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