PHP: Need Help with a FOR Loop with Dynamic Incrementing

Posted on 2014-08-16
Medium Priority
Last Modified: 2014-09-04
Hello, I can't seem to get this and I'm exhausted from trying, my brain is fried hah.

I want:
For loop where I can specify each of the values (min, max, increment). I will let user specify these values thru CMS, it's for a wholesale page where there is restrictions.

So for example:
Minimum order: 10
Then increment from 50 from there.
So if user sets, 10 minimum, 500 maximum, and incrementing by 50

I thought I could do:

for ( $i = 10;  $i <= 500;  $i + 50)
Which obviously doesn't work. Completely fails and puts the page into an endless loop.

(Btw, I'm just using those value above for a clear example. But they will be values ($minqty (10), $maxqty (500), $qtyinc(50) )

So then I tried:

for($i=10;$i<=500;$i+$qtyinc) {
   if ($i!=10) { $i = $i + $qtyinc; }
  echo $i."<br>";

And I get:
10, 61, 112, 163, 214, 265, 316, 367, 418, 469, 520

Close, but no cigar.

The desired result is:
10, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500

How can i do it??

Thanks a lot for your help ! :)
Question by:tonyhhisc
LVL 81

Accepted Solution

arnold earned 2000 total points
ID: 40265187
Your increment portion is missing the equal sign
Without the equal $i is incremented by the default increment of 1 seemingly when $i Is updated.

In a for loop, you if condition as well as the for loop will increment.
First run is 10.
On second run hitting the for loop, the new value will be 60
If check, will increment $i by qtyinc.
Next $i will be 60+qtyinc+50
Hitting if, another qtyinc is added.

To do what you want, you have to use a while loop
While ( $i<=500) {

Here you can test and either increment by 50 or by another value.
LVL 111

Expert Comment

by:Ray Paseur
ID: 40265247
10 minimum, 500 maximum, and incrementing by 50
Does this mean that the client can order 10 items, but if he wants 11 he is forced to order 60 instead?  And if he wants to order 501 this is disallowed?  I'm asking because I think there may be more to the design pattern than just getting the syntax right on a for() loop.  If there is more you can tell us, maybe we can help more.
LVL 84

Expert Comment

by:Dave Baldwin
ID: 40265266
This will generate the list but you will have to add code to select the output you want.
$quantlist = array(10, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500);
foreach($quantlist as $value) {
	echo "Quantity ".$value."<br>";

Open in new window


Author Closing Comment

ID: 40304725
Thank you Arnold, and thanks for your detailed guidance. Much appreciated!

Thanks for your comments Ray.

And thank you Dave, but since the numbers will be entered by the site owner through a CMS it has to work dynamically.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question