Home > PHP, Wordpress > Simplifying Flutter duplicate groups and fields

Simplifying Flutter duplicate groups and fields


Flutter or Fresh-pages is a really handy plugin for WordPress. With it, you can quite easily create custom write panels, fields and groups of information.

Having worked with this plugin quite extensively recently, I have encountered a few issues which have proved somewhat frustrating, dealing with duplicate groups or fields in particular. Flutter uses a counter based system in which to access duplicate groups. Simple enough when using a standard for loop and all the duplicates are in the correct order – but somewhat tricky to handle when say, deleting one of the duplicates.

I’ve since resorted to writing a function to make handling these duplicates and a number of associated problems whilst looping through them much, much simpler.

Which brings us to the following function – which you can paste straight into your theme’s functions.php file :

function jt_get_flutter_duplicates($field, $group){
	if (is_string($group)){
		$numfiles = getGroupDuplicates($group);
		$isgroup = true;
	} else {
    	$numfiles = getFieldDuplicates($field, $group);
    	$isgroup = false;
	}
    $return = false;
    if ($numfiles > 0){
        $return = array();
        $count = 1;
        $tmp = "";
        $first = "";
        $total = ($numfiles > 1) ? 100 : 1;
        while($count <= $total){
        	if ($isgroup){
        		$value = get($field, $count, 1);
        	} else {
            	$value = get($field, $group, $count);
        	}
            if (empty($value) || $value == $first || $tmp == $value){
                $count++;
                continue;
            } elseif ($count == 1){
                $first = $value;
            }

            $tmp = $value;
            $return[] = ($isgroup) ? $count : $value;
        }
    }
    return (is_array($return) && count($return) > 0) ? $return : false;
}

I’m not going to go into too much detail right now, but essentially what this function does is gets the required field or group duplicates, loops through them (up to a max of 100 iterations just to be safe) and places them into an array containing the correct index values (if a group) or an array of field values (if a field).

Group duplicates

Set the first parameter to the name of a field within the group which should contain a unique value (such as name, or caption). The second parameter can be any field within the required group.

$groupItems = jt_get_flutter_duplicates("news_image", "news_image");
if (is_array($groupItems) && count($groupItems) > 0){
    foreach($groupItems as $i) {        
	echo get_image("news_image",$i, 1, 1);
        echo "<br />" . get("news_image_caption",$i, 1, false);
    }
}

Field Duplicates

Set the first parameter to the name of the field you want to retrieve. Set the second parameter to the group index you want to retrieve the field duplicates from. For instance, you could have 1 group which can be duplicated, containing 3 fields, one of which can be duplicated. When looping through the group duplicates, you’ll want another sub-loop to handle the duplicated field contents. The function will return an array of values which you can then use in a standard foreach loop.

$groupItems = jt_get_flutter_duplicates("office_title", "office_title");
if (is_array($groupItems) && count($groupItems) > 0){
    foreach($groupItems as $i) {  
        echo get("office_title",$i,1,false);
        $contacts = jt_get_flutter_duplicates("office_contact", $i);
        if (is_array($contacts) && count($contacts) > 0) {           
           foreach($contacts as $contact){               
               echo "<br />Contact Person: " . $contact;
           }
        }
    }
}

This is of course a quick fix and by no means infallible, so I’d love to hear if you have any improvements or a simpler way of dealing with this particular issue.

Advertisements
Categories: PHP, Wordpress
  1. Marcus
    September 22, 2009 at 3:24 pm

    Really nice work. Sometimes I got one group twice but this could be “fixed” by just saving the post again.

  2. November 11, 2009 at 1:11 am

    Fantastic function, thanks a ton! I don’t know if I’d get anywhere with Flutter if it weren’t for people like you. Thanks!

  3. February 28, 2010 at 12:16 am

    I’m trying to use your function to call the values of a duplicated field.
    It doesn’t seem to be working for me, maybe you can help me.

    Here’s my code in the template:

    	$musictype = jt_get_flutter_duplicates("place music genre", "place general information");
    	if (is_array($musictype) && count($musictype) > 0) {
    		foreach($musictype as $musictype){
    		echo "".$musictype."";
    		}
    	}
    

    Nothing is unchanged in the code you instructed to paste into the functions file.

    • March 11, 2010 at 3:01 pm

      Apologies on the delayed response.

      There seems to be a basic error in your for loop : foreach($musictype as $musictype) – this won’t work, essentially you’re overwriting the array with it’s first element.

      Something like foreach($musictype as $item) would work.

  4. Jeff
    March 21, 2010 at 6:46 pm

    Thank you. Thank you. Thank you. I jumped into a wordpress project with flutter without realizing the repercussions and was investigating deinstallation/total-wp-reinstall options before finding your blog. You’ve saved me hours of work.

    • March 24, 2010 at 8:27 am

      Hi Jeff,

      You’re so welcome. Glad that I’ve managed to help a fellow developer. Good luck with the rest of your project 🙂

  5. Drew
    April 10, 2010 at 11:11 pm

    This seems to work fine in everything but IE. What am I doing wrong?

    0){
    foreach($groupItems as $i) {
    echo "". "<a href='". get("homepage_slideshow_link",$i, 1, false)."'";
    echo get_image("homepage_slideshow_image",$i, 1, 1). "";
    }
    }?>

    • April 12, 2010 at 10:58 am

      Hi Drew,

      I’m not too sure what it is you’re trying to do, as the code I posted originally has nothing to do with browsers. I have however spotted some errors in the code you’ve provided.
      For one, you’re not closing the anchor tag off, and secondly, you’re not wrapping the image inside the anchor tag.

      So try this :
      echo “”. “<a href='”. get(“homepage_slideshow_link”,$i, 1, false).”‘ rel=”nofollow”>”;
      echo get_image(“homepage_slideshow_image”,$i, 1, 1). “</a>”;

      If you need any further help, feel free to send me an email and we can take it from there: calisza at gmail dot com

  6. July 14, 2010 at 8:07 am

    Thanks; very nice. I thought I had that plug-in figured out until I deleted duplicate items.

  7. September 7, 2010 at 9:15 pm

    I stumbled across this just trying to find some flutter/magic fields documentation for the duplicate groups functions, and man, have you saved me some serious time. Thanks very much for sharing this bit of code.

  8. February 21, 2011 at 11:43 am

    You saved my day !! Thank you.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: