2013-02-07 2 views
0

다음은 제 코드입니다. 첫 번째 부분은 완벽하게 작동하지만 두 번째 루프는 결과를 생성하지 않습니다. 이것이하는 일은 시간표를 찾아 그 클래스들을 가져 와서 모든 것을 복사하고 동일한 데이터이지만 다른 이름으로 새로운 시간표를 만듭니다.PHP for 루프가 다른 for 루프 내부에서 제대로 작동하지 않습니까?

다른 for 루프는 학생을 시간 표의 클래스에 추가하는 것입니다. 내가 지금 5 일 동안 그것을 위해 벽에 머리를 치고 있었던 것에 따라 어떤 사람은 충분히 친절하고, 이것에서 나를 도와 준다.

미리 감사드립니다. 코드 :

<?php 
$Q = "INSERT INTO time_table(name, term, year) VALUES 
       ('".$name."', '".$term."', '".$year."')"; 
$res = $db->query($Q); 
//for generating the max table id 
     $sql2 = "select MAX(table_id) as table_id 
     from time_table 
     "; 
     $res2 = $db->query($sql2); 
     $count2 = $res2->num_rows; 
     $row2 = $res2->fetch_assoc(); 

     $table_id = $row2['table_id']; 
$Q = "SELECT class_id as tcid, day as d, teacher_id as tei, location as l 
FROM class_time_table 
WHERE term='".$copy."'"; 
$res = $db->query($Q); 
$num_results = $res->num_rows; 
     for ($i = 0; $i <$num_results; $i++) { 
     $row = $res->fetch_assoc(); 
      $Q4 = "SELECT * FROM students_class WHERE class_id = '".$row['tcid']."' and term = '".$copy."'"; 
      $res4 = $db->query($Q4); 
      $row2 = $res4->fetch_assoc(); 
      //for generating the max table id 
      $class_sysq = "select MAX(class_sys_id) as class_sys_id 
      from students_class 
      "; 
      $class_sysr = $db->query($class_sysq); 
      $count_class_sys = $class_sysr->num_rows; 
      $class_row = $class_sysr->fetch_assoc(); 

      $class_sys_idf = $class_row['class_sys_id']+1; 
      $Q5 = "INSERT INTO students_class (class_sys_id, teachers_id, location, max_students, class_term_fee, class_name, class_sub_name, term, year) VALUES ('".$class_sys_idf."', '".$row2['teachers_id']."', '".$row2['location']."', '".$row2['max_students']."', '".$row2['class_term_fee']."', '".$row2['class_name']."', '".$row2['class_sub_name']."', '".$term."', '".$year."')"; 
      $res5 = $db->query($Q5); 
      //for generating the max table id 
      $max_c_id = "select MAX(class_id) as ci 
      from students_class 
      "; 
      $r_mci = $db->query($max_c_id); 
      $count_class_sys = $r_mci->num_rows; 
      $mci_row = $r_mci->fetch_assoc(); 
      $max_c_idf = $mci_row['ci']; 

      $query2 = "INSERT INTO class_time_table(class_id, teacher_id, table_id, location, day, term, year) VALUES 
       ('".$max_c_idf."', '".$row['tei']."', '".$table_id."', '".$row['l']."', '".$row['d']."', '".$term."', '".$year."')"; 
      $result2 = $db->query($query2); 

     $student_q = "SELECT students.first_name as fn, students.last_name as ln, students.email as e, students.mobile_phone as mp, students.home_phone as hp, students.gender as g, students.dob as dob, students.term_fee as tf, students.join_date as jd, students.date_added as da, student_attending_class.class_id as ci FROM students, student_attending_class, class_time_table where students.student_sys_id = student_attending_class.student_id and student_attending_class.class_id = class_time_table.class_id and class_time_table.class_id = '".$row['tcid']."'"; 
      $student_res = $db->query($student_q); 
      $student_num_results = $student_res->num_rows; 
      for ($i = 0; $i < $student_num_results; $i++) { 
      $theRow = $student_res->fetch_assoc(); 

      //for generating the new system id 
      $sql3 = "select MAX(student_sys_id) as ssi 
      from students"; 
      $res3 = $db->query($sql3); 
      $count3 = $res3->num_rows; 
      $row8 = $res3->fetch_assoc(); 
      $student_system_num = $row8['ssi']+1; 

      $query10 = "INSERT INTO students(student_sys_id, first_name, last_name, email, mobile_phone, home_phone, gender, dob, fee_due, registration_fee, term_fee, fee_paid, join_date, date_added) VALUES 
       ('".$student_system_num."', '".$theRow['fn']."', '".$theRow['ln']."', '".$theRow['e']."', '".$theRow['mp']."', '".$theRow['hp']."', '".$theRow['g']."', '".$theRow['dob']."', '".$theRow['tf']."', 0, '".$theRow['tf']."', 0, '".$theRow['jd']."', '".$theRow['da']."')"; 
      $result10 = $db->query($query10); 
      $query11 = "INSERT INTO student_attending_class(class_id, student_id, waiting_list) VALUES ('".$max_c_idf."', '".$student_system_num."', '0')"; 
      $result11 = $db->query($query11); 

      } 

     } 


?> 
+0

을 어떤 값 $ student_res-> NUM_ROWS/$ student_num_results 얻을 루프 동안합니까? 심지어 0보다 높습니까? –

+0

친구에게 물어 줘서 고마워. 예, 약 700 건. – olbanana

답변

1

예를 들어 두 번째 루프에서는 $ i를 사용하지 말고 $ n을 사용하십시오.

+0

좋은 지적은 가장 혼란 스럽습니다. –

+0

당신이 거의 다이아몬드에서 말한 것처럼 들리는데, 아직 시도하지는 않았지만 그 일을 할 것이라고 생각합니다. 논리적으로 말입니다. 고마워! – olbanana

+0

나는 여전히 그 시간을 시험해 보았다 ... :( – olbanana

0

모르겠지만 두 변수 모두에서 동일한 변수 $ i를 사용했습니다. 아마도 두 번째 루프가 작동하지 않기 때문일 수 있습니다. 두 번째 루프에서 다른 변수 $ j를 시도하십시오.

0

코드의 형식이 잘못되어 두 루프에 대해 동일한 변수를 사용하고 있다는 것을 놓치기 쉽습니다. 따라서 두 번째 루프가 시작될 때 첫 번째 루프는 두 번째 루프의 진행 과정을 잃어 버립니다. foreach()을 사용하거나 두 번째 루프에 다른 변수 이름을 사용하십시오.

0

동일한 변수 인 $i을 사용하는 중첩 된 루프가 있으므로 시간 초과가 발생하는 것일뿐입니다.

그래서 같은 두 번째 루프를 변경해보십시오 :

for($j = 0; $j < $student_num_results; $j++){ 
    ... 
} 
+0

나는 그것의 아직도 타임 아웃을했습니다! – olbanana

+0

덕분에 작동했습니다! – olbanana

+0

그것은 여전히 ​​타임 아웃을 보여 주지만, 페이지를 완전히 다시로드하지는 않지만, brooo – olbanana