1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

FOREACH Loop Problem

Discussion in 'PHP & Perl' started by maple_toast, Mar 30, 2011.

  1. maple_toast

    maple_toast Newbie

    Joined:
    Jul 23, 2008
    Messages:
    30
    Likes Received:
    50
    I'm trying to write a script that will check to see if a specifc url is indexed within Google, Yahoo, & Bing.

    I've got it to work using a single url, however when I add another url, the problems begin. It doesn't matter if I'm checking 2 or 200 urls, only the last url on the list will work. The others return errors.

    Here's my FOREACH LOOP statement:
    Code:
               	if(isset($_POST['submit'])) {
    		$input = trim($_POST['url']);
    		$urls	= explode("\n", $input);
    
    		
    
    		foreach($urls as $url) { 
    
    //Set my variables here and then...
    
    $handle_google  = fopen("$google", "r");
                                    $contents_google = stream_get_contents($handle_google);
                                    fclose($handle_google);
                                    if(strstr($contents_google, $needle) !== false) {
                                            echo '<a href="'.$google.'" target="_blank">found</a>';
                                    }
                                    else {
                                            echo 'not found';
                                    }
                                    echo '</td><td>';
                                   
                                    $handle_yahoo = fopen("$yahoo", "r");
                                    $contents_yahoo = stream_get_contents($handle_yahoo);
                                    fclose($handle_yahoo);
                                    if(strstr($contents_yahoo, $needle_yahoo) !== false) {
                                            echo '<a href="'.$yahoo.'" target="_blank">found</a>';
                                    }
                                    else {
                                            echo 'not found';
                                    }
                                    echo '</td><td>';
                                   
                                    $handle_bing = fopen("$bing", "r");
                                    $contents_bing = stream_get_contents($handle_bing);
                                    fclose($handle_bing);
                                    if(strstr($contents_bing, $needle_bing) !== false) {
                                            echo '<a href="'.$bing.'" target="_blank">found</a>';
                                    }
                                    else {
                                            echo 'not found';
                                    }
    I know the variables work (and the code works) for one single url. I just can't seem to get the rest to work.


    Any help will be appreciated....
     
    Last edited: Mar 30, 2011
  2. neo3187

    neo3187 Senior Member

    Joined:
    Sep 19, 2010
    Messages:
    868
    Likes Received:
    360
    First of all why have u used foreach loop?? Do you want to let the user check multiple urls? seperated by a new line?? If yes then..

    In this part below, where are you using the variable url that you declared in foreach....?? I dont see it anywhere, so ofcourse the loop isnt doing anything at all cz eventhough the loop is running but you are not using the variable which is changing every time a loop completes a cycle...

    Code:
    $handle_google  = fopen("$google", "r");
                                    $contents_google = stream_get_contents($handle_google);
                                    fclose($handle_google);
                                    if(strstr($contents_google, $needle) !== false)
     
  3. maple_toast

    maple_toast Newbie

    Joined:
    Jul 23, 2008
    Messages:
    30
    Likes Received:
    50
    I am using a multi-line submission, so I'm checking each url one at a time. I've taken the variables out for brevity sake. Since the code works for a single line submission, I thought be problem would be with the foreach loop.

    Am I mistaken here?
     
  4. gnote

    gnote Registered Member

    Joined:
    Mar 10, 2009
    Messages:
    80
    Likes Received:
    6
    Occupation:
    Programmer
    Location:
    USA
    I'm going to take a wild guess here and say you need to set your $needle vars inside the for loop? Whatever you're doing when you set those variables, your code is definitely not doing anything with the $url var.
     
  5. coderjeff

    coderjeff Junior Member

    Joined:
    Nov 20, 2007
    Messages:
    130
    Likes Received:
    26
    Location:
    USA
    It's hard to debug your code if you have removed part of it.

    Where are you using the variable $url within your loop? That is the one that is changing each time and it doesn't appear to be used. You probably need to set your variables (or at least some of them) within the loop rather than before the loop.
     
  6. artizhay

    artizhay BANNED BANNED

    Joined:
    Nov 21, 2010
    Messages:
    1,867
    Likes Received:
    1,335
    It would seem prudent to post your whole code. I mean, would you ask someone why a building collapsed but only give them half the blueprints? No.

    The only thing I can see is that I don't think you need the !== operator for strstr. You only need that for strpos. So just != or !strstr. But for some reason, you thought it would be a good idea to change your variables, so beyond your operator usage, I cannot help you.

    Plus you have no closing bracket...I'm assuming you have that in your code and just didn't feel like pasting it...
    And please tell us the output you get when you do run the code with the array and loop...
     
    Last edited: Mar 30, 2011
  7. maple_toast

    maple_toast Newbie

    Joined:
    Jul 23, 2008
    Messages:
    30
    Likes Received:
    50
    Unfortunately, I'm limited in posting urls, and I've tried to go through the code and remove them to meet this limitation, but I can't seem to make the input form happy enough to post.

    In the code, my variables are set within the foreach loop. Like I said, for some reason, it works great on either a single url, or if looping multiple urls, it will not work but only for the last iteration. Even if the urls are identical.

    Thanks,
     
  8. maple_toast

    maple_toast Newbie

    Joined:
    Jul 23, 2008
    Messages:
    30
    Likes Received:
    50
    BTW, I saw that I had the code incorrect in my first post. The variables are set within the foreach loop. (I changed the code).

    So wouldn't it make sense that if it can run one iteration fine, it should do the same for all the following?

    Do I need to unset() all variables before each loop is complete?
     
  9. artizhay

    artizhay BANNED BANNED

    Joined:
    Nov 21, 2010
    Messages:
    1,867
    Likes Received:
    1,335
    Okay...well, what is the output for a loop?
    And try to post your variables without any indication of it being a URL.
    So if you have something like:
    Code:
    http://www.google.com/search?hl=en&source=hp&biw=1366&bih=643&q=keyword_here&aq=f&aqi=&aql=&oq=
    in a variable, try to post it like:
    Code:
    google/search?hl=en&source=hp&biw=1366&bih=643&q=keyword_here&aq=f&aqi=&aql=&oq=
    so that we can get a general idea of what you're doing. I don't think the filter will catch that. But try the operator change I suggested above first and see if that makes a difference when you run the code.

    @your edit&second post: You don't need to unset your variables unless you're setting cookies or session variables. But I don't know what your variables are...
     
    Last edited: Mar 30, 2011
  10. coderjeff

    coderjeff Junior Member

    Joined:
    Nov 20, 2007
    Messages:
    130
    Likes Received:
    26
    Location:
    USA
    <bad suggestion removed>

    You could check to be sure by adding
    Code:
    print_r($urls);
    after the explode to take a look at the array.
     
    Last edited: Mar 30, 2011
  11. artizhay

    artizhay BANNED BANNED

    Joined:
    Nov 21, 2010
    Messages:
    1,867
    Likes Received:
    1,335
    Ah, nice catch on that. That may be the issue he is having, but does trim apply to \n characters within the data? I thought it only applied to whitespace on the ends of the data.
     
  12. artizhay

    artizhay BANNED BANNED

    Joined:
    Nov 21, 2010
    Messages:
    1,867
    Likes Received:
    1,335
    I just wrote a quick test for trim and I don't think that's the issue.

    Code:
    Code:
    <?
    if (isset($_POST['submit'])) {
    	$data = trim($_POST['data']);
    	$dataArr = explode("\n", $data);
    	print_r($dataArr);
    }
    ?>
    <form action="newline.php" method="post">
    <textarea name="data"></textarea><br>
    <input type="submit" name="submit">
    </form>
    Input:
    Code:
    Line 1
    Line 2
    Line 3
    Line 4
    Output:
    Code:
    Array ( [0] => Line 1 [1] => Line 2 [2] => Line 3 [3] => Line 4 )
    Worked fine.
     
    • Thanks Thanks x 2
  13. coderjeff

    coderjeff Junior Member

    Joined:
    Nov 20, 2007
    Messages:
    130
    Likes Received:
    26
    Location:
    USA
    Thanks artizhay for checking.

    If the input lines are making it into the array ok, then we really need to see how the $url variable is being used in the loop (and to be sure that the '$urls' array isn't being modified.)
     
    • Thanks Thanks x 1
  14. maple_toast

    maple_toast Newbie

    Joined:
    Jul 23, 2008
    Messages:
    30
    Likes Received:
    50
    Thanks for all the help so far.

    I don't get any errors in the code, but the code isn't show the results correctly.

    For instance, when I run a multi-line submission on the same exact url, the code will return the following results.

    Checking the results manually, will reveal that the site is found in all three search engines.

    URL
    url: mydomain not found not found not found
    url: mydomain found found found


    I know it's difficult to help without seeing the code. I just tried to post the code again, but it took me forever to try and remove whatever I needed to in order for it to post.

    Hopefully, I'll have my post count up soon enough to post it straight away.

    Thanks,
     
  15. maple_toast

    maple_toast Newbie

    Joined:
    Jul 23, 2008
    Messages:
    30
    Likes Received:
    50
    If anybody wants to know, I found the solution (finally)...

    I had to trim each url from the foreach loop. Now it works like a charm! Thanks for the help all!

    Code:
    foreach($urls as $url) {
    			$url	= trim($url);
     
  16. coderjeff

    coderjeff Junior Member

    Joined:
    Nov 20, 2007
    Messages:
    130
    Likes Received:
    26
    Location:
    USA
    Good to hear that you tracked it down. Always turns out to be something simple :(