Shuffle before looping multiple item fieldset

This topic is: not resolved
Viewing 4 posts - 1 through 4 (of 4 total)
Author Posts
December 18, 2014 at 6:48 am #3580
allenspach
Post count: 2

Hi

I use

<?php if ($choices = $wf->the->has("sc_fieldset_choices")) ?>
<?php foreach ($choices as $choice) : ?>
<?php echo $choice->sc_choice_01; ?>
<?php endforeach; ?>

in a template. Everything works fine. I only couldn’t find out how to shuffle the field values before looping them.

Thanks,
Marcel

December 18, 2014 at 9:20 am #3583
traversal
Post count: 207

Hi, so just to clarify, you’re trying to randomise the collection before output? There’s no built-in method to shuffle, but you could try this:

https://gist.github.com/traversal/727a30139046e962a426

This will get an array of the choices using the items method first, which is then passed into the PHP shuffle function.

December 18, 2014 at 6:23 pm #3584
allenspach
Post count: 2

Hi, thanks for your help. I tried but I get
Warning: Invalid argument supplied for foreach()

December 18, 2014 at 7:33 pm #3587
traversal
Post count: 207

Hi, apologies for that, I wrote the code incorrectly, forgetting that shuffle doesn’t return an array. I’ve updated the gist now:

https://gist.github.com/traversal/727a30139046e962a426

Let me know if that works.

Thanks!

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

Latest From the Blog

MasterPress 1.4.1 is now available

27th May 2024

This update addresses backwards compatibility issues that were introduced in the previous update. There were some edge cases where PHP 7 had issues and it didn’t work at all on PHP 5.6. We have now changed it so that the PHP 8 compatibility changes will now only affect PHP 8 and will be more thorough… 

Plugin Requirements

MasterPress requires a minimum of WordPress version 4.9, MySQL 5.6, and PHP version 5.6.20.

We also recommend that PHP is configured to use a memory limit of 64MB per request (128MB may be required for sites with higher complexity).

This plug-in is not compatible with the WordPress.com hosted service.

Three AM