2010-02-25 77 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> 

希望這有助於您。