zendframework中的数据提交

时间:2010-02-25 10:49:27

标签: zend-framework

在我的zend项目中,我想显示一些健康问题以及下拉框和text-areaa。我的控制器和视图如下所示。

class IndexController extends Zend_Controller_Action

{

public function init()
{
    /* Initialize action controller here */
}

public function indexAction()
{
    $healthproblems =new Model_DbTable_Healthproblems();
    $this->view->healthproblems=$healthproblems->fetchAll();
}

public function addAction()
{

}

}

- index.phtml -

<table border='1'>
<tr>
    <th>Name</th>       
    <th>&nbsp;</th>
    <th>&nbsp;</th>
</tr>
<?php foreach($this->healthproblems as $prob) : ?>
<tr>
    <td><?php echo $this->escape($prob->healthproblem_name);?></td>
    <td><select id="ddl_<?php echo $prob->prob_id; ?>">
            <option selected="selected">--Select--</option>
            <option>Yes</option>
            <option>No</option>
        </select></td>
    <td><input type="text" style="width: 50px" value=""></input></td>

</tr>

<?php endforeach; ?>
<tr><td colspan="3" align="center">
<input type="submit" id="submit" value="Submit" ></input></td></tr>

我的问题是'如何将这些数据添加到数据库中?' problemid和note等字段。还有其他交替方式吗? HeaalthProblems包含在一个表中,我想将每个人的问题插入到另一个表中。请帮助我..

1 个答案:

答案 0 :(得分:0)

首先,我建议您使用Zend_Form,而不是从Scratch创建表单。

其次, 要解决您的问题,您的表单中包含person和problem id的person_id和problem_id字段(隐藏)。

在您的控制器中,您只需捕获数据,然后发送到您的模型(如下所述)。

然后,您需要使用insetPersonH​​ealthProblem($ data)方法创建一个名为PersonProblem的新模型。哪个将从控制器接收数据。

到目前为止,你的$ data数组中会有这样的东西:

array(
'person_id' => 1,
'problem_id' => 15,
'hasproblem' => ', // 1 for yes and 0 for no
'note' => 'something'
);

因此,您的字段需要被称为“hasproblem”,而不是在字段名称中连接问题ID,您将拥有一个隐藏字段。

最后在insetPersonH​​ealthProblem方法中你将插入关系,你将完成类似的事情:

id    |    person_id    |    problem_id   |   hasproblem   |   note   

1              1                 15                1          something

您的表单将如下所示:

<form method="POST" action="url('......')">
<input type="hidden" name="person_id" value="<?php echo $person_id; ?>" />
<input type="hidden" name="person_id" value="<?php echo $problem_id; ?>" />
<table border='1'>
<tr>
    <th>Name</th>       
    <th>&nbsp;</th>
    <th>&nbsp;</th>
</tr>
<?php foreach($this->healthproblems as $prob) : ?>
<tr>
    <td><?php echo $this->escape($prob->healthproblem_name);?></td>
    <td><select id="hasproblem">
            <option selected="selected">--Select--</option>
            <option>Yes</option>
            <option>No</option>
        </select></td>
    <td><input type="text" name="note" style="width: 50px" value=""></input></td>

</tr>

<?php endforeach; ?>
<tr><td colspan="3" align="center">
<input type="submit" id="submit" value="Submit" ></input></td></tr>
</form>

希望这对你有所帮助。