Jiin Posted May 17, 2010 Share Posted May 17, 2010 Thank Ya, Thank Ya very much! Env: php5 server side Linux My code is supposed to: 1) Accept multiple search parameters from an HTML form 2) Search through xml file looking for nodes that match all search parameters 3) Display matched xml nodes The code works, it just seems REALLY INEFFICIENT. Does anybody have any suggestions? <?php $xmlfile = simplexml_load_file('master.xml'); //print_r($xmlfile); // foreach($xmlfile->property as $item) { if ( ("$item->address" == "2012 Cosmic Highway") && ("$item->state" == "Florida") && ("$item->city" == "Tampa") //**I actuall have 30+ search parameters to match, so my IF statement becomes very LONG ) { echo "FOUND MATCH FOR:<br /> $item->state <br /> $item->city <br /> $item->address"; } } ?> Link to comment https://forums.phpfreaks.com/topic/202080-matching-multiple-xml-values-with-if-statement/ Share on other sites More sharing options...
Jiin Posted May 17, 2010 Author Share Posted May 17, 2010 Here is an example XML node I am matching against: <property> <mls_id>007</mls_id> <other_id>4321</other_id> <country>USA</country> <state>Florida</state> <city>Tampa</city> <zip>33624</zip> <address>310 Big Blvd</address> <type>Single Family Home</type> <for_sale>yes</for_sale> <price>200000</price> <for_rent>yes</for_rent> <rent>800</rent> <deposit>800</deposit> <year_built>1990</year_built> <square_feet>1500</square_feet> <gatecode>1212</gatecode> <lockbox>yes</lockbox> <lockbox_number>1221</lockbox_number> <date_available>10/10/2010</date_available> <listing_agent>Jiin</listing_agent> <listing_agent_phone>555-555.1212</listing_agent_phone> <listing_agent_email>[email protected]</listing_agent_email> <property_manager>Tampa Home Locators</property_manager> <owner_name>Bob Dobalina</owner_name> <owner_phone>555-555.1010</owner_phone> <owner_email>[email protected]</owner_email> <tenant>Joe Blow</tenant> <description>Beautiful Single Family Home</description> <bedrooms>3</bedrooms> <bathrooms>3</bathrooms> <listing_date>10/10/2010</listing_date> <listing_expdate>10/10/2011</listing_expdate> <listing_last_modified>10/10/2010</listing_last_modified> <feature>na</feature> <image1>/images/mypic.jpg</image1> <image2>na</image2> <image3>na</image3> <image4>na</image4> <general_comments>Beautifly ensconced in the mountains</general_comments> <showing_comments>This one is going to sell quickly!</showing_comments> <submit>submit</submit> <Update>submit</Update> </property> Link to comment https://forums.phpfreaks.com/topic/202080-matching-multiple-xml-values-with-if-statement/#findComment-1059684 Share on other sites More sharing options...
jdavidbakr Posted May 17, 2010 Share Posted May 17, 2010 My first thought is to use a database lookup, but I'm not sure what exactly you're doing in each condition or how much code is each condition block. My second thought is to use a switch statement off of the MLS ID: foreach($xmlfile->property as $item) { switch($item->mls_id) { case '007': ... break; case '009': ... break; } } My third thought is to consider if, say you're just including an external file in each condition, naming the include files by the msl_id, then you can: foreach($xmlfile->property as $item) { $file = "{$item->mls_id}.php"; if (file_exists($file)) { include($file); } else { echo "File not found"; } } Really, a lot depends on what you're doing in each condition. Link to comment https://forums.phpfreaks.com/topic/202080-matching-multiple-xml-values-with-if-statement/#findComment-1059716 Share on other sites More sharing options...
Jiin Posted May 17, 2010 Author Share Posted May 17, 2010 Thank you jdavidbakr! I really like your ideas and will be using some of them in other parts of my code but I don't fully understand how to apply them to this situation. Let me explain again and maybe we can meet in the middle. Imagine a person wanting to buy a new home comes to our website and fills out a HTML form to help them with their search. The form has 30 different fields they can use to tailor their home search. I don't know which ones they are going to use until my script accesses $_POST['whatever'] vars. So they won't always use the mls_id to search. The script then takes the multiple search criteria and only shows xml nodes(i.e. properties) that match ALL the search criteria. The XML I posted above is an example of ONE xml node in a much larger XML file. As it now, I am using multiple IF (price <= x) && (bedrooms => 3) && (exp) && (exp) && (exp).... this is very ugly. Looking for better way && XML solution Thanks again for the reply. Link to comment https://forums.phpfreaks.com/topic/202080-matching-multiple-xml-values-with-if-statement/#findComment-1059737 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.