PHP: Need Help with a FOR Loop with Dynamic Incrementing

Posted on 2014-08-16
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 76

    Accepted Solution

    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 107

    Expert Comment

    by:Ray Paseur
    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 82

    Expert Comment

    by:Dave Baldwin
    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

    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

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    As this topic comes over and over again in different forms, I've finally decided to write a short (yea, right...) article / tutorial about pagination with PHP with MySQL database. There are dozens of these kind of tutorials, I know - I wanted to mak…
    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    731 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now