Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jul 2007
    Location
    Scotland
    Posts
    134
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Looping DomDocument

    Hi Guys,

    I'm trying to loop through the ciontents of an html page using domDocument, i can get the results back great but it only retrieves 1 result (because i'm not using a foreach)

    code:

    PHP Code:
    <?php
    if (isset($_GET['searchDeep']))
    {
      print 
    $searchString;
      
    // Deep search code
        
    $searchString str_replace" ","+",$searchString);
        
    $search_url "http://www.site.com/mkplSearchResult.htm?dores=true&includeKeywords=$searchString&firstResult=50";

        
    // make the cURL request to $search_url
        
    $ch curl_init();
        
    curl_setopt($chCURLOPT_USERAGENT'Firefox (WindowsXP) - Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6');
        
    curl_setopt($chCURLOPT_URL,$search_url);
        
    curl_setopt($chCURLOPT_FAILONERRORtrue);
        
    curl_setopt($chCURLOPT_AUTOREFERERtrue);
        
    curl_setopt($chCURLOPT_RETURNTRANSFER,true);
        
    curl_setopt($chCURLOPT_TIMEOUT30);
        
    $html curl_exec($ch);
        if (!
    $html) {
            echo 
    "<p class=\"fcs-message-error\">cURL error:" curl_error($ch) . " (Number " curl_errno($ch).")</p>";
        }
        
    curl_close($ch); 

          
    // parse the html into a DOMDocument  
            
    $dom = new DOMDocument();
            @
    $dom->loadHTML($html);

            
    $xpath = new DOMXPath($dom);            
            
    $paras $xpath->query("//div[@id='results']//tr/td[@class='details']/h4/a");
        
    $para $paras->item(0);
                
                
    $urlt $para->textContent;    
                    if(
    $urlt == '' $urlt == null) {
                        echo 
    '<p class="fcs-message-error">No Deep search products found!</p>';        
                        
                    } else {
                    
                    
    // Loop
                    
    foreach ($something as $something)
                    {
                    
                
    $url $para->getAttribute('href');
                
    $url str_replace("zzzzz""graham25s"$url);
                
                
    $xpath = new DOMXPath($dom);
                
    $paras $xpath->query("//div[@id='results']//td[@class='details']//div[@class='description']");
                
    $para  $paras->item(0);
                
    $description $para->textContent;    
                  
                
    $xpath = new DOMXPath($dom);            
                
    $paras $xpath->query("//div[@id='results']//td[@class='details']//h4/a");
                
    $para  $paras->item(0);
                
    $title $para->textContent;                    
                
                
    $link '<a rel="nofollow" href="'.$url.'">'.$title.'</a><br />';                    
                
                
    //print "<br/><strong>".$link."</strong><br/>".$description;
                
    print $link;
                    
                    }
                    
                }                    
    }
    ?>
    it's the foreach part i'm having trouble with i have put one in above where i think it should go, any help would be appreciated

    thanks a lot guys

    Graham

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,389
    Thanks
    13
    Thanked 353 Times in 349 Posts
    Quote Originally Posted by graham23s View Post
    it's the foreach part i'm having trouble with
    PHP Code:
    foreach ($something as $something
    really no idea, why that fails?
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    Regular Coder
    Join Date
    Jul 2007
    Location
    Scotland
    Posts
    134
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Hi Mate,

    I meant to to that believe it or not i was trying to show "i need something as something" its what that something is, is the problem

    cheers

    Graham

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,389
    Thanks
    13
    Thanked 353 Times in 349 Posts
    let’s put it this way, if you loop over something, making the first item something, whereover do you want to loop next (you can’t loop over an item, only arrays are valid)?
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •