1

How to create a select - option in Zend Framework +MySQL Table

COntroller:

$persons = new Application_Model_DbTable_Persons();
$data_persons = $persons->findPersons();

Controller and View? How to? o.O

Jhosman
  • 555
  • 2
  • 8
  • 20

2 Answers2

0

In Controller

   $this->view->dataPerson = $data_persons;

View Html.

  foreach($this->dataPerson as $datPers):
               //Example Get person name.
          echo $this->escape($datPers->name);
           and etc.

 endforeach;
Mubo
  • 1,078
  • 8
  • 16
  • =( Notice: Trying to get property of non-object in /home/namsohj/Dropbox/Proyecto de Grado/Desarrollo/Asuntos_Estudiantiles/application/views/scripts/beneficiarios/insertar.phtml on line 6 Notice: Trying to get property of non-object in /home/namsohj/Dropbox/Proyecto de Grado/Desarrollo/Asuntos_Estudiantiles/application/views/scripts/beneficiarios/insertar.phtml on line 6 You can help me with remote acces? – Jhosman Dec 24 '13 at 18:19
  • Go through this Example so that you can understand what you are doing. It exactly explains what you need to do. http://akrabat.com/wp-content/uploads/Getting-Started-with-Zend-Framework.pdf – Mubo Dec 24 '13 at 18:36
  • The example not include "select" combobox – Jhosman Dec 25 '13 at 19:57
0

The options to the Select box can be added as an array using addMultiOptions() function

$persons = new Application_Model_DbTable_Persons();
$data_persons = $persons->findPersons();
$data_persons_array = array();

foreach($this->dataPerson as $datPers)
    $data_persons_array[$datPers->id] =  $datPers->name


$name = new Zend_Form_Element_Text('name');
$name->setLabel('name')
        ->setRequired(true)
        ->addValidator('NotEmpty');
        ->addMultiOptions($data_persons_array); //Will add the options to select box
Nandakumar V
  • 4,317
  • 4
  • 27
  • 47
  • All this is in the controller? and the line ($name = new Zend_Form_Element_Text('name');) print error, I must include a library? – Jhosman Dec 27 '13 at 13:01
  • It should be added to the php file where you are creating the form and form elements. If you are following the `Getting-Started-with-Zend-Framework.pdf` by Akrabat... you can put it in `forms/Album.php` – Nandakumar V Dec 28 '13 at 04:31