We help IT Professionals succeed at work.
Get Started

Search XML file with PHP

piixeldesigns
on
108 Views
Last Modified: 2015-06-22
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;
	
	}
  
  }
  
?>

Open in new window


Thanks in advance
Comment
Watch Question
Most Valuable Expert 2011
Author of the Year 2014
Commented:
This problem has been solved!
Unlock 1 Answer and 34 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE