www.webdeveloper.com
Results 1 to 8 of 8

Thread: [RESOLVED] for each help

  1. #1
    Join Date
    Dec 2004
    Location
    Oregon
    Posts
    87

    resolved [RESOLVED] for each help

    Hey, I have a script to display images in an ordered list and then styled to make it look nice. The only problem is that this uses the css property float and I am just using div tags so it messes that up. I was wondering how I would go about creating a foreach statement to create a table 4 columns wide? If I am not being clear just let me know and I will try to explain. Thanks for any help.

  2. #2
    Join Date
    May 2007
    Posts
    184
    I'm not sure if I understand right but you could wrap each element in the foreach loop with <td> tags.

  3. #3
    Join Date
    Dec 2004
    Location
    Oregon
    Posts
    87
    Right, that is pretty much what I am wanting to do, but I also only want 4 images per row and that is the part I don't know how to add to the foreach loop

  4. #4
    Join Date
    Nov 2007
    Posts
    140
    You'll need two loops for that.

    PHP Code:
    //$items = your array of items
    $num_cols 4;
    echo 
    '<table>';
    for( 
    $i 0$max count($items) / $num_cols$i $max; ++$i ) {
      echo 
    '<tr>';
      for( 
    $i2 0$i2 $num_cols; ++$i2 ) {
        
    $current_item $i $num_cols $i2;
        if( isset(
    $items[$current_item]) ) {
          echo 
    '<td>'$items[$current_item], '</td>';
        } else {
          echo 
    '<td>&nbsp</td>';
        }
      }
      echo 
    '</tr>';
    }
    echo 
    '</table>'
    Note that the code above is untested but hopefully it helps.

  5. #5
    Join Date
    May 2007
    Posts
    184
    Quote Originally Posted by EJMAES1973
    Right, that is pretty much what I am wanting to do, but I also only want 4 images per row and that is the part I don't know how to add to the foreach loop
    so you only want to go through the foreach loop 4 times?

    Use a counter, check when it reached 4, then use "break;" to break out of the loop.

  6. #6
    Join Date
    Dec 2004
    Location
    Oregon
    Posts
    87
    Quote Originally Posted by tfk11
    Note that the code above is untested but hopefully it helps.

    It worked no worries. I'm surprised it did though. Not because of your code, but because I combined two different sets of code and I know pretty much nothing about for statements. Thank you so much for the help. Does anyone know a good place to learn about for statements?

  7. #7
    Join Date
    Nov 2007
    Posts
    140
    Glad the code helped.

    Everything you need to know about for statements can be found in the php manual under control structures.

  8. #8
    Join Date
    Dec 2004
    Location
    Oregon
    Posts
    87
    alright thanx. I was able to understand that code enough to use it for other things so I think I am getting the hang of it. Thanx again

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles