Jump to content

FieldtypeOptions divide in arrays


mel47
 Share

Recommended Posts

Hi

I'm trying to create a markup of a list of options in 3 differents columns. I'm not sure how to do it. For now, I'm able to display each title:

$field = wire('fields')->get($instru);
$instruments = $field->type->getOptions($field);


        foreach ($instruments as $inst) {
            $out .="<li><h5 class='title is-5'>{$inst->title}</h5></li>";
		}

But I want something like this. Does it make sense? Or what other strategy I should aim?

$field = wire('fields')->get($instru);
$instruments = $field->type->getOptions($field);

$firstCol = Options 1 to 5  // how to do?
$secCol = Options 6 to 10  // how to do?
$thirdCol = Options 11 to 15  // how to do?
$array = [firstcol, seccol, thirdcol]

        foreach ($array as $col) {
            $out .="<div class="column">";
              foreach ( ?? ) {
              $out .="<li>title</li>";
			}
		}

Thanks!

Link to comment
Share on other sites

Hi,

i think this is more a php question than a pw one, then i'll answer in my savage php way ?
let's assume $intruments is an array and you don't know how many intruments there are, you don't need those first, second col and so on, just iterate $isntruments
 

$total  = count($instruments);
$colmax = ceil($total / 3);
$i = 1;
$out = '<div class="column">' . "\n"; // or .= if $out is initialised above
foreach ($instruments as $inst) {
    $out .= '<p>' . $inst . "</p>\n";

    if ( $i == $total ) {
        $out .= "</div>\n";
    }
    elseif ($i < $total && $i % $colmax == 0) {
        $out .= "</div>\n";
        $out .= '<div class="column">' . "\n";
    }
    $i++;
}
echo $out; // or return if in a function

should do the trick, no matter $instruments contains 15, 17 or 200... instruments ?

have a nice day

  • Like 1
Link to comment
Share on other sites

hi @elabx you're completely right, and following you on the elaboration path, my savage little script is just the php way of doing what @mel47 was trying to write with any kind of array and number of columns but, honestly, nowaday, if i had to do this, as much as i love php i would probably simply iterate the array in the template inside one div/container and use css to generate the columns... ?

have a nice day

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...