0

这是我动态生成的表单,允许用户输入学生的分数。我的挑战是如何捕获特定的 subjectCode 及其分数,因为我使用 subjectcode 存储在数据库中。

<?php
$attributes=array('class'=>'','id'=>'');
echo form_open('scoreInsertion/insertScores',$attributes);
echo "<table>";
echo "<tr>";
echo "<td>";?>
<label for="admNo>">Admission Number<span class="required">*</span></label>
<?php echo form_error('admNo');
echo "</td>";
echo "<td>";?>
<input type="text" name="admNo" id="admNo"  value="<?php echo set_value('admNo')?>"   />
<?php echo "</tr>";
echo "<tr>";
echo "<td>";    
?>
<label for="studentName>">Student Name<span class="required">*</span></label>
<?php echo form_error('studentName');
echo "</td>";
echo "<td>";?>
<input type="text" name="studentName" id="studentName"  value="<?php echo $studentName?>"   />
<?php echo "</tr>";
echo "<tr></tr>";
echo "<tr>";
echo "<td>";
?>
<label for="examCategory">Exam Category<span class="required">*</span></label>
<!--<tr><td><?php echo form_error('examCategory')?></td></tr>-->
<td>
<select name="examCategory">
<option value="">--Select Category--</option>
<?php
print_r($exams);
if(isset($exams) && is_array($exams)){
    foreach($exams as $exam){
        $examId=$exam->id;
        $examName=$exam->category;
        echo "<option value='$examId'>$examName</option>";
    }
}
?>
</select></td><?php
echo "<tr></tr>";
echo "<tr>";
echo "<td>";
?>
<label for="term">Academic Term<span class="required">*</span></label>
<!--<tr><td><?php echo form_error('examCategory')?></td></tr>-->
<td>
<select name="term">
<option value="">--Academic Term--</option>
<?php
if(isset($terms) && is_array($terms)){
    foreach($terms as $term){
        $termId=$term->termId;
        $termName=$term->name;
        echo "<option value='$termId'>$termName</option>";
    }
}
?>
</select></td>

<?php
echo "<tr>";
//loop through the subjects array and display them on the view 
if(isset($subjects) && is_array($subjects)){
    foreach($subjects as $subject){
        $subjectName=$subject->subName;
        $subjectCode=$subject->subCode;
        echo "<tr>";
        echo "<tr></tr>";
        echo "<td>";?>
        <label for="<?php echo $subjectName?>"><?php echo $subjectName;?><span class="required">*</span></label>
        <?php echo form_error($subjectName);
        echo "</td>";
        echo "<td>";?>
        <input type="text" name="<?php echo $subjectCode?>" id="<?php echo $subjectName?>"  value="<?php echo set_value($subjectCode)?>"   />
        <?php echo "</tr>";
        }   
}
?>
<?php echo "<tr>";echo "<td>";
echo form_submit('submit','Submit'); 
echo "</td>";
echo "</tr>";
?>
</p>
<?php
echo "</table>";
echo form_close();
?>

用于处理上述 POST 详细信息的 PHP 脚本如下:

function insertScores(){
        $admNo=$this->input->post('admNo');
        $term=$this->input->post('term');
        //get the post variables from the Form
        $posted = $this->input->post();
        $x = array_keys($posted);
        foreach($x as $y) {
            echo $y ." = ". $_POST[$y]."<br/>";
            echo "<br>";
            //write the sql 
            $form_data=array(
            'admNo' =>$admNo,
            'subCode'=>$y,
            'termId'=>$term,
            'score'=>$_POST[$y]                     
            );
            print_r($form_data);
            $this->SaveForm($form_data);
        }
}
4

2 回答 2

0

如果确实如此,您可以为name属性设置一个特定的前缀,然后在您的键中查找以该前缀开头的$_POST键。像这样的东西:

<input type="text"
       name="subject_<?php echo $subjectCode?>"
       id="<?php echo $subjectName?>"
       value="<?php echo set_value($subjectCode)?>"   />

$_POST然后在数组 中搜索它。

$subjectCode = null;
foreach (array_keys($_POST) as $index) {
    if (strpos($index, 'subject_') === 0) { // you found the needed element            
        // get its value and save it in a variable
        $subjectCode = $_POST[$index];
        break;
    }
}
于 2013-03-29T16:45:15.777 回答
0

这段代码解决了我的问题,我在数据库中获取subCode使用admNo然后与POST名称进行比较。

$subjects=$this->subjectModel->get_subjects();
foreach($subjects as $subject){
    $subCode= $subject->subCode;
    foreach($_POST as $key => $value){
        //print_r($key);
        //print_r($value);
        $form_data=array();
        if($key == $subCode){
            //echo $subCode. "=". $value;
            //write the sql for insertion into the database
            $form_data = array(
            'admNo'=>$admNo,
            'subCode'=>$subCode,
            'termId'=>$term,
            'formId'=>$formstudy,
            'score'=>$value,
            'year'=>$year    


            );
            $this->SaveForm($form_data);    

        }
    }
}
于 2013-03-30T13:09:43.617 回答