blingd Posted April 7, 2009 Share Posted April 7, 2009 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) } Quote Link to comment https://forums.phpfreaks.com/topic/152926-php-help-with-xml/ Share on other sites More sharing options...
josh the jenius Posted April 7, 2009 Share Posted April 7, 2009 misread the question, sorry Quote Link to comment https://forums.phpfreaks.com/topic/152926-php-help-with-xml/#findComment-803165 Share on other sites More sharing options...
RichardRotterdam Posted April 7, 2009 Share Posted April 7, 2009 Can you show the xml or a part of it ? Quote Link to comment https://forums.phpfreaks.com/topic/152926-php-help-with-xml/#findComment-803309 Share on other sites More sharing options...
blingd Posted April 7, 2009 Author Share Posted April 7, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/152926-php-help-with-xml/#findComment-803913 Share on other sites More sharing options...
Maq Posted April 8, 2009 Share Posted April 8, 2009 You should use DOM and XPath, it would make things a hell of a lot easier. This is what they're made for anyway, XML. Quote Link to comment https://forums.phpfreaks.com/topic/152926-php-help-with-xml/#findComment-804087 Share on other sites More sharing options...
blingd Posted April 8, 2009 Author Share Posted April 8, 2009 Ohh yeah but wanted to do without DOM and xpath for now. Quote Link to comment https://forums.phpfreaks.com/topic/152926-php-help-with-xml/#findComment-804307 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.