2011-07-07 281 views
3

由於某種原因,我的for循環不是從外觀看起來似乎。我通過在它內部放置一個echo語句來測試它,並且它不顯示,所以一定有什麼錯誤,也許是我的語法,但看了幾個小時後我看不到它。PHP for循環不進入循環

謝謝你的時間。

echo $completedstaffrows; // value of 5 
     echo $completedeventrows; //value of 4 
      echo "<br/>"; 

     //Staff 

      //For loop to enter the correct amount of rows as entered in the form 
      for ($i=0; $i > $completedstaffrows; $i++) 
      { 

       //Data not inserted into Staff table, FK given from dropdown on form to insert in linking table 

       $staffdata = array 
       (
        'staff_id' => $this->input->post ('staff'.$i), 
        'procedure_id' => $procedurefk, 
        'quantity' => $this->input->post ('staff_quantity'.$i), 
        'quantity_sterilised' => NULL, //not implemented yet 
       ); 

       $inserthumanresource = $this->db->insert ('hr', $staffdata); 
       echo "hello world"; // to test if for loop working 
      } 



     //Events 

       //For loop to enter all events rows completed by user 
       for ($i=0; $i > $completedeventrows; $i++) 
       { 

        //First input into "Medical Supplies" table 
        $medsupplies = array 
        (
         'name' => $this->input->post ('supplies'.$i), 
         'manufacturer' => "Bruce Industries" //To be implemented 
        ); 

         //Insert data into table 
         $insertmeds = $this->db->insert ('med_item', $insertmeds); 

         //Get med supplies foreign key for linking table 
         $medsuppliesfk = $this->db->insert_id(); 

        //Then input into table "Event" 

        $eventdata = array 
        (
         'time' => $this->input->post ('time'.$i), 
         'event' => $this->input->post ('event'.$i), 
         'success' => $this->input->post ('success'.$i), 
         'comment' => $this->input->post ('comment'.$i), 
         'procedure_id' => $procedurefk 

        ); 
         //Insert 
         $insertevent = $this->db->insert ('procedure_event', $eventdata); 


         //Get event fk for linking table 
         $eventfk = $this->db->insert_id(); 

        //Linking table "Resources" 

        $resourcedata = array 
        (
         'event_id' => $eventfk, 
         'medical_item_id' => $medsuppliesfk, 
         'quantity' => NULL, //Not implemented yet 
         'unit' => NULL 



        ); 

        $insertresource = $this->db->insert ('resources', $resourcedata); 
+0

而答案來水浸 –

回答

3

變化

for ($i=0; $i > $completedstaffrows; $i++)

for ($i=0; $i < $completedstaffrows; $i++)

你想迭代,而我比可變的數量,而不是更小。

2
$i=0; $i < $completedstaffrows; $i++ 
     ^^^^^ 
+0

感謝不能相信我錯過了,感覺像白癡 – Bob

+0

我該怎麼做? – Bob

+0

點擊他答案旁邊的灰色勾號:) – Tak

4
for ($i=0; $i > $completedstaffrows; $i++) 

應改爲:

for ($i=0; $i < $completedstaffrows; $i++) 

或許:

for ($i=0; $i <= $completedstaffrows; $i++) 
+0

感謝不能相信我沒有發現我現在覺得這麼愚蠢現在haha – Bob

3

您的操作員不正確。將>切換爲<

3

你只在循環,而i大於$completedstaffrows等。將> s更改爲< s。

+0

哇我不相信我沒有發現這一點。謝謝 – Bob

+2

有時它只需要一個不同的眼睛 –

2

那麼你寫的$ completedstaffrows = 5,你初始化$ i = 0,在你寫的「$ i> $ completedstaffrows」的循環中,第一次運行的計算結果爲0> 5,這恰好是錯誤的。這就是爲什麼它不能進入​​循環。因此用「<」替換「>」來解決問題。