Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-17-2012, 09:18 PM   PM User | #1
HDRebel88
Regular Coder

 
Join Date: May 2011
Posts: 112
Thanks: 11
Thanked 5 Times in 5 Posts
HDRebel88 is an unknown quantity at this point
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..
HDRebel88 is offline   Reply With Quote
Old 12-17-2012, 09:26 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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>'
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
HDRebel88 (12-17-2012)
Old 12-17-2012, 09:32 PM   PM User | #3
HDRebel88
Regular Coder

 
Join Date: May 2011
Posts: 112
Thanks: 11
Thanked 5 Times in 5 Posts
HDRebel88 is an unknown quantity at this point
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>'
HDRebel88 is offline   Reply With Quote
Old 12-17-2012, 09:38 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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..
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
HDRebel88 (12-17-2012)
Old 12-17-2012, 09:47 PM   PM User | #5
HDRebel88
Regular Coder

 
Join Date: May 2011
Posts: 112
Thanks: 11
Thanked 5 Times in 5 Posts
HDRebel88 is an unknown quantity at this point
Right... it finally clicked just a second ago.

Thanks.
HDRebel88 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:05 PM.


Advertisement
Log in to turn off these ads.