Jump to content

How to output text input in a repeater field


Davabo
 Share

Recommended Posts

Hey guys,

I'm currently building a shop page for a friend and im creating all the options via the panel. So when he wants to add a new item to his shop, he goes to the specific page and adds all the information in via the panel.

I have everything working perfectly, mostly using checkbox field types for available sizes etc....

He has asked me now to give each product a colour item, but the catch is that he wants to be able to type in the colour himself. As you know many products come in different colours so i cant create a selection from him to choose from a list of colours like i have done with sizes.

So I built a repeater field with a textarea inside.

Co7Cldt.png

My question is how do i output these inputs on the page in a select box?

Here is my code so far, 

  <?php foreach($page->colour as $colour) {
                    echo "<select id ='colour'>
                          <option value='Select Colour' selected='selected' disabled>Select Colour</option>
                          <option>$colour->colourtext</option>
 
                          </select>";
                    } 
                ?>  
 
 
The problem with the code above is that it creates a seperate selectbox for each colour input. I have tried a few different things like eq(1n), first() etc... and nothing works. I would really appreciate some help.
 
Thank you!
Link to comment
Share on other sites

// You don't need to loop the select

$options = "<option value='Select Colour' selected='selected' disabled>Select Colour</option>";

foreach($page->colour as $colour) {

$value = $colour->colourtext;

$options .= "<option value='" . $value . "'>" . $value . "</option>";

}

echo "<select id='colour'>" . $options . "</select>";

ps, could have a bug or 2 typed in browser.
  • Like 3
Link to comment
Share on other sites

Hi Davabo,

the problem seems to be your generated HTML code, not the PHP code. You need to create one single select with a dynamic list of options. Try this:

<select id="colour">
  <option value="-" selected="selected" disabled="disabled">Select Colour</option>
  <?php foreach($page->colour as $colour): ?>
    <option value="<?= $colour->colourtext ?>"><?= $colour->colourtext ?></option>
  <?php endforeach; ?>
</select>
  • Like 4
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...