php, foreach in foreach


In this code, there are two foreachs. It worked fine when I use it separately. However, it doesn't work together. I've been struggling with this problem for two days..

$urls = nl2br($this->input->post('urls'));
$result = explode("<br />", $urls);

$n = 0;
foreach($result as $row)
    $Google_Play_URL = $row;
    $string = file_get_contents($Google_Play_URL);

    $dom = new DOMDocument();
    $anchors = $dom->getElementsByTagName('a');

    $i = 0;
    foreach ($anchors as $anchor) 

        if ($anchor->nodeValue === 'Email Developer') {
            $email = str_replace('mailto:', '', $anchor->getAttribute('href'));

            if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
                echo $email;

                $id = $this->session->userdata(SESSION_USERID); 

                $country = 'US';
                $type = 'android';

                $query = 'SELECT idx FROM db_advertisers WHERE email = "'.$email.'"';
                $result = $this->db->query($query);

                if($result->num_rows() < 1)
                    $query = 'INSERT INTO db_advertisers (email, type, url, country, submit_user) VALUES ("'.$email.'", "'.$type.'", "'.$Google_Play_URL.'", "'.$country.'", "'.$id.'")';


When I send multiple values, this code only save one data. It supposes to save multiple data. Can you see the problem?




The only obvious issue is that in the inner foreach loop, you are setting

$result = $this->db->query($query);

which is the same variable you have in the outer foreach loop

foreach($result as $row)

Changing one of the variable names should fix the issue.

People are also looking for solutions to the problem: php - How do I adjust the timezone information when sending this iCal invite?


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.