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 8 of 8
  1. #1
    New Coder
    Join Date
    Aug 2007
    Posts
    35
    Thanks
    3
    Thanked 0 Times in 0 Posts

    foreach? - treat first, middle and last differently

    The best way I can explain what I am wanting to do is take an array...

    lets say the contents are 1,2,3,4,5

    I am currently using a foreach on the array, but I'm not sure how it will play into this. Assuming this can be done with foreach and appending to a final $output, I would like to end up with something like this:

    Code:
    <div class="first">1</div>
    <div class="mid">2</div>
    <div class="mid">3</div>
    <div class="mid">4</div>
    <div class="last">5</div>
    I have the concept down, but I can't wrap my head around the php way of it, just looking for pointers...

    Thanks!
    Last edited by fruitwerks; 12-15-2010 at 08:44 PM.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,511
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    what are 3 and 4? are they also "middle"?

  • #3
    New Coder
    Join Date
    Aug 2007
    Posts
    35
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Yes, sorry I'll edit my post to not confuse others!

    Thanks!

  • #4
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    1
    Thanked 1 Time in 1 Post
    This should do it:

    PHP Code:
    $a = array(1,2,3,4,5);
    $l count($a);
    $output '';
    foreach (
    $a as $n) {
      
    $output .= "<div class=\"";
      switch (
    $n) {
        case 
    1:
          
    $c 'first';
          break;
        case 
    $l:
          
    $c 'last';
          break;
        default:
          
    $c 'mid';
          break;
      }
      
    $output .= "\">$n</div>\r\n";
    }
    echo 
    $output
    Last edited by Chipzzz; 12-15-2010 at 09:19 PM. Reason: Code correction

  • Users who have thanked Chipzzz for this post:

    fruitwerks (12-15-2010)

  • #5
    New Coder
    Join Date
    Aug 2007
    Posts
    35
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks, but it is not that simple... I used an array of numbers to keep things simple, but I guess it is confusing people.

    Here is the actual code I will be modifying, it returns chunks of html. I also wanted to randomize the array before processing it, know how to do that, but just wanted to mention.

    It will always be a different count, so anything between 1 and up to 10 items. I want to add some special logic for a single item and just two items - 1 item will get the class first, two items will get first and last class...

    Code:
    <?php
    $ar_chunks = split(',',$chunks);
          $ar_chunks = array_unique($ar_chunks);
          $output = '';
          foreach ($ar_chunks as $chunk) {
          if ($chunk != '') {
          $c = $modx->getChunk($chunk);
          if ($c) $output .= $c;
          }
          }
          if ($output != '' && isset($wrapperdiv)) {
          $output = '<div id="'.$wrapperdiv.'">'.$output.'</div>';
          }
          return $output;
    ?>
    I probably should have posted this to start with...

    Thanks!

  • #6
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    1
    Thanked 1 Time in 1 Post
    I didn't test this so it may be a bit buggy, but it should be pretty close to what you want:

    PHP Code:
    <?php
      $ar_chunks 
    split(',',$chunks);
      
    $ar_chunks array_unique($ar_chunks);
      
    $output '';
      
    $l count($ar_chunks);
      
    $n 0;
      foreach (
    $ar_chunks as $chunk) {
        if (
    $chunk != '') {
          
    $c $modx->getChunk($chunk);
          if (
    $c) {
            
    $output .= "<div class=\">";
            switch (
    $n) {
            case 
    0:
              
    $output .= 'first';
              break;
            case 
    $l:
              
    $output .= 'last';
              break;
            default:
              
    $output .= 'mid';
              break;
          }
        }
        
    $output .= "\"</div>\r\n";
        
    $n += 1;
      }
      if (
    $output != '' && isset($wrapperdiv)) {
        
    $output '<div id="'.$wrapperdiv.'">'.$output.'</div>';
      }
      return 
    $output;
    ?>
    Last edited by Chipzzz; 12-15-2010 at 10:07 PM. Reason: Sure enough it was (& may still be) a bit buggy

  • #7
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    This will work on any array values, not just integers:
    PHP Code:
    $items = array( 'one''two''three''four''five' );

    $output = array();

    while ( ! empty( 
    $items ) )
    {

        
    $item array_shift$items );

        switch ( 
    TRUE )
        {

            case ! isset( 
    $is_first ):
                
    $is_first TRUE;
                
    $class    'first';
                break;

            case empty( 
    $items ):
                
    $class 'last';
                break;

            default:
                
    $class 'mid';

        }

        
    $output[] = '<div class="' $class '">' $item '</div>';

    }

    $output implode"\n"$output );

    echo 
    $output
    Output:
    Code:
    <div class="first">one</div>
    <div class="mid">two</div>
    <div class="mid">three</div>
    <div class="mid">four</div>
    <div class="last">five</div>
    Be aware that using array_shift() will cause the $items array to be empty after the loop, so if you need that info afterward make a copy of it first.
    Last edited by kbluhm; 12-16-2010 at 04:56 PM.

  • Users who have thanked kbluhm for this post:

    fruitwerks (12-16-2010)

  • #8
    New Coder
    Join Date
    Aug 2007
    Posts
    35
    Thanks
    3
    Thanked 0 Times in 0 Posts
    That looks pretty slick - I'll have a look tomorrow

    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
    •