2012-04-20 102 views
0

您好我正在使用zend表單multicheckbox。Zend Form Multicheck dynamic

我有一個$數組,其中我有一個'id''name''姓氏''地址'和'城市'的用戶列表。 我需要創建一個複選框中,我可以選擇beetwen名+姓+加+城市和返回CONTROLER名字和姓氏選擇的ID ...

這裏是我的形式:

class Application_Form_MultiplaSelezione extends Zend_Form 
{ 
public function init() 
{ 
    /* Form Elements & Other Definitions Here ... */ 
} 

public function selezione($array){ 
    $this->setMethod('post');  
    $count=count($array);  
    $multipla=new Zend_Form_Element_MultiCheckbox('scelta');   
    for($i=0;i<$count;$i++){    
     foreach ($array[$i] as $chiave=>$valore){ 
      if($chiave=='idnomeutente'){ 
       $nomeutente=$valore; 
      } 
      if($chiave=='nome'){ 
       $nome=$valore; 
      } 
      if($chiave=='cognome'){ 
       $cognome=$valore; 
      } 
      if($chiave=='indirizzo'){ 
       $indirizzo=$valore; 
      } 
      if($chiave=='residenza'){ 
       $residenza=$valore; 
      } 

     } 
     $val=$nome.' '.$cognome.' '.$indirizzo.' '.$residenza; 
     $multipla->addMultiOption($nomeutente, $val); 


     if($i==0){ 
      $iduser=$nomeutente; 
     } 
    } 

    $multipla->setValue($iduser); 

    $submit= new Zend_Form_Element_Submit('submit'); 
    $submit->setLabel('Seleziona'); 

    $this->addElements(array($multipla,$submit));   
} 
    } 

爲什麼不起作用?

+0

你的代碼不清楚,你的期望輸出到底是什麼? '$ array'包含什麼? 「返回控制器所選名稱和姓氏的ID」是什麼意思? – Liyali 2012-04-20 15:04:21

+0

另外,「爲什麼它不起作用」是向占卜者提問的好問題,在這裏,大多數人可以首先在閱讀錯誤時回答問題。什麼不行? – Liyali 2012-04-20 15:31:11

+0

我寫在數組中有一個id姓名地址和城市的用戶列表。我想擁有一個包含所有元素的多重元素,並且我想選擇它的ID。我說這是行不通的,因爲如果我試圖看到這個頁面沒有任何東西出現,並且瀏覽器繼續加載某些東西...... – Simone 2012-04-20 16:44:47

回答

0

嘗試閱讀和理解:

<?php 

class Application_Form_MultiplaSelezione extends Zend_Form 
{ 
public function init() 
{ 
    $this->setMethod('post'); 

    $multipla=new Zend_Form_Element_MultiCheckbox('scelta'); 
    $multipla->setMultiOptions($this->getOptionsToMultipla()); 

    /* some other fields */ 

    $this->addElements(array(
     $multipla, 
     /* others fields */ 
     )); 
} 

public function getOptionsToMultipla() 
{ 
    /* there you can use static or dinamic method which: 
     1. select data from table 
     2. in foreach remake rows to your form, and add it to array ($array[] = "your string";) 
     3. return $array; 
     TIP: you should make other array where you have id of the rows, with it you can decode the request (compare index given from form) 
     (if you have problem with it I can help you in other thread :)*/ 

    /* for example of static method */ 
    return Your_Model::getOptionsToMultipla(); 
} 

要將此代碼,您必須在您的模型和控制器添加一些變化。祝你好運:)

+0

我弄清楚什麼是瀏覽器無法正常工作的問題。但是現在我有一個奇怪的問題。達到數組中信息的代碼(用我的代碼表示)不起作用。這很奇怪,因爲我在控制器的操作之前使用了與相同數組相同的代碼。我現在使用它的形式,我認爲是相同的,但在代碼不工作的形式....瀏覽器日誌是:警告:無效的參數爲foreach()提供...如果你認爲這是更好,我會打開另一個帖子.. – Simone 2012-04-21 17:27:49

+0

嘗試改變它http://pastebin.com/YTNXQgJb爲它:http://pastebin.com/3wfa7BdR如果$ valore.cognom給出一個錯誤嘗試改變它在$ valore ['cognom'] – 2012-04-21 17:47:15

+0

我發現問題是另一個...我通過行動之間的數組$ this - > _ helper-> redirector('action','controller',NULL,array('array'=> $陣列));重定向似乎不太好... – Simone 2012-04-21 17:57:32