2013-02-07 84 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

我不敢肯定,但你在這兩個循環中使用同一個變量$我所以也許是因爲你的第二個循環無法正常工作。在第二個循環中嘗試另一個變量$ j。

0

的代碼格式不,所以很容易錯過,您使用相同的變量爲兩個迴路,因此第二循環開始的時候,第一個失去曲目的進度。使用foreach()或第二個循環的另一個變量名稱。

0

你可能會得到一個超時由於具有嵌套循環這兩者都使用相同的變量,$i到,它只是不斷遞增。

嘗試改變第二個循環,像這樣:

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

我這樣做,它仍然超時! – olbanana

+0

它的工作的感謝! – olbanana

+0

但它仍然顯示超時,那並不完全重新加載頁面,但在其制定的計算....任何事情兄弟? – olbanana