我想知道是否有人可以启发我。我的控制器函数接受表单输入请求,并通过模型函数将其保存到数据库。我的代码如下。
我尝试了几种方法,但看起来我被困住了;下面是我的控制器。
function submit_result()
{
if($this->input->post('Name') && $this->input->post('Class') &&
$this->input->post('Subject') && $this->input->post('grade_value') && $this->input->post('teacher_value'))
{
$this->load->model('class_model');
$this->class_model->submit_grade($this->input->post('Name'), $this->input->post('Class'), $this->input->post('Subject'), $this->input->post('grade_value'), $this->input->post('teacher_value'), $this->input->post('comment_value'));
redirect(base_url().'Grader/search2/');
}
}
}
?>
这里的模型看起来像
function submit_grade($name, $class, $subject, $result, $teacher, $comment)
{
$sql = "INSERT INTO `kepsek_ifa`.`mon_ifa_result`
(`id`, `Name`, `Class`, `Subject`, `Result`, `Teacher`, `Comment`)
VALUES
(NULL," . $this->db->escape($name) . "," . $this->db->escape($class) . ","
. $this->db->escape($subject) . ", '$result' ,"
. $this->db->escape($teacher) . "," . $this->db->escape($comment) . ")";
$query = $this->db->query($sql);
}
}
在我看来我有这个
</head>
<body>
<div id="container">
<h1><?php echo $student_name?></h1>
<div id="body">
<p>
<?php $this->load->helper('form'); ?>
<?php if ( ! is_null($subject_result)): ?>
<?php if (count($subject_result)): ?>
<div style="padding-top: 20px">
<table style="border: 1px solid black">
<tr style="font-weight: bold"><td>CLASS</td><td>SUBJECT</td><td>RESULT</td><td>GRADER</td><td>COMMENT</td></tr>
<?php foreach ($subject_result as $result): ?>
<tr><td><?php echo $class ?></td><td><?php echo $result->Subject ?></td><td>
<?php $hidden = array('Name' => $student_name, 'Class' => $class, 'Subject' => $result->Subject);
echo form_open("Grader/submit_result", '', $hidden);
?>
<?php echo form_dropdown('grade_value', $grade_code); ?>
</td><td>
<?php echo form_dropdown('teacher_value', $teacher_data); ?>
</td><td>
<?php echo form_input('comment_value'); ?>
<?php echo form_submit('submit_result', 'Submit'); ?>
</td><td>
<?php echo form_close(); ?>
</td></tr>
<?php endforeach ?>
</table>
<p><a href="<?php echo base_url(). "Grader/search2" ?>"> HOME </a></p>
</div>
<?php else: ?>
<p><em>There are no results for your query.</em></p>
<?php endif ?>
<?php endif ?>
</p>
</div>
<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p>
</div>
我完全设置了视图,除了每行有一个提交按钮的部分,我想要的是一个提交按钮来保存我视图中的所有数据
我尝试了不同的组合,例如在我的视图中使用form_hidden但仍然失败。
问题
如何在我的视图中只有一个提交页面可以保存用户提交的所有记录? 有帮助吗?
答案 0 :(得分:0)
你可以这样做: 在您的视图文件中:
<?php
echo form_open("Grader/submit_result");
echo form_hidden('Name' , $student_name);
echo form_hidden('Class' , $class);
?>
<table style="border: 1px solid black">
<tr style="font-weight: bold"><td>CLASS</td><td>SUBJECT</td><td>RESULT</td><td>GRADER</td><td>COMMENT</td></tr>
<?php
form_hidden('username', 'johndoe');
foreach ($subject_result as $result): ?>
<tr><td><?php echo $class ?></td><td><?php echo $result->Subject ?></td><td>
<?php
echo form_hidden('Subject[]',$result->Subject);
?>
<?php echo form_dropdown('grade_value[]', $grade_code); ?>
</td><td>
<?php echo form_dropdown('teacher_value[]', $teacher_data); ?>
</td><td>
<?php echo form_input('comment_value'); ?>
<?php echo form_submit('submit_result', 'Submit'); ?>
</td><td>
</td></tr>
<?php endforeach ?>
</table>
<?php echo form_close(); ?>
在你的控制器循环中通过主题计数。
$c = count($this->input->post('Subject'));
for($i=0;$i<$c;$i++){
$subject = $this->input->post('Subject')[$i];
$grade_value = $this->input->post('grade_value')[$i];
$teacher_value = $this->input->post('teacher_value')[$i];
// insert code goes here.
}