Hello
I am trying to add two records to an array so I can identify
which record has a priority 1 or priority 2.
This is not working, how do get this to work?

<?
             //get data from database
              $query = "SELECT a.street, a.city, a.province,
                            b.dob, b.id1_name, b.id1_value, 
                            b.id1_priority, b.id2_name, b.id2_value,
                            b.id2_priority
                        FROM pat_address a
                        LEFT JOIN pat_ident b
                        ON a.patient_id = b.patient_id
                        WHERE a.patient_id = '$patient_id'
                        AND a.deleted = 'N'
                        AND b.deleted = 'N'; 
              $result = mysqli_query ($mysqli, $query);
              while($row = mysqli_fetch_array($result)) 
                 {
                    list($street, $city, $province,
                         $birthday_c, $lang1_code,
                         $id_priority1, $id_name1_c, 
                         $id_value1, $id_priority2, 
                         $id_name2_c, $id_value2,
                        ) = $row;
                 }



                //if a priority exists add the data to array  
                 $id_array = array();//identify array
                if(!empty($id_priority1) && $id_priority1 > 0)
                  {
                     $id_array['pri'] = $id_priority1;//priority
                     $id_array['name'] = $id_name1_c; //name
                     $id_array['value'] = $id_value1; //value

                  }

                if(!empty($id_priority2) && $id_priority2 > 0)
                  {
                     $id_array['pri'] = $id_priority2; //priority
                     $id_array['name'] = $id_name2_c;  //name
                     $id_array['value'] = $id_value2;  //value
                  }  


             //sort into priority 1 & 2
             foreach ($id_array AS $idray)
                  {
                     echo"()".$idray['pri'];
                     switch($idray['pri'])
                       {
                          case'1':
                            $name1 = $idray['name'];
                            $value1 = $idray['value'];
                            $priority1 = $idray['pri'];
                          break;

                          case'2':
                             $name2 = $idray['name'];
                             $value2 = $idray['value'];
                             $priority2 = $idray['pri'];
                          break;
                       }    
                  }

?>

Member Avatar for diafol
$id_array = array();//identify array

Place it before the while loop

Thanks for responding.

What is the benifit of placing the $id_array = array(); before the While loop when the array is not being populated by the while loop?

I chose not to use the while loop because I wanted to manually add the the two related records.

<?
$id_array = array();//identify array
if(!empty($id_priority1) && $id_priority1 > 0) 
   { 
       $id_array[] = array('pri' => $id_priority1, 'name' => $id_name1_c, 'value' => $id_value1);
   } 
elseif(!empty($id_priority2) && $id_priority2 > 0) 
   { 
       $id_array[] = array('pri' => $id_priority2, 'name' => $id_name2_c, 'value' => $id_value2);
   }  
?>

//I tried the changes below and could not get it to work.

<?
if(!empty($id_priority1) && $id_priority1 > 0) 
   { 
       $id_array[0][] = array('pri' => $id_priority1, 'name' => $id_name1_c, 'value' => $id_value1);
   } 
elseif(!empty($id_priority2) && $id_priority2 > 0) 
   { 
       $id_array[1][] = array('pri' => $id_priority2, 'name' => $id_name2_c, 'value' => $id_value2);
   } 

?>

//I got this to work:

<?
$id_array = array();//identify array
if(!empty($id_priority1) && $id_priority1 > 0)
  {
     $id_array[0]['pri'] = $id_priority1;
     $id_array[0]['name'] = $id_name1_c;
     $id_array[0]['value'] = $id_value1;
  }

if(!empty($id_priority2) && $id_priority2 > 0)
  {
     $id_array[1]['pri'] = $id_priority2;
     $id_array[1]['name'] = $id_name2_c;
     $id_array[1]['value'] = $id_value2;
  }
?>  
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.