...

View Full Version : foreach? - treat first, middle and last differently



fruitwerks
12-15-2010, 08:31 PM
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:



<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!

mlseim
12-15-2010, 08:41 PM
what are 3 and 4? are they also "middle"?

fruitwerks
12-15-2010, 08:44 PM
Yes, sorry I'll edit my post to not confuse others!

Thanks!

Chipzzz
12-15-2010, 09:15 PM
This should do it:



$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;

fruitwerks
12-15-2010, 09:27 PM
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...


<?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!

Chipzzz
12-15-2010, 09:54 PM
I didn't test this so it may be a bit buggy, but it should be pretty close to what you want:



<?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;
?>

kbluhm
12-16-2010, 12:19 AM
This will work on any array values, not just integers:


$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:


<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.

fruitwerks
12-16-2010, 12:23 AM
That looks pretty slick - I'll have a look tomorrow

Thanks!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum