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 2 of 2
  1. #1
    DoA
    DoA is offline
    New Coder
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Why is this outputting the wrong stuff?

    Code:
    $Target = "phrase 2"
    $textarea_array=array("phrase 1","phrase 2");
    
    foreach ($textarea_array as $XQuery) {
    
    
    $html = "http://www.mysite.co.uk/search?api=" .$XQuery;
    $dom = new DOMDocument;
    @$dom->loadHTMLFile($html);
    $xpath = new DOMXPath($dom);
    $aTag = $xpath->query('//h3[@class="r"]/a');
    foreach ($aTag as $val) {
         $arr[] = $val->getAttribute('href');
    }
    foreach($arr as $key => $value)
    {
    if(stristr($value, $Target))
    {
    $Position = $key;
    break;	
    }	
    else {
    $Position = "Not Found";
    }
    }
    echo $XQuery;
    echo "-";
    echo $Position;
    echo "<br><br>"; 
    }
    What should be being output is

    phrase 1 - position of phrase 1
    phrase 2 - position of phrase 2

    What is actually being output is

    phrase 1 - position of phrase 1
    phrase 2 - position of phrase 1

    And I just can see why!?

  • #2
    met
    met is offline
    Regular Coder
    Join Date
    Oct 2009
    Location
    United Kingdom
    Posts
    728
    Thanks
    4
    Thanked 119 Times in 119 Posts
    my guess is because of

    PHP Code:
    if(stristr($value$Target))
    {
        
    $Position $key;
        break;     
    // this 
    quote php ~

    break ends execution of the current for, foreach, while, do-while or switch structure.

    so it's only "for-eaching" once, hence why position is the same for both.

    edit ~ but this is just speculation


  •  

    Posting Permissions

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