XML Counting

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bijukon
Forum Newbie
Posts: 1
Joined: Sat Feb 13, 2010 3:59 am

XML Counting

Post by bijukon »

Hi

I've been working on a script that goes through an xml feed that contains property data. I've got most of the property details being collected correctly, however I'm struggling with getting some particulars (such as number of bathroom). The problem for me is that I need to tally up all the bathrooms (type=8) and showerooms (type=13). Some properties don't always have this data, so in such instances I take the number of bedrooms divided by 3 (result being rounded up).

The issue I'm getting is that I can't reliably get an accurate number of total bathrooms & Showerrooms, which I can pass out to the main section. The results vary from accurate to quite a bit out especially if a particular type is listed more than once to indicate multiple rooms, as this feed lists rooms varyingly. e.g. type 13 may be listed 2 or 3 times (with number being 1 in each case). any advice appreciated.


My code for this section is:

Code: Select all

foreach($advert->roomsList->room as $rooms) {
   if ($rooms->type=="1") {
   $bedrooms=$rooms->number;
   }
   if ($rooms->type=="8") {
   $bathroom=$rooms->number;
   }
   if ($rooms->type=="13") {
   $showerroom=$rooms->number;
   }
 
}
 
The data is presented as follows

Code: Select all

    <estate id="xxxxxx" type="1">      <reference>286834349</reference>      <mandate_num>1810</mandate_num>      <mandate_type>1</mandate_type>      <roomsList>         <room id="3948806" rank="1">            <type>1</type>            <comment lang="fr_FR"><![CDATA[]]></comment>            <area></area>            <number>3</number>            <coating></coating>            <floor></floor>         </room>         <room id="3948807" rank="2">            [b]<type>13</type>[/b]            <comment lang="fr_FR"><![CDATA[]]></comment>            <area></area>            [b]<number>1</number>[/b]            <coating></coating>            <floor></floor>         </room>         <room id="3948808" rank="3">            [b]<type>13</type>[/b]            <comment lang="fr_FR"><![CDATA[]]></comment>            <area></area>            [b]<number>1</number>[/b]            <coating></coating>            <floor></floor>         </room>         <room id="3948809" rank="4">            <type>8</type>            <comment lang="fr_FR"><![CDATA[]]></comment>            <area></area>            <number>1</number>            <coating></coating>            <floor></floor>         </room>         <room id="3948810" rank="5">            <type>30</type>            <comment lang="fr_FR"><![CDATA[]]></comment>            <area></area>            <number>1</number>            <coating></coating>            <floor></floor>         </room>         <room id="3948811" rank="6">            <type>1</type>            <comment lang="fr_FR"><![CDATA[]]></comment>            <area></area>            <number>1</number>            <coating></coating>            <floor></floor>         </room>         <room id="3948812" rank="8">            <type>1</type>            <comment lang="fr_FR"><![CDATA[]]></comment>            <area></area>            <number>1</number>            <coating></coating>            <floor></floor>         </room>         <room id="3948813" rank="9">            <type>10</type>            <comment lang="fr_FR"><![CDATA[]]></comment>            <area></area>            <number>1</number>            <coating></coating>            <floor></floor>         </room>         <room id="3948814" rank="10">            <type>41</type>            <comment lang="fr_FR"><![CDATA[]]></comment>            <area></area>            <number>1</number>            <coating></coating>            <floor></floor>         </room>         <room id="3948815" rank="11">            <type>29</type>            <comment lang="fr_FR"><![CDATA[]]></comment>            <area></area>            <number>1</number>            <coating></coating>            <floor></floor>         </room>         <room id="3948816" rank="12">            <type>1</type>            <comment lang="fr_FR"><![CDATA[]]></comment>            <area></area>            <number>1</number>            <coating></coating>            <floor></floor>         </room>         <room id="3948817" rank="13">            <type>18</type>            <comment lang="fr_FR"><![CDATA[]]></comment>            <area></area>            <number>1</number>            <coating></coating>            <floor></floor>         </room>         <room id="3948818" rank="7">            <type>43</type>            <comment lang="fr_FR"><![CDATA[]]></comment>            <area></area>            <number>1</number>            <coating></coating>            <floor></floor>         </room>      </roomsList>   </estate>

Thanks in advance for the help.
User avatar
a.heresey
Forum Commoner
Posts: 59
Joined: Wed Dec 13, 2006 7:31 pm
Location: Chesapeake, VA, US

Re: XML Counting

Post by a.heresey »

In your loop you're not adding, just assigning over and over.

Code: Select all

 
foreach($advert->roomsList->room as $rooms) {
   if ($rooms->type=="1") {
   //$bedrooms=$rooms->number;
   $bedrooms = $bedrooms+$rooms->number;
   }
   if ($rooms->type=="8") {
  // $bathroom=$rooms->number;
   $bathroom=$bathroom+$rooms->number;
   }
   if ($rooms->type=="13") {
   //$showerroom=$rooms->number;
   $showerroom=$showerroom+$rooms->number;
   }
 
}
 
Post Reply