xml parse with php... am i missing something? please help

Discussion in 'PHP' started by cgo85, Sep 5, 2008.

  1. #1
    The code below is suppose to output a list of neighborhoods for a given city. Right now it's taking the first neighborhood and repeating (what seems to be) infinitely (ex. eastside eastside eastside eastside.......). I need it to pull all neighborhoods (ex. eastside, westside, downtown, south hill.......). Any help is appreciated


    <?php
    
    
    function marketwatch($city1, $state1)
    {
        // Setup query string parameters 
        $apikey = 'MyKey';
        $request = 'http://api.trulia.com/webservices.php?library=LocationInfo';
        $request .= '&function=getNeighborhoodsInCity&city='.urlencode ($city1);
        $request .= '&state='.$state1;
        $request .= '&apikey='.$apikey;
        
        // Make the request
        $response = file_get_contents($request);
    
        // Retrieve HTTP status code
        list($version,$status_code,$msg) = explode(' ',$http_response_header[0], 3);
    
        // Check the HTTP Status code
        if($status_code != 200) 
        {
            die('Your call to Trulia Web Services failed: HTTP status of:' . $status_code);
        }
    
        // Create a new DOM object
        $dom = new DOMDocument('1.0', 'UTF-8');
    
        // Load the XML into the DOM
        if ($dom->loadXML($response) === false) 
        {
            die('XML Parsing failed');
        }
    
        // Get the first searchResultsURL XML node
        $searchResultsURL = $dom->getElementsByTagName('searchResultsURL')->item(0);
    
    
        // Write out our table header w/ the City / State the data is for
        echo '<table width="100%" cellspacing="0">';
        echo '<tr><td><b>Neighborhoods ';
        echo '<a href="'.$searchResultsURL->nodeValue.'">'.$city1.', '.$state1.'</a>';
        echo '</b></td></tr>';
        echo '<tr><td class="center"><br>';
        echo '</td><td class="center">';
        echo '</td><td class="center"><br>';
        echo '</td></td>';
    
        // Get the first listingStat XML node
        $neighborhood = $dom->getElementsByTagName('neighborhood')->item(0);
    
    
        // Write out our table rows w/ data
        while($neighborhood) 
        {
            $i++;
            $neigh =  $neighborhood->getElementsByTagName('name')->item(0);
            if (($i % 2) == 1) 
            {
                echo '<tr bgcolor="#FAF6E4"><td class="center">';
            }
            else
            {
                echo '<tr><td class="center">';
            }
    
            print_r($neigh->nodeValue);
            echo '</td></tr>';
    
            $neigh = $neigh->nextSibling;
        }
    
        // Finish off our table w/ Trulia attribution
        echo '</table><br>';
        echo '<a href="http://www.trulia.com" target="_top" title="Trulia Real Estate Search">';
        echo '<img src= "http://images.trulia.com/images/logos/trulia_logo_70x42.jpg" alt="Search Real Estate and Homes for Sale" border="0" />';
        echo '</a>';
    }
    
    
    ?>
    <?php
    marketwatch('Seattle', 'WA');
    ?>
    PHP:

     
    cgo85, Sep 5, 2008 IP
  2. lanmonkey

    lanmonkey Active Member

    Messages:
    549
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    60
    #2
    lanmonkey, Sep 5, 2008 IP
  3. cgo85

    cgo85 Peon

    Messages:
    380
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    that didn't work... but I was thinking along the same lines as that.

    for that part, below is an excerpt of something I have working correctly parsing a different xml. If you can see where the ->firstChild line is I took that out above code because there is no child element for 'neighborhood'.


        // Get the first listingStat XML node
        $listingStats = $dom->getElementsByTagName('listingStats')->item(0);
        $listingStat = $listingStats->firstChild;
    
        // Write out our table rows w/ data
        while($listingStat) 
        {
            $i++;
            $weekEndingDate =  $listingStat->getElementsByTagName('weekEndingDate')->item(0);
            $subcategory = $listingStat->getElementsByTagName('subcategory')->item(0);
            $medianListingPrice = $subcategory->getElementsByTagName('medianListingPrice')->item(0);
            $numberOfProperties =  $subcategory->getElementsByTagName('numberOfProperties')->item(0);
            if (($i % 2) == 1) 
            {
                echo '<tr bgcolor="#FAF6E4"><td class="center">';
            }
            else
            {
                echo '<tr><td class="center">';
            }
    
            print_r($weekEndingDate->nodeValue);
            echo '</td><td class="center">';
            print_r(number_format($numberOfProperties->nodeValue));
            echo '</td><td class="center">$';
            print_r(number_format($medianListingPrice->nodeValue));
            echo '</td></tr>';
    
            $listingStat = $listingStat->nextSibling;
        }
    PHP:
     
    cgo85, Sep 5, 2008 IP