在Zend的另一个视图中显示表单数据

时间:2014-08-12 14:36:57

标签: zend-framework view controller zend-form

我是Zend的新手,我的问题对你来说很简单。我想制作一个控制器,在另一个视图中显示表单输入数据。数据是电子邮件文本和上传的文本文件。我创建了索引视图和结果视图。 但我一无所获。当我用文本替换$ email的值时,它的工作原理!我找不到出了什么问题。 控制器还应按名字

显示已排序的文件
Id,Firstname,Lastname
5,John,Doe
6,Adam,Ant
7,Victor,Hugo
8,Britannie,Spears

这是我的控制者:

public function indexAction()
    {
        // initialzing of the customized form
        $form = new Application_Form_Upload();

        $rq = $this->getRequest();

        $isForm = true; // the form has to be shown only if true

        if ($rq->isPost()) {
            if ($form->isValid($rq->getPost())) {
                // show the uploaded data instead of the form
                $isForm = false;
                $this->view->data = new Application_Model_DataViewer();
                $this->view->data->parseFromForm($form);

                $result = new Zend_View();
                $this->view->result= $result;
                $this->render('result');
            }
        }

        if ($isForm) {
            $this->view->form = $form;}
    }

这是我的模特:

class Application_Model_DataViewer
{
    /**
     * @var string Entered e-mail address
     */
    private $email;
    /**
     * @var array Array of extracted data from uploaded file
     */
    private $data;

    public function __construct(){
        $this->email=null;
        $this->data=array();
    }

    /**
     * Extracts the data from the form-object and saves it internally
     * @param $form Application_Form_Upload
     */
    public function parseFromForm($form){
        if(!isset($form))return;
        if(!isset($form->file)||!$form->file instanceof Zend_Form_Element_File){
            throw new Zend_Exception('The field File is empty or has wrong type');
        }

        // for validation of the IDs
        $ival = new Zend_Validate_Int();

        // reading the CSV-file (values should be separated by comma, if not - should be extended)
        if(($fp = @fopen($form->file->getFileName(), 'r')) !== false){
            while(($data = fgetcsv($fp, 500, ',')) !== false){
                if(
                    !is_array($data)
                    ||!$ival->isValid($data[0])
                    ||count($data)<3
                )continue;
                $this->data[$data[1]] = $data;
            }
        }else{return;}
        @fclose($fp);

        ksort($this->data);

        $this->email = $form->getValue('email');
    }

    /**
     * @return null|string
     */
    public function getEmail(){
        return $this->email;

    }

    /**
     * @return array
     */
    public function getData(){
        return $this->data;
    }

}

这是我的索引和结果视图

<?php 
// the form should only be rendered if form must be shown
if(isset($this->form)){
    $this->form->setAction($this->url());
    echo $this->form; 
}
?>

结果视图:

<?php 
// if the required data is submitted, it will be checked and displayed
if(isset($this->data)){
?>
<p>Thank you <strong><?php echo $this->escape($this->data->getEmail()); ?></strong>.</p>

<p>The result of the sorting is:
<?php
    foreach($this->data->getData() as $row){
        echo '<div>', $this->escape($row[0]), ',',
            $this->escape($row[1]), ',',
            $this->escape($row[2]), '</div>';
    }
?></p><?php
}

?>

1 个答案:

答案 0 :(得分:0)

在视图中使用部分帮助器。

请参阅:http://framework.zend.com/manual/2.2/en/modules/zend.view.helpers.partial.html

例如。你的index.phtml

if ($this->data) {
    echo $this->partial("result.phtml", array("data" => $this->data->getData()));
}

您可以通过 $ this-&gt; data

访问result.phtml中的变量
相关问题