aarondurber01 Posted January 8, 2016 Share Posted January 8, 2016 I am using an expert agent XML feed to populate a clients estate agent website. Below is a snap shot of the nodes. I wish to loop through the rooms node using PHP to display the room name, then the size, then the description. Is there an easy way/simple solution to accomplish this within PHP? <property reference="xxxxx"> <main_advert></main_advert> <street>Kenilworth Walk</street> <brochure>xxxxx</brochure> <property_type>House</property_type> <rooms> <room name="Entrance hall"> <measurement_text/> <description> with radiator, carpet. </description> </room> <room name="Separate WC"> <measurement_text/> <description> with low level WC, hand basin, double glazed UPVC window, radiator, carpet. </description> </room> <room name="Lounge"> <measurement_text> 13' 10'' x 15' 8'' (4.21m x 4.77m) </measurement_text> <description> with double glazed UPVC french doors and windows to garden, carpet, two radiators. </description> </room> </rooms> </property> I am not a php expert, I'd appreciate any help or guidance. Quote Link to comment https://forums.phpfreaks.com/topic/300227-using-php-to-loop-through-xml-nodes/ Share on other sites More sharing options...
requinix Posted January 9, 2016 Share Posted January 9, 2016 Use SimpleXML. Example: $xml = new SimpleXMLElement("URL", 0, true); foreach ($xml->property as $property) { // $node["key"] for attributes $reference = (string)$property["reference"]; // $node->name for sub nodes $type = (string)$property->property_type; $street = (string)$property->street; // multiple elements are accessed by their shared name $rooms = count($property->rooms->room); // room is basically an array of <room>s echo "Property {$reference} is a {$type} on {$street} with {$rooms} room(s).\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/300227-using-php-to-loop-through-xml-nodes/#findComment-1529297 Share on other sites More sharing options...
aarondurber01 Posted January 9, 2016 Author Share Posted January 9, 2016 i'll give that a try thanks. Quote Link to comment https://forums.phpfreaks.com/topic/300227-using-php-to-loop-through-xml-nodes/#findComment-1529352 Share on other sites More sharing options...
aarondurber01 Posted January 11, 2016 Author Share Posted January 11, 2016 Still struggling. i am using wordpress 4.4, an expert agent feed and expert agent plugin to deliver the feed. I need to query the rooms node (as above) each room has a size and description node. i would like to display this data on a wordpress post page. i can pull individual node details and sub nodes using code i found with google search. <?php if(get_post_meta($post->ID, 'entrance_hall_name', true) || // this is what i have named the node for room name get_post_meta($post->ID, 'entrance_hall_desc', true) || // this is what i have named the node for room description get_post_meta($post->ID, 'entrance_hall_size', true) || // this is what i have named the node for room size ): ?> <?php if(get_post_meta($post->ID, 'entrance_hall_name', true)): ?> <b><?php echo get_post_meta($post->ID, 'entrance_hall_name', true); ?></b> <br> <?php if(get_post_meta($post->ID, 'entrance_hall_desc', true)): ?> <?php echo get_post_meta($post->ID, 'entrance_hall_desc', true); ?> <?php endif; ?> <br><br> <?php if(get_post_meta($post->ID, 'entrance_hall_size', true)): ?> <?php echo get_post_meta($post->ID, 'entrance_hall_size', true); ?> <? php echo "<br><br>"; ?> <?php endif; ?> <?php endif; ?> <?php if(get_post_meta($post->ID, 'sep_wc_name', true)): ?> <?php echo get_post_meta($post->ID, 'sep_wc_name', true); ?> <?php endif; ?> <br><br> <?php endif; ?> I want to loop through the rooms node returning the values for those that are there, not all rooms have a size all help appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/300227-using-php-to-loop-through-xml-nodes/#findComment-1529402 Share on other sites More sharing options...
requinix Posted January 11, 2016 Share Posted January 11, 2016 I don't see the connection. What does post metadata have to do with an XML feed? I thought you were trying to load an XML feed and display data from it? What does WordPress have to do with it? Quote Link to comment https://forums.phpfreaks.com/topic/300227-using-php-to-loop-through-xml-nodes/#findComment-1529405 Share on other sites More sharing options...
aarondurber01 Posted January 11, 2016 Author Share Posted January 11, 2016 my fault for bad explanations, new to this I have a clients website that runs wordpress 4.4, it uses an expert agent feed and the all in one import wordpress plugin to import the feed. I then need to display the node details. i have used the code above after a google search. to reiterate i am new to this and not an expert in PHP or xml. thanks Quote Link to comment https://forums.phpfreaks.com/topic/300227-using-php-to-loop-through-xml-nodes/#findComment-1529426 Share on other sites More sharing options...
requinix Posted January 11, 2016 Share Posted January 11, 2016 This might be turning into a "how do I do $x in WordPress" question... How much did you copy and where did you put it? Do you know what $post is? Is it coming from the imported data you/WP got from the XML? That code you have now: what does it do now and what is it supposed to be doing instead? Quote Link to comment https://forums.phpfreaks.com/topic/300227-using-php-to-loop-through-xml-nodes/#findComment-1529427 Share on other sites More sharing options...
aarondurber01 Posted January 11, 2016 Author Share Posted January 11, 2016 i got the code from here https://perishablepress.com/wordpress-custom-fields-tips-tricks/ $post is the variable for the post item/page, no its from the standard wordpress install, i think. the code pull the data from the node entrance_hall_name (which i have named) entrance_hall_name maps to the rooms node, room name attribute and displays on screen. Am i still explaining myself really badly? Quote Link to comment https://forums.phpfreaks.com/topic/300227-using-php-to-loop-through-xml-nodes/#findComment-1529432 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.