Jump to content


Photo

Easiest way for clients to add select options in form

form select

  • Please log in to reply
5 replies to this topic

#1 onjegolders

onjegolders

    Hero Member

  • Members
  • PipPipPipPipPip
  • 804 posts
  • 211

  • LocationMidlands, UK

Posted 09 April 2012 - 11:45 AM

Scratching my head here. I'm trying to find a simple way for the client to be able to add options to a select tag in a form.
Do I really have to use pages here?
I can use a textarea - convert new lines to <li> tags but that won't cut it for <option> tags.
Repeater field? If that is the best option could anyone tell me how I would output the results as <li> tags?

Ideally it would be a fieldtype where each drop-down was some sort of item that I could loop in <option> tags.

Thanks

Edit: I've come up with this code for a repeater file and it seems to be doing the trick.

<?php
echo "<ul>";
foreach ($page->subject_options as $item) {
  echo "<li>$item->subject_option</li>";
}
echo "</ul>";
?>

I would be interested though if there was a faster or 'cleaner' way of doing this.

Edited by onjegolders, 09 April 2012 - 12:06 PM.


#2 Soma

Soma

    Hero Member

  • Moderators
  • 3,205 posts
  • 1757

  • LocationSH, Switzerland

Posted 09 April 2012 - 12:16 PM

Pages are great for this. A recent new feature allows you to add pages in a page select, you can enable it in the fields input settings.

Using pages you could setup a section for the user to add pages used as options later, and even sort them in the tree. No need to even have a select page field on the page.

You could also just use that "section" and it's childs to output the options.


echo "<select name='myoptions'>";
foreach($pages->get("/myoptions/")->children() as $option){
    echo "<option value='$option->name'>$option->title</option>";
}
echo "</select>";


or if through a page select field "select_options" on page it would be


echo "<select name='myoptions'>";
foreach($pages->selet_options as $option){
    echo "<option value='$option->name'>$option->title</option>";
}
echo "</select>";


Other ways would be to have a textarea and each new line render as an option. Then the code would be like this:

in optionstext field:

Option1
Option2
Option3

echo "<select name='myoptions'>";
foreach(explode("\n",$page->optionstext) as $option){
    echo "<option value='$option'>$option</option>";
}
echo "</select>";

@somartist | modules created | support me, flattr my work flattr.com


#3 diogo

diogo

    Hero Member

  • Moderators
  • 2,008 posts
  • 1089

  • LocationPorto, Portugal

Posted 09 April 2012 - 12:28 PM

Again late :)
I buit a function for the newlines solution:


function newlinesToOption($field){
  $select = "<select>";
  foreach(explode("\n", $field) as $line){
	$line = trim($line);
	$lowerLine = strtolower($line);
	$select .= "<option value='$lowerLine'>$line</option>";
  }
  $select .= "</select>";
  return $select;
}
echo newlinesToOption($page->yourField);


#4 onjegolders

onjegolders

    Hero Member

  • Members
  • PipPipPipPipPip
  • 804 posts
  • 211

  • LocationMidlands, UK

Posted 09 April 2012 - 12:37 PM

Wow thanks both of you!


Again late :)
I buit a function for the newlines solution:


function newlinesToOption($field){
  $select = "<select>";
  foreach(explode("\n", $field) as $line){
	$line = trim($line);
	$lowerLine = strtolower($line);
	$select .= "<option value='$lowerLine'>$line</option>";
  }
  $select .= "</select>";
  return $select;
}
echo newlinesToOption($page->yourField);


Diogo, is this code that I can copy and paste into a new module?

Pages are great for this. A recent new feature allows you to add pages in a page select, you can enable it in the fields input settings.

Using pages you could setup a section for the user to add pages used as options later, and even sort them in the tree. No need to even have a select page field on the page.

You could also just use that "section" and it's childs to output the options.


echo "<select name='myoptions'>";
foreach($pages->get("/myoptions/")->children() as $option){
	echo "<option value='$option->name'>$option->title</option>";
}
echo "</select>";


or if through a page select field "select_options" on page it would be


echo "<select name='myoptions'>";
foreach($pages->selet_options as $option){
	echo "<option value='$option->name'>$option->title</option>";
}
echo "</select>";


Other ways would be to have a textarea and each new line render as an option. Then the code would be like this:

in optionstext field:

Option1
Option2
Option3

echo "<select name='myoptions'>";
foreach(explode("\n",$page->optionstext) as $option){
	echo "<option value='$option'>$option</option>";
}
echo "</select>";


Soma, thanks, I do really want to use native PW functionality, I am just finding it really hard to get my head around the workflow in setting it all up.
Create template, add fields, disable editing of parent - I know it's like this at the beginning for everybody probably, but my head's in a spin!

There's lots of great documentation in the forums but the one thing I think is lacking is a real walkthrough - as much to see seasoned users' workflows as the code itself.

#5 diogo

diogo

    Hero Member

  • Moderators
  • 2,008 posts
  • 1089

  • LocationPorto, Portugal

Posted 09 April 2012 - 12:43 PM

It makes more sense to keep the select tags outside the function, so you can control the attributes
new code goes like this:
function newLinesToOptions($field){
  $options = "";
  foreach(explode("\n", $field) as $line){
   $line = trim($line);
   $lowerLine = strtolower($line);
   $options .= "\t<option value='$lowerLine'>$line</option>\n";
  }
  return $options;
}?>

<select name="name">
<?php echo newLinesToOptions($page->yourField); ?>
</select>

edit:

Diogo, is this code that I can copy and paste into a new module?


No, this is only a normal function. If you have to use it more then once, put the function on an include file and call it with this part of the code:
<select name="name">
<?php echo newLinesToOptions($page->yourField); ?>
</select>


#6 onjegolders

onjegolders

    Hero Member

  • Members
  • PipPipPipPipPip
  • 804 posts
  • 211

  • LocationMidlands, UK

Posted 09 April 2012 - 12:55 PM

It makes more sense to keep the select tags outside the function, so you can control the attributes
new code goes like this:

function newLinesToOptions($field){
  $options = "";
  foreach(explode("\n", $field) as $line){
   $line = trim($line);
   $lowerLine = strtolower($line);
   $options .= "\t<option value='$lowerLine'>$line</option>\n";
  }
  return $options;
}?>

<select name="name">
<?php echo newLinesToOptions($page->yourField); ?>
</select>

edit:


No, this is only a normal function. If you have to use it more then once, put the function on an include file, and call it with this part of the code:
<select name="name">
<?php echo newLinesToOptions($page->yourField); ?>
</select>


OK thanks.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users