1

I'm trying to make a drop down list with PHP using a foreach loop to loop through the data. This works without a drop down when I was displaying the results in a table so the loop does work.

It seems the drop down list gets populated (as the list expands/contracts when I've added new fields in for testing) but no data is actually shown. I only need to show one field, though. Here's my code:

    <select name="language_select">
        <?php foreach($this->getContent('languages') as $language => $value) : ?>
            <option value="<?($language['name']);?>"></option>
        <?php endforeach ?>
    </select> 

So it fetches an array and tries to return the data, pretty simple. What am I doing wrong?

9
  • Is short PHP tag enabled on your server? Try changing <? to <?php and see if that helps. Commented Jul 31, 2013 at 16:34
  • @AmalMurali does it matter? Commented Jul 31, 2013 at 16:35
  • You need to echo the value Commented Jul 31, 2013 at 16:35
  • You have to echo the name within the tags as well as in the value like: <option value="<?($language['name']);?>"><?($language['name']);?></option> (are you echoing the variables?) Commented Jul 31, 2013 at 16:35
  • where's echo ? or print ? Commented Jul 31, 2013 at 16:35

2 Answers 2

1

I'm not to sure of the composition the return of $this->getContent('languages') but I think this is what you need.

<select name="language_select">
    <?php foreach($this->getContent('languages') as $language) : ?>
        <option value="<?= urlencode($language['name']);?>"><?= htmlspecialchars($language["name"]) ?></option>
    <?php endforeach ?>
</select> 
Sign up to request clarification or add additional context in comments.

6 Comments

This works! With one slight problem: it is displaying the id field (language_id) instead of the name field, as specified in the loop. Any suggestions?
Nothing gets returned. I've added in the 'languages_id' field to test what comes back, and now each field is duplicated i.e 0,0,1,1,2,2 etc.
I've viewed the array in Chrome, and each element in the array is matched correctly. I.e [0] is matched to [language_id]1, [name]English, [shortcode]en and so on. So I'm not sure why it is displaying just the ID field.
@CiaranReen Again what does print_r($this->getContent('languages')); show
If you cared to read my above comment I said 'nothing gets returned'.
|
0

Managed to get it working, with Orangepills help (thanks), with this:

<option value="<?= ($language['name']);?>"><?= ($language['name']) ?></option>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.