PHP: Need Help with a FOR Loop with Dynamic Incrementing

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 ! :)
tonyhhiscAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

arnoldCommented:
Your increment portion is missing the equal sign
$i+=50.
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
$i=10;
While ( $i<=500) {




Here you can test and either increment by 50 or by another value.
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
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.
0
Dave BaldwinFixer of ProblemsCommented:
This will generate the list but you will have to add code to select the output you want.
<?php 
$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

0
tonyhhiscAuthor Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.