递归函数逻辑问题

时间:2011-03-09 16:01:33

标签: php recursion

我有一个带有错误的递归函数,我遇到了麻烦。逻辑如下: 表单包含各种类型的字段。一种类型的字段是引用字段,它将表单链接到另一个表单。而且,参考文献可以链接。示例:我可以有一个处方表格,其中包含对遭遇的参考,其中包含对具有人物参考的医生的参考。相同的遭遇形式有一个患者参考,也指人。

因此,医生姓氏的参考字段链看起来像处方上的字段49指的是遭遇。遇到的第7场是指博士。 Doctor 6上的字段是指Person。人员字段1指的是姓氏。 [49-7-6-1]

患者姓氏的参考链将遵循相同的逻辑[49-5-4-1]。

我的问题是这个。使用当前函数,如果我在较低级别具有分支引用,则引用链字符串会出现问题。而不是以[49-5-4-1]为患者姓氏结束,我得到[49-7-5-4-1]。

该功能如下:

function get_reference_fields($form_id, $base_fields = array(), $ref_field_id = null, $ref_string = null, $field_name = null,$base_form = null,$form_name=null) {

         if(!$base_form) {
             $base_form = $form_id;
         }
         App::import('model', 'Section');
         $zmr_sections = new Section();


         $sectionList = $zmr_sections->getSectionList($form_id);


         if ($sectionList) {

             $new_base_fields = $this->Field->find('list', array('joins' => array(
                 array(
                     'table' => 'field_types',
                     'alias' => 'Type',
                     'type' => 'left',
                     'foreignKey' => false,
                     'conditions' => array('Type.id = Field.field_type_id')
                 )), 'conditions' => array('Type.base_type != "formatting" AND Type.base_type != "reference" AND Type.base_type != "reverse reference"', 'Field.active=1', 'Field.section_id IN (' . $sectionList . ')')));


             if ($ref_field_id) {
                 $newFields = array();


                 foreach ($new_base_fields AS $key => $field) {
                     if ($field_name) {

                         $field = $form_name . ' - ' . $field;
                     }

                     $newFields[$ref_string . '-' . $key] = $field;

                 }
                 $new_base_fields = $newFields;
             }
             if (!$base_fields) {
                 $base_fields = array();
             }

             $base_fields = $base_fields + $new_base_fields;


             $reference_fields = $this->Field->find('all', array('joins' => array(
                 array(
                     'table' => 'field_types',
                     'alias' => 'Type',
                     'type' => 'left',
                     'foreignKey' => false,
                     'conditions' => array('Type.id = Field.field_type_id')
                 )), 'conditions' => array('Type.base_type' => 'reference', 'Field.active=1', 'Field.section_id IN (' . $sectionList . ')')));


             foreach ($reference_fields AS $reference_field) {

                 $field_name = $reference_field['Field']['name'];
                 $query = "SELECT form_id FROM zmr_lists WHERE id=" . $reference_field['Field']['zmr_list_id'];

                 $list_data = $this->query($query);
                 $new_form_id = $list_data[0]['zmr_lists']['form_id'];



                 if($form_id == $base_form){
                     $ref_string = $reference_field['Field']['id'];
                     $form_name = $this->Field->Section->Form->field('Form.label',array('Form.id'=>$new_form_id));

                 }else{
                     $ref_string .= '-'. $reference_field['Field']['id'];
                     $form_name .= '-'. $this->Field->Section->Form->field('Form.label',array('Form.id'=>$new_form_id));

                 }
                 $base_fields = $this->get_reference_fields($new_form_id, $base_fields, $reference_field['Field']['id'], $ref_string, $field_name,$base_form,$form_name);

             }


         }

         return $base_fields;

     }

==================================
并且,数据的样本结果集是:

[49-7-6-48] => Doctor - Primary Role
[49-7-6-43] => Doctor - State
[49-7-6-11] => Doctor - Password
[49-7-6-2] => Doctor - Last Name
[49-7-6-10] => Doctor - Username
[49-7-6-28] => Doctor - City
[49-7-6-1] => Doctor - First Name
[49-7-6-24] => Doctor - Middle Initial
[49-7-6-27] => Doctor - Address2
[49-7-6-25] => Doctor - Date of Birth
[49-7-6-47] => Doctor - Phone Number
[49-7-6-26] => Doctor - Address1

[49-7-5-68] => PSG Study - Patient ID Number
[49-7-5-67] => PSG Study - Insurance Provider's Name
[49-7-5-74] => PSG Study - PCP referral required?
[49-7-5-73] => PSG Study - Pre-Authorization Obtained?
[49-7-5-22] => PSG Study - Special Needs
[49-7-5-76] => PSG Study - PCP Phone Number
[49-7-5-69] => PSG Study - Group Number
[49-7-5-70] => PSG Study - Relationship to Insured Member
[49-7-5-71] => PSG Study - Referring Doctor
[49-7-5-75] => PSG Study - If Yes, PCP Name:
[49-7-5-72] => PSG Study - Diagnoses:
[49-7-5-4-48] => Patient - Primary Role
[49-7-5-4-43] => Patient - State
[49-7-5-4-11] => Patient - Password
[49-7-5-4-2] => Patient - Last Name
[49-7-5-4-10] => Patient - Username
[49-7-5-4-28] => Patient - City
[49-7-5-4-1] => Patient - First Name
[49-7-5-4-24] => Patient - Middle Initial
[49-7-5-4-27] => Patient - Address2
[49-7-5-4-25] => Patient - Date of Birth
[49-7-5-4-47] => Patient - Phone Number
[49-7-5-4-26] => Patient - Address1

=========================

有人可以帮我找出正确生成参考链的算法吗?请注意,表格在技术上可以参考自己。例如,医生可以参考另一位医生(实习生或主管等)

1 个答案:

答案 0 :(得分:-1)

我找到了答案。我知道ref_string的初始化是问题所在。我无法理解它。我在递归函数调用

之后立即添加了这个
  if ($ref_string) {
                        $ref_array = explode('-', $ref_string);
                        array_pop($ref_array);
                        $ref_string = implode('-', $ref_array);
                        $form_name_array = explode('-', $form_name);
                        array_pop($form_name_array);
                        $form_name = implode('-', $form_name_array);
                    }