SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: looping?

  1. #1
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,644
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    looping?

    this bit of code
    PHP Code:
    <?php
    for ($i=1$i<=6$i++) {
        echo 
    "<div>";
        echo 
    "<label for='file".$i."'>Image ".$i.":</label>";
        echo 
    "<input type='file' name='file".$i."' id='file".$i."'></div>";
        echo 
    "\r\n";
    }
    ?>
    which produces
    HTML Code:
    <div><label for='file1'>Image 1:</label><input type='file' name='file1' id='file1'></div> 
    <div><label for='file2'>Image 2:</label><input type='file' name='file2' id='file2'></div> 
    <div><label for='file3'>Image 3:</label><input type='file' name='file3' id='file3'></div> 
    <div><label for='file4'>Image 4:</label><input type='file' name='file4' id='file4'></div> 
    <div><label for='file5'>Image 5:</label><input type='file' name='file5' id='file5'></div> 
    <div><label for='file6'>Image 6:</label><input type='file' name='file6' id='file6'></div>
    but would like it to instead produce
    HTML Code:
    <div><label for='file1'>Image 1:</label><input type='file' name='file1' id='file1'></div> 
    <div><label for='file2'>Image 2:</label><input type='file' name='file2' id='file2'></div> 
    <div><label for='file3'>Image 3:</label><input type='file' name='file3' id='file3'></div> 
    </div><div class="column">
    <div><label for='file4'>Image 4:</label><input type='file' name='file4' id='file4'></div> 
    <div><label for='file5'>Image 5:</label><input type='file' name='file5' id='file5'></div> 
    <div><label for='file6'>Image 6:</label><input type='file' name='file6' id='file6'></div>
    How do I change that if statement so I can split up the 6divs in h alf?

    Thanks
    "Oh, and Jenkins--apparently your mother died this morning."

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Since the loop number is hard coded, you might as well hard code the splitting into two 3 time loops:
    PHP Code:
     <?php
    for ($i=1$i<=3$i++) {
        echo 
    "<div>";
        echo 
    "<label for='file".$i."'>Image ".$i.":</label>";
        echo 
    "<input type='file' name='file".$i."' id='file".$i."'></div>";
        echo 
    "\r\n";
    }
    echo 
    '</div><div class="column">';
    for (
    $i=4$i<=6$i++) {
        echo 
    "<div>";
        echo 
    "<label for='file".$i."'>Image ".$i.":</label>";
        echo 
    "<input type='file' name='file".$i."' id='file".$i."'></div>";
        echo 
    "\r\n";
    }
    ?>

  3. #3
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    This will work for any number of iterations:
    PHP Code:
    for ($i=1$i<=6$i++) {
        
        if (
    $i && ($i 1) % == 0) {
            echo 
    "</div><div class=\"column\">\r\n";
        }
        
        echo 
    "<div>";
        echo 
    "<label for='file".$i."'>Image ".$i.":</label>";
        echo 
    "<input type='file' name='file".$i."' id='file".$i."'></div>";
        echo 
    "\r\n";


  4. #4
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,644
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    thanks
    "Oh, and Jenkins--apparently your mother died this morning."

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Here's another way using array_chunk()
    PHP Code:
    // create a spoof array with 7 items
    $vals range(1,7);

    // chunk it into 3s
    $parts array_chunk($vals3);

    // PHP_EOL = add an OS independent line end
    foreach($parts as $part){
      foreach(
    $part as $p){
        echo 
    $pPHP_EOL;
      }
    echo 
    'DIVIDER'PHP_EOL ;
    }

    // gives:
    1
    2
    3
    DIVIDER
    4
    5
    6
    DIVIDER
    7
    DIVIDER 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •