Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    May 2011
    Posts
    124
    Thanks
    12
    Thanked 6 Times in 6 Posts

    Dynamic closing div placement

    I need some help adding a closing <div> tag to my php. I want the closing tag to placed when a row of items is either 4 items long or less. So say I have two rows, one with 4 entries and one with 3... The row with 4 should add the closing div after the 4th entry, and the one with 3 should add the closing div after the 3rd (or draw a blank 4th div; which may be a better option).


    Here's what I have so far:
    PHP Code:
    $i=0;
        foreach(
    glob('./images/stills/story_boards/*.*') as $filename){
        if(
    $i==0){
        
    $content2.='<div>';
        }
        
    //$content2.='<img src="'.$filename.'" />';
        
    $i++;
        if(
    $i==5){
        
    $i=0;
        }
        }
    $content2.='</div>'
    Last edited by HDRebel88; 12-17-2012 at 09:52 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Presumably it will open after the 4 right?
    Use a modulus operator.
    PHP Code:
    $i 0;
    $content2 '<div>';
    foreach (...)
    {
        if (
    $i && $i == 0)
        {
            
    $content2 .= '</div><div>';
        }
        
    // append stuff here.
        
    ++$i;
    }
    $content2 .= '</div>'

  • Users who have thanked Fou-Lu for this post:

    HDRebel88 (12-17-2012)

  • #3
    Regular Coder
    Join Date
    May 2011
    Posts
    124
    Thanks
    12
    Thanked 6 Times in 6 Posts
    That seems like it would work, but how would I get the data into the first div?

    Shouldn't it be something like this:

    PHP Code:
    $i 0;
    $content2 '<div>';
    foreach (...)
    {
        if(
    $i==0){
        
    //first div's data here
        
    $content2.='</div><div>';
        }
        elseif (
    $i && $i == 0)
        {
            
    $content2 .= '</div><div>';
        }
        
    // append stuff here.
        
    ++$i;
    }
    $content2 .= '</div>'

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Nope, it won't close/open the div unless you are on iteration > 0. Place whatever you want to append to $content2 where I commented it.

    Edit:
    For a quick example:
    PHP Code:
    <?php

    $i 
    0;
    $range range(111);
    $content2 .= '<div>';
    foreach (
    $range AS $item)
    {
        if (
    $i && $i == 0)
        {
            
    $content2 rtrim($content2', ');
            
    $content2 .= '</div><div>';
        }

        
    $content2 .= $item ', ';
        ++
    $i;
    }
    $content2 rtrim($content2', ');
    $content2 .= '</div>';
    print 
    $content2;
    Should result in:
    Code:
    <div>1, 2, 3, 4</div><div>5, 6, 7, 8</div><div>9, 10, 11</div>
    Last edited by Fou-Lu; 12-17-2012 at 09:40 PM.

  • Users who have thanked Fou-Lu for this post:

    HDRebel88 (12-17-2012)

  • #5
    Regular Coder
    Join Date
    May 2011
    Posts
    124
    Thanks
    12
    Thanked 6 Times in 6 Posts
    Right... it finally clicked just a second ago.

    Thanks.


  •  

    Posting Permissions

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