-1

Want to create a counter for dynamic value from the database. Like if I am getting $count as 1 then it shows only two div like First for 0 and second for 1. If I am getting $count value 50 then it should show me 3 divs like 0, 5 and 0. For multiple count values how can I make it dynamic for the $count value?

Given below I have attached an image like if I am getting this no then how can I separate this no to all dynamic div.

sweta
  • 93
  • 1
  • 8
  • 1
    Please paste your code – Mayank Vadiya Jan 02 '18 at 12:34
  • 1
    Please edit your question and add the relevant parts of your code into it. You need to show your own efforts, because Stack Overflow is not a write-my-code-for-me service. Please see [How to Ask](https://stackoverflow.com/questions/how-to-ask). – Dave Jan 02 '18 at 12:38

3 Answers3

1

try this.

<?php
 $count=1264553;
 $array = str_split($count);
  echo "<div>0 </div>";
 foreach($array as $value){
echo "<div>".$value."</div>";
 }
 ?>
NanThiyagan
  • 552
  • 6
  • 13
1

According to me, you should first convert your count to 3 digit number: How to format numbers with 00 prefixes in php?

Than convert 3 digits number to string and split it to an array of letters: http://php.net/manual/en/function.str-split.php

Iterate over the array and generate divs as per your requirements.

Dhiraj kadam
  • 377
  • 3
  • 15
1

You can easily achieve this in php by embbed html code in php. You can use while or for or foreach , any of them. Example code is follows :

With while :

$count=1;
$i=0;
while($i < $count){
echo "<div>".$count."</div>";
$i++;
}

With for loop :

$count=1;
for($i=0;$i < $count;$i++){
echo "<div>".$count."</div>";
}

With foreach :

Here we have to use array

$count=1;
$array = str_split($count);
echo "<div>0 </div>";
foreach($array as $value){
echo "<div>".$value."</div>";
 }

Any more clearification if need please let me know.

Prabhu Nandan Kumar
  • 1,205
  • 12
  • 22