Link to home
Start Free TrialLog in
Avatar of piixeldesigns
piixeldesigns

asked on

Search XML file with PHP

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
Avatar of Hugh McCurdy
Hugh McCurdy
Flag of United States of America image

I'm not entirely sure what you want.  Having said that, I think your problem might be that you are calling unset on $keywordQuery (etc.) right before you try to use it.
Avatar of piixeldesigns
piixeldesigns

ASKER

Yes I have to do the unset otherwise if I just leave it as a blank variable (which is correct because the user has the option to leave some of the form options blank).

If I don't do the unset I then get the error of empty delimiter?

Basically I have a form on my home page (search form with four options:
- Keyword
- classification
- location
- job type

The user has the option to fill out which ever of those they choose, then based on what was chosen I want to search through the xml file and pull out the corresponding data.

Thanks,
Under no circumstances should you attempt to reference a variable that isn't set (except for the isset() function).  Your log file is likely filled with warnings that you've accessed an uninitialized variable.

A better approach is to test the variable only if it's set -- use isset()

The stristr() tests are inside the block for the if statement.  Is that really what you want?  It looks to me that the tests will run either once or zero times for the entire file and you want to run the test for each record in the file.  Right?
I have given the null values a actual value now so that has fixed that problem with the isset() etc....

Basically the main thing I am having trouble doing is understanding how to query the xml file with the posted variables, if either one or more of those variables are matched to results in the xml then display them.

I know how to do this quite easily in mysql but its just the data is only in xml format.

Sorry if this is a little unclear but i'm not that good with php and xml. MySQL and php i'm fine.

Thanks
And yes for example if someone choses a classification and job type plus keyword I want to pull out all results in the xml that match those three variables sent. So if this happens to be matched in 6 results out of the 100 results in the xml file then only pull out the 6 results that match.
An XML file is sequential, like an old tape drive.  You have to start at the beginning and read each record one at a time and compare each one as you go.  You could print each found record as you find it.

An SQL file is "random access."  However, unless all selection fields are indexed, the SQL engine still has to compare each record to the selection query.  In the case of SQL, this is done for you.  In the case of XML, you have to do the comparison yourself.

On advantage of SQL is that you can create a list of found records.  (This could be a list of keys, such as RecordID's).  In XML this doesn't work because you have to rewind the "tape" to print records you found (if you don't "print" each found record as you find it).

Does this make any sense?
Yes that makes sense thanks - so looking at my code (from my understanding it does print out each record as it finds them however just in the query part if say someone has chosen a location and a job type (location+jobtype) it seems to only find everything other than what was chosen.
Instead of giving us code that doesn't work, please try it this way.  Post the test data you are using, and post the outputs you want to achieve.  Once we can see that we will be able to help and explain the process.  Thanks, ~Ray
Sending it through now Ray.

Thanks
OK so here is my xml data:

<?xml version="1.0" encoding="UTF-8"?>
<Jobs>
  <Job Site="CorporateMicroSite">
    <Reference>2000115</Reference>
    <Title>Senior Office Manager</Title>
    <ShortDescription>150 character grab line...</ShortDescription>
    <AdDetail>&lt;ul&gt;&lt;li&gt;Great role&lt;/li&gt;&lt;li&gt;Great people&lt;/li&gt;&lt;li&gt;Great salary&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Text of the ad...&lt;/p&gt;</AdDetail>
    <Consultant>Elise Someone</Consultant>
    <ContactNumber>02 1234 5678</ContactNumber>
    <ContactEmail>elise@something.com.au</ContactEmail>
    <CompanyName>Company ABC - 200908040122273091</CompanyName>
    <Location>NSW - Sydney Metro</Location>
    <JobType>Full time</JobType>
    <Classification>Retail</Classification>
    <Position>Office Mgmt &amp; Coord</Position>
    <LinkOutUrl>https://www.something.com.au/mylink</LinkOutUrl>
    <ApplicationEmail>rmillard@something.net.au</ApplicationEmail>
    <SalaryType>Per Year</SalaryType>
    <SalaryMin>65000</SalaryMin>
    <SalaryMax>80000</SalaryMax>
    <SalaryCurrency>AUD</SalaryCurrency>
    <ResidentsOnly>0</ResidentsOnly>
  </Job>
  <Job Site="CorporateMicroSite">
    <Reference>2000115</Reference>
    <Title>Design Manager</Title>
    <ShortDescription>150 character grab line...</ShortDescription>
    <AdDetail>&lt;ul&gt;&lt;li&gt;Great role&lt;/li&gt;&lt;li&gt;Great people&lt;/li&gt;&lt;li&gt;Great salary&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Text of the ad...&lt;/p&gt;</AdDetail>
    <Consultant>Elise Someone</Consultant>
    <ContactNumber>02 1234 5678</ContactNumber>
    <ContactEmail>elise@something.com.au</ContactEmail>
    <CompanyName>Company ABC - 200908040122273091</CompanyName>
    <Location>NSW - Sydney Metro</Location>
    <JobType>Full time</JobType>
    <Classification>Retail</Classification>
    <Position>Office Mgmt &amp; Coord</Position>
    <LinkOutUrl>https://www.something.com.au/mylink</LinkOutUrl>
    <ApplicationEmail>rmillard@something.net.au</ApplicationEmail>
    <SalaryType>Per Year</SalaryType>
    <SalaryMin>65000</SalaryMin>
    <SalaryMax>80000</SalaryMax>
    <SalaryCurrency>AUD</SalaryCurrency>
    <ResidentsOnly>0</ResidentsOnly>
  </Job>
 <Job Site="CorporateMicroSite">
    <Reference>2000115</Reference>
    <Title>Marketing Manager</Title>
    <ShortDescription>150 character grab line...</ShortDescription>
    <AdDetail>&lt;ul&gt;&lt;li&gt;Great role&lt;/li&gt;&lt;li&gt;Great people&lt;/li&gt;&lt;li&gt;Great salary&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Text of the ad...&lt;/p&gt;</AdDetail>
    <Consultant>Elise Someone</Consultant>
    <ContactNumber>02 1234 5678</ContactNumber>
    <ContactEmail>elise@something.com.au</ContactEmail>
    <CompanyName>Company ABC - 200908040122273091</CompanyName>
    <Location>NSW - Sydney Metro</Location>
    <JobType>Full time</JobType>
    <Classification>Retail</Classification>
    <Position>Office Mgmt &amp; Coord</Position>
    <LinkOutUrl>https://www.something.com.au/mylink</LinkOutUrl>
    <ApplicationEmail>rmillard@something.net.au</ApplicationEmail>
    <SalaryType>Per Year</SalaryType>
    <SalaryMin>65000</SalaryMin>
    <SalaryMax>80000</SalaryMax>
    <SalaryCurrency>AUD</SalaryCurrency>
    <ResidentsOnly>0</ResidentsOnly>
  </Job>
</Jobs>

Open in new window


so my expected output that I want to achieve is say for example:
someone chooses a location and a job type. If that job type doesn't exist in that location don't show.

If it does exist in that location then show a result (echo out all the values for the corresponding <Job>.

My Form fields are as follows:
- Keyword
- Classification
- Location
- Job Type

Please let me know if you need anything else Ray.

Thanks
Thanks.  I looked at the XML and noted that all locations and job types are exactly the same.  It might be useful to change that data sample to allow for testing.
I hope this is better for you.

<?xml version="1.0" encoding="UTF-8"?>
<Jobs>
  <Job Site="CorporateMicroSite">
    <Reference>2000115</Reference>
    <Title>Senior Office Manager</Title>
    <ShortDescription>150 character grab line...</ShortDescription>
    <AdDetail>&lt;ul&gt;&lt;li&gt;Great role&lt;/li&gt;&lt;li&gt;Great people&lt;/li&gt;&lt;li&gt;Great salary&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Text of the ad...&lt;/p&gt;</AdDetail>
    <Consultant>Elise Someone</Consultant>
    <ContactNumber>02 1234 5678</ContactNumber>
    <ContactEmail>elise@something.com.au</ContactEmail>
    <CompanyName>Company ABC - 200908040122273091</CompanyName>
    <Location>WA - Perth</Location>
    <JobType>Contract</JobType>
    <Classification>Industrial</Classification>
    <Position>Office Mgmt &amp; Coord</Position>
    <LinkOutUrl>https://www.something.com.au/mylink</LinkOutUrl>
    <ApplicationEmail>rmillard@something.net.au</ApplicationEmail>
    <SalaryType>Per Year</SalaryType>
    <SalaryMin>65000</SalaryMin>
    <SalaryMax>80000</SalaryMax>
    <SalaryCurrency>AUD</SalaryCurrency>
    <ResidentsOnly>0</ResidentsOnly>
  </Job>
  <Job Site="CorporateMicroSite">
    <Reference>201023</Reference>
    <Title>Design Manager</Title>
    <ShortDescription>150 character grab line...</ShortDescription>
    <AdDetail>&lt;ul&gt;&lt;li&gt;Great role&lt;/li&gt;&lt;li&gt;Great people&lt;/li&gt;&lt;li&gt;Great salary&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Text of the ad...&lt;/p&gt;</AdDetail>
    <Consultant>Elise Someone</Consultant>
    <ContactNumber>02 1234 5678</ContactNumber>
    <ContactEmail>elise@something.com.au</ContactEmail>
    <CompanyName>Company ABC - 200908040122273091</CompanyName>
    <Location>SA - Adelaide</Location>
    <JobType>Part time</JobType>
    <Classification>Hospitality</Classification>
    <Position>Office Mgmt &amp; Coord</Position>
    <LinkOutUrl>https://www.something.com.au/mylink</LinkOutUrl>
    <ApplicationEmail>rmillard@something.net.au</ApplicationEmail>
    <SalaryType>Per Year</SalaryType>
    <SalaryMin>65000</SalaryMin>
    <SalaryMax>80000</SalaryMax>
    <SalaryCurrency>AUD</SalaryCurrency>
    <ResidentsOnly>0</ResidentsOnly>
  </Job>
 <Job Site="CorporateMicroSite">
    <Reference>2000115</Reference>
    <Title>Marketing Manager</Title>
    <ShortDescription>150 character grab line...</ShortDescription>
    <AdDetail>&lt;ul&gt;&lt;li&gt;Great role&lt;/li&gt;&lt;li&gt;Great people&lt;/li&gt;&lt;li&gt;Great salary&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Text of the ad...&lt;/p&gt;</AdDetail>
    <Consultant>Elise Someone</Consultant>
    <ContactNumber>02 1234 5678</ContactNumber>
    <ContactEmail>elise@something.com.au</ContactEmail>
    <CompanyName>Company ABC - 200908040122273091</CompanyName>
    <Location>NSW - Sydney Metro</Location>
    <JobType>Full time</JobType>
    <Classification>Retail</Classification>
    <Position>Office Mgmt &amp; Coord</Position>
    <LinkOutUrl>https://www.something.com.au/mylink</LinkOutUrl>
    <ApplicationEmail>rmillard@something.net.au</ApplicationEmail>
    <SalaryType>Per Year</SalaryType>
    <SalaryMin>65000</SalaryMin>
    <SalaryMax>80000</SalaryMax>
    <SalaryCurrency>AUD</SalaryCurrency>
    <ResidentsOnly>0</ResidentsOnly>
  </Job>
</Jobs>

Open in new window


Thanks
See http://www.laprbass.com/RAY_temp_piix.php
<?php // RAY_temp_piix.php
error_reporting(E_ALL);
echo "<pre>";

// REQUIRED FOR PHP 5.1+
date_default_timezone_set('America/Chicago');

// TEST DATA FROM THE POST AT EE
$xml = <<<ENDXML
<?xml version="1.0" encoding="UTF-8"?>
<Jobs>
  <Job Site="CorporateMicroSite">
    <Reference>2000115</Reference>
    <Title>Senior Office Manager</Title>
    <ShortDescription>150 character grab line...</ShortDescription>
    <AdDetail>&lt;ul&gt;&lt;li&gt;Great role&lt;/li&gt;&lt;li&gt;Great people&lt;/li&gt;&lt;li&gt;Great salary&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Text of the ad...&lt;/p&gt;</AdDetail>
    <Consultant>Elise Someone</Consultant>
    <ContactNumber>02 1234 5678</ContactNumber>
    <ContactEmail>elise@something.com.au</ContactEmail>
    <CompanyName>Company ABC - 200908040122273091</CompanyName>
    <Location>NSW - Sydney Metro</Location>
    <JobType>Full time</JobType>
    <Classification>Retail</Classification>
    <Position>Office Mgmt &amp; Coord</Position>
    <LinkOutUrl>https://www.something.com.au/mylink</LinkOutUrl>
    <ApplicationEmail>rmillard@something.net.au</ApplicationEmail>
    <SalaryType>Per Year</SalaryType>
    <SalaryMin>65000</SalaryMin>
    <SalaryMax>80000</SalaryMax>
    <SalaryCurrency>AUD</SalaryCurrency>
    <ResidentsOnly>0</ResidentsOnly>
  </Job>
  <Job Site="CorporateMicroSite">
    <Reference>2000115</Reference>
    <Title>Design Manager</Title>
    <ShortDescription>150 character grab line...</ShortDescription>
    <AdDetail>&lt;ul&gt;&lt;li&gt;Great role&lt;/li&gt;&lt;li&gt;Great people&lt;/li&gt;&lt;li&gt;Great salary&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Text of the ad...&lt;/p&gt;</AdDetail>
    <Consultant>Elise Someone</Consultant>
    <ContactNumber>02 1234 5678</ContactNumber>
    <ContactEmail>elise@something.com.au</ContactEmail>
    <CompanyName>Company ABC - 200908040122273091</CompanyName>
    <Location>NSW - Sydney Metro</Location>
    <JobType>Full time</JobType>
    <Classification>Retail</Classification>
    <Position>Office Mgmt &amp; Coord</Position>
    <LinkOutUrl>https://www.something.com.au/mylink</LinkOutUrl>
    <ApplicationEmail>rmillard@something.net.au</ApplicationEmail>
    <SalaryType>Per Year</SalaryType>
    <SalaryMin>65000</SalaryMin>
    <SalaryMax>80000</SalaryMax>
    <SalaryCurrency>AUD</SalaryCurrency>
    <ResidentsOnly>0</ResidentsOnly>
  </Job>
 <Job Site="CorporateMicroSite">
    <Reference>2000115</Reference>
    <Title>Marketing Manager</Title>
    <ShortDescription>150 character grab line...</ShortDescription>
    <AdDetail>&lt;ul&gt;&lt;li&gt;Great role&lt;/li&gt;&lt;li&gt;Great people&lt;/li&gt;&lt;li&gt;Great salary&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Text of the ad...&lt;/p&gt;</AdDetail>
    <Consultant>Elise Someone</Consultant>
    <ContactNumber>02 1234 5678</ContactNumber>
    <ContactEmail>elise@something.com.au</ContactEmail>
    <CompanyName>Company ABC - 200908040122273091</CompanyName>
    <Location>NSW - Sydney Metro</Location>
    <JobType>Full time</JobType>
    <Classification>Retail</Classification>
    <Position>Office Mgmt &amp; Coord</Position>
    <LinkOutUrl>https://www.something.com.au/mylink</LinkOutUrl>
    <ApplicationEmail>rmillard@something.net.au</ApplicationEmail>
    <SalaryType>Per Year</SalaryType>
    <SalaryMin>65000</SalaryMin>
    <SalaryMax>80000</SalaryMax>
    <SalaryCurrency>AUD</SalaryCurrency>
    <ResidentsOnly>0</ResidentsOnly>
  </Job>
</Jobs>
ENDXML;

// CREATE AN OBJECT FROM THE TEST DATA
$obj = SimpleXML_Load_String($xml);

// ACTIVATE THIS TO SHOW THE OBJECT
var_dump($obj);

// USE AN ITERATOR TO ACCESS PROPERTIES OF THE OBJECT
foreach ($obj->Job as $job)
{
    $s = $job['Site'];
    $t = (string)$job->Title;
    $m = number_format((string)$job->SalaryMin);

    // SHOW THE PROPERTIES WE ACCESSED
    echo PHP_EOL
    . 'AT '
    . $s
    . ' THERE IS AN OPENING FOR A '
    . $t
    . ' PAYING AT LEAST '
    . $m
    ;
}

Open in new window

Note the use of the explicit string casting statement (string) around line 88-89. This is not necessary for echo but is required for functions that expect the input to be a string or a loose data type that can be converted to a number.  For example, number_format() barks if your input is an object instead of something numeric.

HTH, ~Ray
Thanks Ray - I have one other question, how can I link the above you have done with my search form. Please explain if its already there and I have just missed it.

Thanks Again,
Here is a better example, using the newly posted data.

You can use the GET-method request to connect HTML forms with PHP scripts when you are searching for data.  Please read this over and post back with any questions.  In the demo script I hardcoded the selection criteria on line 9.  It could easily have come from a form.
http://us.php.net/manual/en/tutorial.forms.php
<?php // RAY_temp_piix.php
error_reporting(E_ALL);
echo "<pre>";

// REQUIRED FOR PHP 5.1+
date_default_timezone_set('America/Chicago');

// SOMETHING TO SEARCH FOR
$jobtype = 'CoNTract';

// TEST DATA FROM THE POST AT EE
$xml = <<<ENDXML
<?xml version="1.0" encoding="UTF-8"?>
<Jobs>
  <Job Site="CorporateMicroSite">
    <Reference>2000115</Reference>
    <Title>Senior Office Manager</Title>
    <ShortDescription>150 character grab line...</ShortDescription>
    <AdDetail>&lt;ul&gt;&lt;li&gt;Great role&lt;/li&gt;&lt;li&gt;Great people&lt;/li&gt;&lt;li&gt;Great salary&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Text of the ad...&lt;/p&gt;</AdDetail>
    <Consultant>Elise Someone</Consultant>
    <ContactNumber>02 1234 5678</ContactNumber>
    <ContactEmail>elise@something.com.au</ContactEmail>
    <CompanyName>Company ABC - 200908040122273091</CompanyName>
    <Location>WA - Perth</Location>
    <JobType>Contract</JobType>
    <Classification>Industrial</Classification>
    <Position>Office Mgmt &amp; Coord</Position>
    <LinkOutUrl>https://www.something.com.au/mylink</LinkOutUrl>
    <ApplicationEmail>rmillard@something.net.au</ApplicationEmail>
    <SalaryType>Per Year</SalaryType>
    <SalaryMin>65000</SalaryMin>
    <SalaryMax>80000</SalaryMax>
    <SalaryCurrency>AUD</SalaryCurrency>
    <ResidentsOnly>0</ResidentsOnly>
  </Job>
  <Job Site="CorporateMicroSite">
    <Reference>201023</Reference>
    <Title>Design Manager</Title>
    <ShortDescription>150 character grab line...</ShortDescription>
    <AdDetail>&lt;ul&gt;&lt;li&gt;Great role&lt;/li&gt;&lt;li&gt;Great people&lt;/li&gt;&lt;li&gt;Great salary&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Text of the ad...&lt;/p&gt;</AdDetail>
    <Consultant>Elise Someone</Consultant>
    <ContactNumber>02 1234 5678</ContactNumber>
    <ContactEmail>elise@something.com.au</ContactEmail>
    <CompanyName>Company ABC - 200908040122273091</CompanyName>
    <Location>SA - Adelaide</Location>
    <JobType>Part time</JobType>
    <Classification>Hospitality</Classification>
    <Position>Office Mgmt &amp; Coord</Position>
    <LinkOutUrl>https://www.something.com.au/mylink</LinkOutUrl>
    <ApplicationEmail>rmillard@something.net.au</ApplicationEmail>
    <SalaryType>Per Year</SalaryType>
    <SalaryMin>65000</SalaryMin>
    <SalaryMax>80000</SalaryMax>
    <SalaryCurrency>AUD</SalaryCurrency>
    <ResidentsOnly>0</ResidentsOnly>
  </Job>
 <Job Site="CorporateMicroSite">
    <Reference>2000115</Reference>
    <Title>Marketing Manager</Title>
    <ShortDescription>150 character grab line...</ShortDescription>
    <AdDetail>&lt;ul&gt;&lt;li&gt;Great role&lt;/li&gt;&lt;li&gt;Great people&lt;/li&gt;&lt;li&gt;Great salary&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Text of the ad...&lt;/p&gt;</AdDetail>
    <Consultant>Elise Someone</Consultant>
    <ContactNumber>02 1234 5678</ContactNumber>
    <ContactEmail>elise@something.com.au</ContactEmail>
    <CompanyName>Company ABC - 200908040122273091</CompanyName>
    <Location>NSW - Sydney Metro</Location>
    <JobType>Full time</JobType>
    <Classification>Retail</Classification>
    <Position>Office Mgmt &amp; Coord</Position>
    <LinkOutUrl>https://www.something.com.au/mylink</LinkOutUrl>
    <ApplicationEmail>rmillard@something.net.au</ApplicationEmail>
    <SalaryType>Per Year</SalaryType>
    <SalaryMin>65000</SalaryMin>
    <SalaryMax>80000</SalaryMax>
    <SalaryCurrency>AUD</SalaryCurrency>
    <ResidentsOnly>0</ResidentsOnly>
  </Job>
</Jobs>
ENDXML;

// CREATE AN OBJECT FROM THE TEST DATA
$obj = SimpleXML_Load_String($xml);

// ACTIVATE THIS TO SHOW THE OBJECT
// var_dump($obj);

// USE AN ITERATOR TO ACCESS PROPERTIES OF THE OBJECT
foreach ($obj->Job as $job)
{
    // EXTRACT SOME DATA FROM THE OBJECT
    $s = $job['Site'];
    $t = strtoupper((string)$job->JobType);
    $m = number_format((string)$job->SalaryMin);

    // IF THIS DATA MATCHES OUR SELECTION CRITERIA (CASE-INSENSITIVE)
    if (strtoupper($t) == strtoupper($jobtype))
    {
        // SHOW THE PROPERTIES WE ACCESSED
        echo PHP_EOL
        . 'AT '
        . $s
        . ' THERE IS AN OPENING FOR A '
        . $t
        . ' PAYING AT LEAST '
        . $m
        ;
    }
    else
    {
        echo PHP_EOL
        . 'OMITTED: '
        . $t
        ;
    }
}

Open in new window

Thanks Ray that has cleared things up a little for me now, however on line 96 of your example where you run a if statement to see if the data matches the query. That is fine for a single search query but as my form has 4 fields thus four options, would I just do the if statement with $jobtype = etc...OR $keyword = etc... OR.

Or do I have to go about multiple search criteria in a different fashion.

Thanks Again,
Where would we be looking for $keyword?  Is it part of one of the XML data elements?
Yes they keyword would be searched in the Title, ShortDescription and AdDetail elements.

Location would be searched in Location element.

Classification in the Classification element and JobType in the JobType element.
Looking at this (example) for a little more clarification...
Location would be searched in Location element.
Is this a search like a fuzzy logic match or a comparison for equality?  The logic for processing the XML is fairly simple, but if you want search-style matching you might want to hire a professional development firm to help you with it.  

Here is an example.  Let's say the location tag in the XML contains something like this:

<Location>WA - Perth</Location>

And let's say the client puts this into his form:

Pearth

Should that constitute a match?  It will not match if you use comparison operators on the strings themselves.  However these words sound pretty much alike, Perth and Pearth.  And if you take the soundex() and metaphone() results you get something like this, identical for both words:

SOUNDEX: P630
METAPHONE: PR0

A levenshtein() distance of zero on both soundex and metaphone is a linguistic match.  So I guess there are several different layers to the question.  At the top is. "How do I isolate data elements inside an XML string?" and I think we have run that to ground.  In the sublayers we find questions about checking more than one element of the XML for matching information.  This can be done with nested if() statements.  And below that we get to the definition of what constitutes an acceptable level of matching.
ok the matching needs to be an exact match as I already have pre populated the dropdown inputs except the keyword input obviously, directly from the xml file so it will always be an exact match.

The keyword match is if for example:
150 grab line text.

If the search is 0 that will display a match because 150 has a 0 in it.

So you believe I can achieve this with nested if statements?
Substring matching example:
<?php // RAY_temp_piix.php
error_reporting(E_ALL);
echo "<pre>";

// SOMETHING TO SEARCH FOR
$thing = '0';

// SOMETHING TO SEACH IN
$string = '150 grab, etc...';

// DO THE SEARCH
$poz = strpos($string, $thing);
if ($poz !== FALSE) echo "I FOUND <b>$thing</b> IN <b>$string</b>";

Open in new window

Avatar of Pierre François
Back to the roots: about searching through XML files in PHP, take a look at this article explaining how to use Xpath, which is THE skill for searching XML (or HTML) files: https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/XML/XPath/A_9856-Retreive-specific-parts-of-HTML-documents-without-the-hassle-of-monster-regular-expressions.html
@pfrancois: As I understood the question the author was not trying to find the tags, instead, the author was trying to match (or search) in the contents of certain tags.  See the comments at ID: 37865852.  I may be misunderstanding the objectives, but I think we're fairly close ;-)
Yes Fairly Close,

I have got it to work now as you said Ray as a non exact match for multiple search variables using your substring match. however there will be a fair few nested if's for the exact matches....4 ways to match 4 items against each other.

For example:
- Keyword + Location as one search
- Keyword + Classification as one search
- Keyword + Job Type as one search

There would be 3 ifs straight away.
Ok, what's the point?  Is 3 if() statements somehow a problem here?
no it's not a problem at all, just wondering if there is a method I should be using to condense down a lot of if statements?
If you have a lot of decisions you need your program to make, you need a lot of if() statements.

"Method" is a term of art in computer programming.  It refers to a function (sub-program) that is within a class and becomes part of an object.

Have you written any of the code to do what you're asking about here?  If so, please post the code, the test data, and the expected outputs so we can see what you've got and see if we can suggest the next steps. Thanks, ~Ray
I'm going to back Ray up here.  At this point, seeing the updated code matters.  If there's a reasonable way to condense if statements that makes sense, I'm sure Ray, I or someone else, will tell you about it once you publish the updated code.

The first order of business is getting a working program.  Once that is done, if efficiency can be added, we can work on that.  ~Hugh
Thanks Guys I agree with Ray and hmccurdy, I will be posting up the updated version of the code tomorrow and hopefully we can crack it from there, then talk efficiency.

Thanks Again
OK, Here is my latest code, now I have tried to do something with $globalPoz using stripos
. But I don't think this is working.

Please have a look through - thanks (I am trying to do the exact match searching.

<?php
                		$doc = new DOMDocument(); 
						$doc->load( $path.'inc/jobs.xml' ); 
				
						if($_POST['keyword'] == "") {
							$keywordQuery = "zzzzzzzznothing";
						} else {
							$keywordQuery = $_POST['keyword'];
						}
				
						$go = $_POST['go'];
						//$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(isset($go)) {
							
  								$keywordStringSearch = $adDetail . ', ' . $shortDescription . ', ' . $titleh;
								$poz = strpos($keywordStringSearch, $keywordQuery);
								$poz1 = strpos($classification, $classificationQuery);
								$poz2 = strpos($location, $locationQuery);
								$poz3 = strpos($jobType, $jobTypeQuery);
								
								$globalSearchXML = $keywordStringSearch . ', ' . $classification . ', ' . $location . ', ' . $jobType;
								$globalQuerySearch = $keywordQuery . ', ' . $classificationQuery . ', ' . $locationQuery . ', ' . $jobTypeQuery;
								$globalPoz = strpos($globalSearchXML, $globalQuerySearch);
								
								echo $globalSearchXML . '<br />';
								echo $globalQuerySearch;
								
								if ($globalPoz !== FALSE) {
							?>
                            	<p>
   		<strong><a href="<?php echo $path; ?>candidates/jobs-board/job-detail/?ref=<?php echo $reference; ?>" 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; ?>" title="<?php echo $titleh; ?>" target="_self">Read More</a></strong>
   </p>
   							<?php
								//echo "I FOUND <b>$thing</b> IN <b>$string</b>";
								} 
							
							}
							
 								
  						
						}
  
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Maybe if you want to post the HTML form that creates the request, and post the test data that you want to process, we can show you how to do it.
Why did you give a marked down grade?  According to the E-E Grading Guidelines, you're expected to explain grades that are marked down.  This is more important in cases when we asked you questions and you did not respond.
The answer helped me partially in resolving the issue and I went through a different way to come up with a solution.
But we asked you for some help in order to help you, and you just ignored the request.  For two months!  How does that translate into a marked-down grade?  If you believe this fits into the E-E grading guidelines, I would love to hear your explanation!