2010-02-25 14 views
0

在我的zend项目中,我想要展示一些健康问题 - 一个下拉框和一个text-area.My控制器和视图如下。zendframework中的数据提交

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> 

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

回答

0

首先,我会推荐您使用Zend_Form而不是从头开始创建表单。

第二, 为了解决您的问题,您需要在表单中为人员和问题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> 

希望这有助于您。