Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4053272
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:25:34+00:00 2026-05-20T14:25:34+00:00

I have a recursive function with a bug that I am having trouble with.

  • 0

I have a recursive function with a bug that I am having trouble with. The logic is as follows:
Forms have Fields of various types. One type of field is a Reference Field, which links a Form to another Form. And, References can chain. Example: I can have a Prescription Form with a Reference to an Encounter, that has a Reference to Doctor that has a Reference to Person. The same Encounter Form has a Reference to Patient That also refers to Person.

So, the Reference field chain for Doctor Last Name would look like Field 49 on Prescription refers to Encounter. Field 7 on Encounter refers to Doctor. Field 6 on Doctor refers to Person. Field 1 on Person refers to Last Name. [49-7-6-1]

The Reference chain for Patient Last Name would be [49-5-4-1] following the same logic.

My problem is this. With the current function, the Reference chain strings have problems if I have branching references at a lower level. Instead of ending up with [49-5-4-1] for Patient Last Name, I get [49-7-5-4-1].

The function is as follows:

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;

     }

==================================
And, a sample result set for the data is:

[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

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

Can anybody help me figure out an algorithm to properly generate the reference chains? Please note, it is technically possible for a form to refer to itself. For example, a Doctor can have a Reference to another Doctor (trainee or supervisor, etc.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T14:25:34+00:00Added an answer on May 20, 2026 at 2:25 pm

    I found the answer. I knew the initialization of the ref_string was the problem. I just couldn’t get my head around it. I added this immediately after the recursive function call

      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);
                        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a recursive function that returns a value. public function getparent ($user) {
I have a recursive scalar function that needs to update a record in another
Say I have a recursive function that I want to know how many times
MATLAB is a pass by value language. I have a recursive function that processes
I have a simple recursive function that calculates a simple pendulum swing decay, using
I have a recursive function from a C book as follows: void print(int a[],
I have a recursive lambda function in one of my objects, and it needs
I have a recursive function with a static variable count. The function increments count
I have a recursive function reading a table of contents of documents from a
I have a recursive function being used to generate a menu on my site.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.