开发者

PHP for loop not entering into loop

For some reason my for loop is not starting by the looks it seems. I tested it by placing an echo statement inside it and it does not display so there must be something wrong, maybe my syntax but I cannnot see it after looking at it for hours.

Thanks for your time.

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 im开发者_StackOverflowplemented
                    );

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


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

Should read:

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

Or maybe:

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


change

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

to

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

You want to iterate while i is LESS than the variable amount, not more.


You operator is incorrect. Switch the > to <.


You're only looping while i is greater than $completedstaffrows etc. Change the >s to <s.


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


Well you write that $completedstaffrows=5 and you init $i=0, in the loop you write "$i > $completedstaffrows" which for the first run evaluates to 0 > 5 which happens to be false. So thats why it dosn't enter the loops. So replace ">" with "<" to resolve the problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜