2008年8月3日星期日

[fw-mvc] [SOLVED] RE: Populate drop-down list from database table

Thanks to everyone who replied (on & off-list)

With the replies I had, and a refreshed set of eyes, it was trivial.

In my model, I added a method of:

function getPairs()
{
$result = $this->getAdapter()->fetchPairs('SELECT id, description FROM my_table');
return $result;
}

Then in the form I did:

$mm = new my_model;
$pairs = $mm->getPairs();

$selector = new Zend_Form_Element_Select('selector');
$selector->setLabel('My Test Selector')
->setRequired(false)
->setMultiOptions($pairs);

GTG

________________________________________
From: Gordon Ross [gr306@ucs.cam.ac.uk]
Sent: 03 August 2008 00:11
To: fw-mvc@lists.zend.com
Subject: [fw-mvc] Populate drop-down list from database table

I'm new to Zend, and I'm trying to work out how to populate an HTML drop-down list from a SQL table easily (without having to write a bucket load of code)

Searching the list I found http://www.nabble.com/how-to-use-fetchPairs()-td17545498.html - but this talks about sloctables which I don't know about.

I also discovered http://www.zfforums.com/zend-framework-components-13/databases-20/how-do-i-fill-zend-form-select-db-769.html but this talks about fetchpairs, which appears to be something extra to standard PHP/Zend database APIs.

Is there an easy way to populate a drop-down list from a SQL table in Zend framework/PHP?

Thanks,

GTG

没有评论: