943,683 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 632
  • PHP RSS
Oct 4th, 2008
0

Math Problem

Expand Post »
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.
php Syntax (Toggle Plain Text)
  1. 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.
Similar Threads
Reputation Points: 21
Solved Threads: 11
Junior Poster
MVied is offline Offline
111 posts
since Aug 2008
Oct 4th, 2008
1

Re: Math Problem

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:
php Syntax (Toggle Plain Text)
  1. $loop=0;
  2. while ($loop<=100)
  3. {
  4. $val=$loop/10;
  5. if ($val==round($val))
  6. {
  7. echo ($i % ($amount / 10) == 0) ? "<div class=\"load\"></div>" : "";
  8. }
  9. $loop+=1;
  10. }
Hope that helps to answer your question.
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007
Oct 4th, 2008
0

Re: Math Problem

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

Soon I'm going to post it in the Web Developer's Lounge. It'll be something everyone wants.
Reputation Points: 21
Solved Threads: 11
Junior Poster
MVied is offline Offline
111 posts
since Aug 2008
Oct 4th, 2008
0

Re: Math Problem

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:
Click to Expand / Collapse  Quote originally posted by MVied ...
php Syntax (Toggle Plain Text)
  1. echo ($i % ($amount / 10) == 0) ? "<div class=\"load\"></div>" : "";
wouldn't work.

Can you post the rest of your code?
Reputation Points: 358
Solved Threads: 89
Posting Shark
R0bb0b is offline Offline
986 posts
since Jun 2008
Oct 4th, 2008
0

Re: Math Problem

Oh, you got it, OK.
Reputation Points: 358
Solved Threads: 89
Posting Shark
R0bb0b is offline Offline
986 posts
since Jun 2008
Oct 4th, 2008
1

Re: Math Problem

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
Last edited by MVied; Oct 4th, 2008 at 11:13 pm.
Reputation Points: 21
Solved Threads: 11
Junior Poster
MVied is offline Offline
111 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: 30-11-1999 Help Me
Next Thread in PHP Forum Timeline: Adding Advanced Ranking Surveys





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC