Okay, so I'm writing an application. In this application there is a drop down with the selections 10, 20, 30, and so on to 100. This drop down controls the amount of times a loop will occur. Basically, every time the application gets to a loop that is 10% of the total amount of loops, I want it to echo out a small line. On the user end this outputs a small block in a progress bar.

For example if you choose 10, then every loops it would output a block on the progress bar to give us 100%.

If you choose 20, every 2 loops it would output a block on the progress bar to give us 100%.

The easy way out would be to make a huge case select of if, elseif statement, but that's not what I want to do.

As an example, if you choose 10 (\$amount = 10), it works correctly. \$i is the increment of the loop.

``echo (\$i % (\$amount / 10) == 0) ? "<div class=\"load\"></div>" : "";``

I know that's very confusing, but hopefully someone can understand and give me some mathematical help. :P

Answered by cwarn23 387 in a post from

I have had to do similar things in the past and I find division and rounding to be the answer. An example is as below:

``````\$loop=0;
while (\$loop<=100)
{
\$val=\$loop/10;
if (\$val==round(\$val))
{
echo (\$i % (\$amount / 10) == 0) ? "<div class=\"load\"></div>" : "";
} …``````

## All 5 Replies

I have had to do similar things in the past and I find division and rounding to be the answer. An example is as below:

``````\$loop=0;
while (\$loop<=100)
{
\$val=\$loop/10;
if (\$val==round(\$val))
{
echo (\$i % (\$amount / 10) == 0) ? "<div class=\"load\"></div>" : "";
}
\$loop+=1;
}``````

commented: Your response helped me solve my problem. Much thanks. +1

Well, your solution didn't work for me, but it got me thinking and now my code works. :D

Soon I'm going to post it in the Web Developer's Lounge. It'll be something everyone wants. ;)

I don't know if round would be necessary since 10, 20, 30..., 100 are all % 10 evenly. :-/

And I don't see why this code:

``echo (\$i % (\$amount / 10) == 0) ? "<div class=\"load\"></div>" : "";``

wouldn't work. :-/

Can you post the rest of your code?

Oh, you got it, OK.

Basically the problem was \$i starting off as 0. This threw it off completely. I used \$n to start as 1 and count (because I use the \$i for more than just an increment).

I'll be posting this project soon. I know you'll like it R0b ;)

commented: Sounds interesting +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.