#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2005
    Posts
    3
    Rep Power
    0

    Number series loops


    I'm working on a very simple script to create a numbering database file for a numbering machine. ! basically have 4 variables:

    Total numbers
    Starting number
    Numbers per page
    Pages per set

    Here's an example of the output I'm needing:

    Total numbers 6
    Starting number 1
    Numbers per page 2
    Pages per set 3
    Output: 1,2,1,2,1,2,3,4,3,4,3,4,5,6,5,6,5,6


    I'm really having trouble wrapping my head around the while loops. Here's what I've got thus far:

    PHP Code:

    $total_numbers 
    6;
    $start_number 1;
    $numbers_per_page 2;
    $pages_per_set 3;
    $print_number $start_number;

    $tn 1;
    while (
    $tn <= $total_numbers)
    {
        
    $pps 1;
        while (
    $pps <= $pages_per_set
        {
            
    $npp 1;
            if (
    $pps <= $pages_per_set)
            {
                
    $print_number $start_number;
            }

            while (
    $npp <= $numbers_per_page
            {
                print 
    $print_number."\n"
                
    $print_number++;
                
    $npp++;
            }
            
    $pps++;
        }
        
    $tn++;

    I'm obviously missing a few crucial steps, but haven't had enough experience to figure this out.
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    use for loops, not while loops. This will make the code much more compact and readable.

    PHP Code:
    $total_numbers 18
    $start_number 1
    $numbers_per_page 3
    $pages_per_set 3

    $number_of_sets $total_numbers / ($pages_per_set $numbers_per_page);

    for (
    $set 0$set $number_of_sets$set++)
        for (
    $page 0$page $pages_per_set$page++)
            for (
    $number 0$number $numbers_per_page$number++)
                
    # calculation of the number 
    In the innermost loop, you can now calculate the number.

    Look at the number sequence:

    Code:
    set 0:
    start, start + 1, ..., start + npp - 1 (repeated pps times)
    set 1:
    start + npp, start + npp + 1, ..., start + npp + npp - 1 (repeated pps times)
    set 2:
    start + 2 * npp, start + 2 * npp + 1, ..., start + 2 * npp + npp - 1 (repeated pps times)
    ...
    Do you see the pattern? As a hint: The number depends on the start number, the current set and the number index within the page.

    The problem with your approach is that you're constantly incrementing the current number while it's actually going up and down.
    Last edited by Jacques1; November 28th, 2012 at 02:07 PM. Reason: nonsense
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2005
    Posts
    3
    Rep Power
    0
    Thanks for the help. I ended up sticking with while loops for now since that's what I know. Here's my code:

    PHP Code:
    $total_numbers 10;
    $start_number 1;
    $numbers_per_page 2;
    $pages_per_set 4;
    $fill 4;


    $tn 1;
    while (
    $tn <= ($total_numbers/$numbers_per_page))
    {
        
    $pps 1;
        while (
    $pps <= $pages_per_set
        {
            
    $npp 1;
            if (
    $pps <= $pages_per_set)
            {
                
    $print_number $start_number;
            }
            while (
    $npp <= $numbers_per_page
            {
                
    $print_number_length strlen($print_number);
                
    $print_number_fill = ($fill $print_number_length);
                
                for( 
    $i=0$i<$print_number_fill$i++ )
                {
                    
    $zeros $zeros.'0';
                }
                echo 
    "$zeros$print_number\n";
                
    $print_number++;
                
    $npp++;
                unset(
    $zeros);
            }
            
    $pps++;
        }
        
    $tn++;
        
    $start_number = ($start_number+$numbers_per_page);

    the output for that code:
    0001
    0002
    0001
    0002
    0001
    0002
    0001
    0002
    0003
    0004
    0003
    0004
    0003
    0004
    0003
    0004
    0005
    0006
    0005
    0006
    0005
    0006
    0005
    0006
    0007
    0008
    0007
    0008
    0007
    0008
    0007
    0008
    0009
    0010
    0009
    0010
    0009
    0010
    0009
    0010
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by thenewbeat
    I ended up sticking with while loops for now since that's what I know.
    If you don't know "for" loops, learn them. It's the most basic control structure right after "if" and "while". And it isn't complicated at all. A "for" loop is basically just a special "while" loop with an integrated counter mechanism.

    You also don't have to do the string padding yourself, the printf() function will do it for you:
    PHP Code:
    // pad the number with 0s on the left to at least 4 characters
    printf('%04d'7); 
    So that's how I would have solved the problem:

    PHP Code:
    <?php

    $total_numbers 
    36;  
    $start_number 1;  
    $numbers_per_page 2;  
    $pages_per_set 3;
    $min_number_width 4

    $number_of_sets $total_numbers / ($pages_per_set $numbers_per_page); 

    for (
    $set 0$set $number_of_sets$set++) 
        for (
    $page 0$page $pages_per_set$page++) 
            for (
    $number 0$number $numbers_per_page$number++) 
                
    printf("%0{$min_number_width}d"$start_number $set $numbers_per_page $number);
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2005
    Posts
    3
    Rep Power
    0
    Thanks for the printf tip. When I use it I get a strange tag after each number.
    PHP Code:
    $fill 5;

    printf("%0{$fill}d"$print_number); 
    outputs:

    00001<d>
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    Software Design


    Hello everyone,

    I am looking for a tool to help me memorize arabic text. This tool should be similar to this one, "How to Memorize Verbatim Text - Productivity501". Please search for that article thru Google as outside links are not allowed here. The tool I'm referring to is at the bottom of the article.

    The tool should be able to extract up to first 3 letters of an Arabic word when I copy paste the Arabic text into the tool field box.

    Can this be done automatically or will I need this be done manually where someone goes through the text and inserts first 3 letters from each word. What programming language can this be coded in? Which would give the best result? I want it to be a fluid user experience.

    Here is an example of Arabic text. Please search for "Learn Quran" thru Google as outside links are not allowed here.

    Thanks!

IMN logo majestic logo threadwatch logo seochat tools logo