Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-20-2010, 12:36 AM   PM User | #1
DoA
New Coder

 
Join Date: Mar 2010
Posts: 14
Thanks: 2
Thanked 0 Times in 0 Posts
DoA is an unknown quantity at this point
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!?
DoA is offline   Reply With Quote
Old 03-20-2010, 01:41 PM   PM User | #2
met
Regular Coder

 
Join Date: Oct 2009
Location: United Kingdom
Posts: 728
Thanks: 4
Thanked 119 Times in 119 Posts
met has a little shameless behaviour in the past
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
met is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:10 PM.


Advertisement
Log in to turn off these ads.