0

I currently am trying the following code:

 $sql = $db->query("select id,username,fullname from userss ORDER BY fullname ASC");
        while($row = $sql->fetch()) {
            $username[] = $row['username'];
        }
        $form = new Zend_Form();
        $form->setAction('/index/change')
             ->setMethod('post');

        $element = new Zend_Form_Element_Select('foo', array(
            'multiOptions' => array(
                $username,
                )));
        $form->addElement($element);
        echo $form;

The desired result is the <select> form showing the elements <option value='username'>Full Name</option> where username is username from SQL and Full Name is fullname from sql.

Right now, it shows <option value='0'>Full Name</option>

I'm not quite sure how to specify the first part of the array, so that it will essentially be username => Full Name

Any suggestions? Thanks!

2 Answers 2

3

To set the value of an option in a Zend_Form_Element_Select, the array key defines the value, and the array value defines the text in the select that the user sees.

Try this:

$element = new Zend_Form_Element_Select('foo', array(
    'multiOptions' => array(
        $username  => $fullname,
        $username2 => $fullname2,
)));

You were seeing 0 since the array didn't have string keys, it defaulted to use the numerically indexed array keys as values.

Given your code, you would do this:

$username = array();

while($row = $sql->fetch()) {
    $username[$row['username']] = $row['fullname'];
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the response! I'm giving this a shot, but a bit confused what you meant in the first bit of code. Where did variables $fullname and $username2 and $fullname2 come from? The only variable I have is $username as defined in the while loop.
It was just an example of what the array might look like if you had multiple items (setting key => value pairs for the select box). You can just replace your loop with the one I posted and keep 'multiOptions' => array($username) as is; or use beginner's answer as fetchPairs is a good solution as well if you don't need the user id for anything.
Ahhh, thank you. The issue was actually that I simplified my query in this post (fullname isn't an actual column...I needed to concat two tables...and thus the $username variable was still getting a 0)
2

To make populating select element easier ZF provides fetchPairs

     $sql = "select username,fullname from userss ORDER BY fullname ASC";
      $db =  Zend_Db_Table::getDefaultAdapter();
     $select = new Zend_Form_Element_Select('foo');
     $select->setMultiOptions($db->fetchPairs($sql));

This way saves you from iterating data (for, while loops) yourself.

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.