troubleshooting Question

Search XML file with PHP

Avatar of piixeldesigns
piixeldesigns asked on
PHPXML
34 Comments1 Solution110 ViewsLast Modified:
Hi There,

I have a form which has four fields, 3 drop downs and 1 input field. Using this form I want to search through my xml file which has my data in there, I then want to go another page which will display my results, I have this all working fine my only problem is I don't know what to use or how to search through the xml file from a form.

I have tried using stristr which does work however with my form the user can fill out just one option or all four, or two etc...and if any are left blank it throughs an error of empty delimiter.

Please Help - Here is my code:
<?php
                $doc = new DOMDocument(); 
				$doc->load( $path.'inc/jobs.xml' ); 
				
				$keywordQuery = $_POST['keyword'];
				$classificationQuery = $_POST['classification'];
				$jobTypeQuery = $_POST['jobType'];
				$locationQuery = $_POST['location'];
   
$jobs = $doc->getElementsByTagName( "Job" ); 
$i = 0;
$max_loop = 7;
foreach( $jobs as $job ) { 
	
  $references = $job->getElementsByTagName( "Reference" ); 
  $reference = $references->item(0)->nodeValue; 
   
  $titles= $job->getElementsByTagName( "Title" ); 
  $titleh= $titles->item(0)->nodeValue; 
   
  $shortDescriptions = $job->getElementsByTagName( "ShortDescription" ); 
  $shortDescription = $shortDescriptions->item(0)->nodeValue;
  
  $adDetails = $job->getElementsByTagName( "AdDetail" ); 
  $adDetail = $adDetails->item(0)->nodeValue; 
  
  $consultants = $job->getElementsByTagName( "Consultant" ); 
  $consultant = $consultants->item(0)->nodeValue;
  
  $contactNumbers = $job->getElementsByTagName( "ContactNumber" ); 
  $contactNumber = $contactNumbers->item(0)->nodeValue;
  
  $contactEmails = $job->getElementsByTagName( "ContactEmail" ); 
  $contactEmail = $contactEmails->item(0)->nodeValue;
  
  $companyNames = $job->getElementsByTagName( "CompanyName" ); 
  $companyName = $companyNames->item(0)->nodeValue;
  
  $locations = $job->getElementsByTagName( "Location" ); 
  $location = $locations->item(0)->nodeValue;
  
  $jobTypes = $job->getElementsByTagName( "JobType" ); 
  $jobType = $jobTypes->item(0)->nodeValue;
  
  $classifications = $job->getElementsByTagName( "Classification" ); 
  $classification = $classifications->item(0)->nodeValue;
  
  $positions = $job->getElementsByTagName( "Position" ); 
  $position = $positions->item(0)->nodeValue;
  
  $linkOutUrls = $job->getElementsByTagName( "LinkOutUrl" ); 
  $linkOutUrl = $linkOutUrls->item(0)->nodeValue;
  
  $applicationEmails = $job->getElementsByTagName( "ApplicationEmail" ); 
  $applicationEmail = $applicationEmails->item(0)->nodeValue;
  
  $salaryTypes = $job->getElementsByTagName( "SalaryType" ); 
  $salaryType = $salaryTypes->item(0)->nodeValue;
  
  $salaryMins = $job->getElementsByTagName( "SalaryMin" ); 
  $salaryMin = $salaryMins->item(0)->nodeValue;
  
  $salaryMaxs = $job->getElementsByTagName( "SalaryMax" ); 
  $salaryMax = $salaryMaxs->item(0)->nodeValue;
  
  $salaryCurrencys = $job->getElementsByTagName( "SalaryCurrency" ); 
  $salaryCurrency = $salaryCurrencys->item(0)->nodeValue;
  
  $residentsOnlys = $job->getElementsByTagName( "ResidentsOnly" ); 
  $residentOnly = $residentsOnlys->item(0)->nodeValue;
  
  if($keywordQuery || $locationQuery || $classificationQuery || $jobTypeQuery) {
	  
	  
		  		unset($keywordQuery);
				unset($classificationQuery);
				unset($jobTypeQuery);
				
	  
  	if (stristr($adDetail, $keywordQuery) || 
	stristr($shortDescription, $keywordQuery) ||
	stristr($titleh, $keywordQuery) || 
	stristr($classification, $classificationQuery) ||
	stristr($location, $locationQuery) || 
	stristr($jobType, $jobTypeQuery)) {
		
		
?>

	<p>
   		<strong><a href="<?php echo $path; ?>candidates/jobs-board/job-detail/?ref=<?php echo $reference; ?>" class="red" title="<?php echo $titleh; ?>" target="_self"><?php echo $titleh; ?></a></strong> - (<?php echo $jobType; ?>)<br />
   		<?php echo $location; ?><br />
        <?php echo $shortDescription; ?><br />
        <strong><a href="<?php echo $path; ?>candidates/jobs-board/job-detail/?ref=<?php echo $reference; ?>" class="red" title="<?php echo $titleh; ?>" target="_self">Read More</a>	</strong>
   </p>

<?php  
	}

	} else {
?>
   
   <p>
   		<strong><a href="<?php echo $path; ?>candidates/jobs-board/job-detail/?ref=<?php echo $reference; ?>" class="red" title="<?php echo $titleh; ?>" target="_self"><?php echo $titleh; ?></a></strong> - (<?php echo $jobType; ?>)<br />
   		<?php echo $location; ?><br />
        <?php echo $shortDescription; ?><br />
        <strong><a href="<?php echo $path; ?>candidates/jobs-board/job-detail/?ref=<?php echo $reference; ?>" class="red" title="<?php echo $titleh; ?>" target="_self">Read More</a>	</strong>
   </p>
   
   
  <?php
  $i++;
	if($i==$max_loop) break;
	
	}
  
  }
  
?>

Thanks in advance
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 34 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 34 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros