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

    Loop in wrong place or much more serious?

    Hi guys,

    My script does the following:

    Takes a textarea input, cleans it & splits it into an array. 1 text area line = 1 array value.
    Open an HTML page with DOM and find the 'Target' on the page.
    Returns it's position.

    Here's what should be output:

    Text Area Line 1 - Position (of Text Area Line 1)
    Text Area Line 2 - Position (of Text Area Line 2)

    However what's actually being output is:

    Text Area Line 1 - Position (of Text Area Line 1)
    Text Area Line 2 - Position (of Text Area Line 1)

    Why is it holding on to the position variable? I think it's simply because my loops are in the wrong place.

    Code:
    <?php
    
    // Get Textarea string and trim spaces & blank lines
    $textarea = $_POST['phrase'];
    $textarea = trim($textarea, " \r\n");
    
    // Explode textarea on newlines to get individual search phrases
    $textarea_array = explode("\n", $textarea);
    
    //
    //Get site to look for (Target) & force removal of http:// & http://www. to be user friendly.
    // Removal is case insensitive.
    //
    
    $Target = $_POST['target'];
    
    function remove_http($Target = '')
    {
    return(str_ireplace(array('http://www.','http://'), '', $Target));
    }
    $Target = remove_http($Target);
    
    //
    //
    //Main Foreach loop - takes each line of text area and searches URL for results.
    //
    //
    
    foreach ($textarea_array as $XQuery) {
    
    //Replace space ( ) with + to ensure compatibility in the next step
    $XQuery = str_replace(" ","+",$XQuery);
    
    $html = "http://www.mysite.co.uk/search?api=" .$XQuery ."&productvalid=1";
    $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))
         {
    //
    // Result found, Position is the key + 1 (first array value is zero).
    // Then further define it to include the nice text, this'll make it easier to echo later.
    //
    $Position = $key + 1;
    //         break;
        }
    else {
    //
    // Result not found, set $Position to not found.
    //
    $Position = "Not Found";
    }
    }  
    //
    // We're out of the loop, so tell the good people the results!
    //
    echo $XQuery;
    echo "-";
    echo $Position;
    }
    ?>

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    It appears to be a loop problem. Here's a trimed version that may make it easier to see:
    PHP Code:
    foreach ($textarea_array as $XQuery) {
      foreach (
    $aTag as $val) {
        
    // set $arr
      
    }
      foreach(
    $arr as $key => $value)
      }
      
    // output

    See, each pass of the outer foreach re-initializes the $arr, so the inner loop that's grabbing the position is finding position 1 each time.
    Are you a Help Vampire?


  •  

    Posting Permissions

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