Jump to content

Recommended Posts

Hey guys I need help in my php..

Im having trouble counting up how many times my category appears in my xml document. 

 

heres my code:


<?php
if (file_exists('vacancies.xml'))
{
$xml = simplexml_load_file('vacancies.xml');

if ($xml)
createmenu($xml);
}

function createmenu($xml)
{
echo "<select id='List'
onChange='Javascript:newLabSelection(this.options[this.selectedIndex].value)'>";

foreach ($xml->vacancy as $vacancy) {


	$myarray = array();
$key = "Engineering";
foreach($myarray as $key => $value) 
{ 
if(array_key_exists($key, $array))
{
			$myarray[$key]=$value;

}
else {
$myarray[$key]= 0;
	}
	}
			echo "<option value='".$vacancy->vid."'>"." $key "." $value </option>";

	}
	echo "</select>";

}


?>


 

 

this is pseudo-code that i have that may help:

 

create an empty array

loop for each vacancy

if the category is a key in the array

increment the value of the array element which has the category as

the key

else

create an array element with the category as the key and set its

value to 1

end loop

An empty array variable can be created with a statement like

$myarray = array();

You can check if a given value is a key in an array with the array_key_exists function

$key = "IT";

if (array_key_exists($key, $myarray)) {

// increment the value in $myarray[$key]

} else {

// set the value of $myarray[$key] to 0

}

So you now have an associative array storing the frequency of each category in the vacancy data. Now you

need to iterate through the array elements getting both the element key (category) and element value

(frequency). A variation on the foreach statement will do this

foreach ($myarray as $key => $value) {

// do something with $key (category) and $value (frequency)

}

Link to comment
https://forums.phpfreaks.com/topic/152926-php-help-with-xml/
Share on other sites

this is my vacancies.xml

 

 

<?xml version = "1.0"?>
<vacancies xmlns="http://www.cs.waikato.ac.nz/ap65/vacancies/vacancy">
<vacancy>
	<vid>VAC-2009-03-20-087</vid>
	<title>Assistant</title>
	<description>A job for an assistant</description>
	<company>Fairfax</company>
	<type>contract</type>
	<category>Engineering</category>
	<location>Hamilton</location>
	<salary>21.00</salary>
	<eid>EMP-298756</eid>
</vacancy>
<vacancy>
	<vid>VAC-2003-04-30-098</vid>
	<title>Admininstrator</title>
	<description>Working as the administrator</description>
	<company>Unisys</company>
	<type>permanent</type>
	<category>IT</category>
	<location>Aucklad</location>
	<salary>28.00</salary>
	<eid>EMP-123456</eid>
</vacancy>
<vacancy>
	<vid>VAC-2009-05-09-076</vid>
	<title>Customer Service</title>
	<description>Be able to communicate with customers</description>
	<company>Southern Cross</company>
	<type>contract</type>
	<category>Engineering</category>
	<location>Hamilton</location>
	<salary>15.50</salary>
	<eid>EMP-654321</eid>
</vacancy>
<vacancy>
	<vid>VAC-2007-10-06-051</vid>
	<title>Maintenance</title>
	<description>Maintenance of all manufacturing equipment</description>
	<company>Drake Industrial</company>
	<type>permanent</type>
	<category>Engineering</category>
	<location>Hamilton</location>
	<salary>20.00</salary>
	<eid>EMP-789654</eid>
</vacancy>
</vacancies>   

Link to comment
https://forums.phpfreaks.com/topic/152926-php-help-with-xml/#findComment-803913
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.