2013-03-29 2 views
0

이것은 사용자가 students.my의 점수를 입력 할 수있는 동적으로 생성 된 양식입니다. 특정 subjectCode와 그 점수를 캡처하는 방법은 subjectcode를 사용하여 저장하므로 데이터베이스에. 위의 POST 세부 사항을 처리하는동적으로 생성 된 폼의 POST 값을 PHP에서 가져 오는 방법

<?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(); 
?> 

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); 
     } 
} 
+0

같은 파일의 모든 내용은 다음과 같습니다. 왜냐하면 함수 앞뒤에 열기 태그 ('')가 없기 때문입니다. 그리고이 함수는 닫는'}'도 없습니다. –

+0

['heredoc'] (http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc)를 사용해야합니다. 형식 지정 출력에 도움이됩니다. – Kermit

답변

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; 
    } 
} 
+0

양식과 스크립트가 다릅니다. – samlebo

+0

그건 중요하지 않아야합니다. – Havelock

0

, 나는 subCode은 다음 POST 이름과 비교 DB에 admNo를 사용하여 가져옵니다.

$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);  

     } 
    } 
} 
관련 문제