开发者

data submission in zendframework

开发者 https://www.devze.com 2022-12-21 09:41 出处:网络
In my zend project ,I want to show some health problems along-with a drop-down box and a text-area.My Controller and views are given below.

In my zend project ,I want to show some health problems along-with a drop-down box and a text-area.My Controller and views are given below.

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>

My problem is 'How to add these data into database?' fields such as prob开发者_StackOverflow社区lemid and note.Any other alternating way is possible? HeaalthProblems containing in one table and i want to insert each individual person's problems into another table.Please help me..


First I will recommend you to use Zend_Form instead of create forms from Scratch.

Second, to solve your problem you'd have an person_id and problem_id field (hidden) with the person and problem id in your form.

In your controller you will just catch the data and then send to your model (described bellow).

Then you need to create a new model called PersonProblem with insetPersonHealthProblem($data) method. Which will receive the data from controller.

Until now you will have something like this in your $data array:

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

So your field need to be called "hasproblem" and instead of concatenate the problem id in the field name you'll have a hidden field.

Finally in in the insetPersonHealthProblem method you'll insert the relationship, and you will finish with something like that:

id    |    person_id    |    problem_id   |   hasproblem   |   note   

1              1                 15                1          something

Your form will look like this:

<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>

Hope this help you.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号