xml parsing... I'm missing something. But can't figure out what!

Discussion in 'XML & RSS' 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