title: Extract multiple values from the XML node and map it to the Database
body: <p>This php script below fetches XML job feed from the remote url and map the fields into the Database. The script was working fine, till yesterday when the specialty node values from the XML feed were changed to multiple values separated with comma. </p>
  
  <pre><code>&lt;?php
      require("globals.php5");
      require("cookies.php5");
  
      define(HPP_ACCT,111);
      define(HPP_UID,10788);
      error_reporting(E_ALL);
  
      if(isset($_POST["submit"])) {
          $filename="http://example.com/xmlfeed";
          $ch = curl_init($filename);
          curl_setopt($ch, CURLOPT_HEADER, 0);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          $output = curl_exec($ch);
          if($output === false) throw new Exception('Curl error: ' . curl_error($ch),__LINE__);
  
          curl_close($ch);
          $xml = simplexml_load_string($output);
  
          if($xml) {
              $hmacct = HPP_ACCT;
              $db = db_career();
              $sql = "select sp_code,sp_name from specialties";
              $res = $db-&gt;query($sql);
              if( !$res ) 
                  throw new Exception(DEBUG?"$db-&gt;error : $sql":'Problem with Specialties Table',__LINE__);
              $hamspec = array();
              while( list($spc,$sph) = $res-&gt;fetch_row() ) {
                  $hamspec[$sph] = $spc; 
              }
              $res-&gt;free();
              $sql = "select st_code,st_name from states";
              $res = $db-&gt;query($sql);
              if( !$res ) 
                  throw new Exception(DEBUG?"$db-&gt;error : $sql":'Problem with States Table',__LINE__);
              $hamstadt = array();
              while( list($spc,$sph) = $res-&gt;fetch_row() ) { 
                  $hamstadt[$sph] = $spc; 
              }
              $res-&gt;free();
  
              if(HPP_ACCT!=""){
                  $sql = "delete from opportunities where o_acct='".HPP_ACCT."' AND status=1 ";
                  $res = $db-&gt;query($sql);
              }
              $client = new Customer($db,HPP_UID);
  
              $states = array(
                      'Alabama'=&gt;'AL',
                      'Alaska'=&gt;'AK',
                      'Arizona'=&gt;'AZ',
                      'Arkansas'=&gt;'AR',
                      'California'=&gt;'CA',
                      'Colorado'=&gt;'CO',
                      'Connecticut'=&gt;'CT',
                      'Delaware'=&gt;'DE',
                      'Florida'=&gt;'FL',
                      'Georgia'=&gt;'GA',
                      'Hawaii'=&gt;'HI',
                      'Idaho'=&gt;'ID',
                      'Illinois'=&gt;'IL',
                      'Indiana'=&gt;'IN',
                      'Iowa'=&gt;'IA',
                      'Kansas'=&gt;'KS',
                      'Kentucky'=&gt;'KY',
                      'Louisiana'=&gt;'LA',
                      'Maine'=&gt;'ME',
                      'Maryland'=&gt;'MD',
                      'Massachusetts'=&gt;'MA',
                      'Michigan'=&gt;'MI',
                      'Minnesota'=&gt;'MN',
                      'Mississippi'=&gt;'MS',
                      'Missouri'=&gt;'MO',
                      'Montana'=&gt;'MT',
                      'Nebraska'=&gt;'NE',
                      'Nevada'=&gt;'NV',
                      'New Hampshire'=&gt;'NH',
                      'New Jersey'=&gt;'NJ',
                      'New Mexico'=&gt;'NM',
                      'New York'=&gt;'NY',
                      'North Carolina'=&gt;'NC',
                      'North Dakota'=&gt;'ND',
                      'Ohio'=&gt;'OH',
                      'Oklahoma'=&gt;'OK',
                      'Oregon'=&gt;'OR',
                      'Pennsylvania'=&gt;'PA',
                      'Rhode Island'=&gt;'RI',
                      'South Carolina'=&gt;'SC',
                      'South Dakota'=&gt;'SD',
                      'Tennessee'=&gt;'TN',
                      'Texas'=&gt;'TX',
                      'Utah'=&gt;'UT',
                      'Vermont'=&gt;'VT',
                      'Virginia'=&gt;'VA',
                      'Washington'=&gt;'WA',
                      'West Virginia'=&gt;'WV',
                      'Wisconsin'=&gt;'WI',
                      'Wyoming'=&gt;'WY'
                    );
  
              $ar = array();
  
              foreach($xml-&gt;Opportunity as $job) {
                  $jid = "JOB {" .addslashes($job-&gt;OpportunityId)."}";
                  $jobid = "(JOB ID " .addslashes($job-&gt;JobNumber).")";
                  $facility = addslashes($job-&gt;facility)." ".$jobid;
                  $city = $job-&gt;city;
                  $state = $states["$job-&gt;state"];
                  $spec = addslashes(trim($job-&gt;specialty));
  
                  switch($spec) {
                      case "Family Medicine":
                          $spec="Family Practice";
                          break;
                      case "Internal Medicine":
                          $spec="Internal Medicine";
                          break;
                      case "Pediatric General":
                          $spec="Pediatric - General";
                          break;
                  }
  
                  $spec = trim($spec);
                  try{
                      @$spec2 = $spec == 'Emergency Medicine'?'EM':($spec == 'Hospitalist Medicine'?'HOS':$hamspec["$spec"]);
  
                  }catch(Exception $e){
                      echo $e-&gt;getMessage().' ('.$e-&gt;getCode().')&lt;br&gt;';
                  }
                  if(!$spec2){
                      echo "No specialty mapping for ".$spec.". Will be skipped.&lt;br/&gt;";
                  } else {
                      $title=$job-&gt;headline;
                      if($title==''||$title==null)
                          $title = $spec2." at ".$facility;
                          $description = html_entity_decode(addslashes($job-&gt;description));
                          $contact = $job-&gt;recruiter_name;
                          $phone = $job-&gt;RecruiterPhone;
                          $email = $job-&gt;recruiter_email;
                          $ldescr = html_entity_decode(addslashes($job-&gt;facil_description));
                          $acity = addslashes($city); $astate = addslashes($state);
                          $sql = "select l_id from locations where l_acct = $client-&gt;acct and status = 1 and l_facility = '$facility' and l_city = '$acity' and l_state = '$astate'";
  
                          $res = $db-&gt;query($sql);
                          if( !$res ) 
                              throw new Exception(DEBUG?"$db-&gt;error : $sql":'Problem with Locations Table',__LINE__);
                          if( !$res-&gt;num_rows ) { 
                              // create new location
                              $cdescr = html_entity_decode(addslashes($job-&gt;city_description));
                              $sql ="insert into locations (l_facility,l_city,l_state,l_uid,l_acct,l_description,l_commdescr,exp_date) values('$facility','$acity','$astate',$client-&gt;uid,$client-&gt;acct,'$ldescr','$cdescr',ADDDATE(NOW(), INTERVAL 1 YEAR))";
                              $result = $db-&gt;query($sql);
                              if( !$result ) 
                                  throw new Exception(DEBUG?"$db-&gt;error: $sql":'Can not insert locations',__LINE__);
                              $locid = $db-&gt;insert_id;
                          } else 
                              list($locid) = $res-&gt;fetch_row();
                      $res-&gt;free();
                      //create Opportunity
                      @$opp = new Opportunity($db,0,$locid,$client-&gt;uid,$client-&gt;acct);
                      $opp-&gt;o_name = $jid;
                      $opp-&gt;o_city = ($acity);
                      $opp-&gt;o_state = ($astate);
                      $opp-&gt;specialty = $spec2;
                      $opp-&gt;o_facility = $facility;
                      $opp-&gt;description = $description;
                      $opp-&gt;exp_date = $client-&gt;exp_date;
                      $opp-&gt;practice_type = $spec2 == 'HOS'?'Hosp':($spec == 'EM'?'ER':'');
                      $opp-&gt;o_email = addslashes($email);
                      $opp-&gt;o_phone = addslashes($phone);
                      $opp-&gt;o_contact = addslashes(htmlspecialchars( strip_tags($contact)));
  
                      @$opp-&gt;save();
                      $sql = "insert into importrac (jobid,jobacct,jobflag,jobopp) VALUES ('$jid',$client-&gt;acct,0,$opp-&gt;id) ON DUPLICATE KEY UPDATE jobflag=0";
                      $db-&gt;query($sql);
                  }
  
              }
  
              print "jobs added!";
          }
      }
  ?&gt;
  &lt;h1&gt;XML Import&lt;/h1&gt;
  &lt;form action="import.php" method="post" enctype="multipart/form-data"&gt;
  &lt;input type="submit" name="submit" value="Run"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Below is the example of the current XML feed with multiple specialties:</p>
  
  <pre><code>&lt;Opportunity&gt;
         &lt;headline&gt;Jobs in Los Angeles, CA&lt;/headline&gt;
         &lt;specialty&gt;Family Medicine, Internal Medicine, Pediatric - General&lt;/specialty&gt;
         &lt;city&gt;San Fernando Valley, CA&lt;/city&gt;
         &lt;city_description&gt;&lt;/city_description&gt;
         &lt;state&gt;California&lt;/state&gt;
         &lt;facility&gt;Medical Center&lt;/facility&gt;
         &lt;facil_description&gt;&lt;/facil_description&gt;
         &lt;description&gt;&lt;/description&gt;
         &lt;recruiter_name&gt;&lt;/recruiter_name&gt;
         &lt;recruiter_email&gt;&lt;/recruiter_email&gt;
         &lt;OpportunityId&gt;777&lt;/OpportunityId&gt;
         &lt;ScanCode&gt;&lt;/ScanCode&gt;
         &lt;recruiter_image&gt;&lt;/recruiter_image&gt;
         &lt;JobNumber&gt;155&lt;/JobNumber&gt;
         &lt;RecruiterPhone&gt;(503) 555-333&lt;/RecruiterPhone&gt;
     &lt;/Opportunity&gt;
  </code></pre>
  
  <p>If there is only one specialty value from the XML feed, php script will map it properly: <code>&lt;specialty&gt;Family Medicine&lt;/specialty&gt;</code>. </p>
  
  <p>Is it possible to extract multiple specialty values from the XML specialty node above and map it with the current PHP/MySQL code?</p>
  
  <hr>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Even if I use explode function, couple of jobs were not imported. Please see the list below. </p>
  
  <pre><code>&lt;specialty&gt;Family Medicine, Geriatric Medicine&lt;/specialty&gt;
  &lt;specialty&gt;Medical Oncology, Surgery - Breast&lt;/specialty&gt;
  &lt;specialty&gt;Emergency Medicine, Family Medicine&lt;/specialty&gt;
  &lt;specialty&gt;Internal Medicine, Pediatric - General&lt;/specialty&gt;
  &lt;specialty&gt;Emergency Medicine, Family Medicine&lt;/specialty&gt;
  &lt;specialty&gt;Orthopedics - General, Orthopedics - Sports Medicine&lt;/specialty&gt;
  &lt;specialty&gt;Family Medicine, Urgent Care/Immediate Care&lt;/specialty&gt;
  &lt;specialty&gt;Family Medicine, Internal Medicine, Pediatric - General&lt;/specialty&gt;
  &lt;specialty&gt;Family Medicine, Sports Medicine&lt;/specialty&gt;
  &lt;specialty&gt;Cardiology, Cardiology - Adv Heart Failure/Transplant&lt;/specialty&gt;
  &lt;specialty&gt;Cardiology, Cardiology - Non-Invasive&lt;/specialty&gt;
  &lt;specialty&gt;Pediatric - Developmental/Behavioral, Pediatric – Neurodevelopment Disabilities
  &lt;/specialty&gt;
  &lt;specialty&gt;Pediatric - Critical Care Medicine, Pediatric - Intensivist
  &lt;/specialty&gt;
  &lt;specialty&gt;Family Medicine, Internal Medicine, Urgent Care/Immediate Care
  &lt;/specialty&gt;
  &lt;specialty&gt;Family Medicine, Urgent Care/Immediate Care&lt;/specialty&gt;
  </code></pre>