Jump to content

Loop in the wrong place, help please!!


doa24uk

Recommended Posts

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.

 

<?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;
}
?>

 

Link to comment
Share on other sites

try moving your echo statements up to the bottom of the previous loop so the actuall echo out in the same loop the decision is being made.

 

from this:

$Position = "Not Found";
}
}
echo $XQuery;
echo "-";
echo $Position;
} 

 

to this:

$Position = "Not Found";
}
echo $XQuery;
echo "-";
echo $Position;
}
}


HTH
Teamatomic

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.