0
在這段代碼中,有兩個foreach。當我單獨使用它時,它運行良好。但是,它不能一起工作。我一直在努力解決這個問題了兩天..php,foreach在foreach
$urls = nl2br($this->input->post('urls'));
$result = explode("<br />", $urls);
$n = 0;
foreach($result as $row)
{
$n++;
$Google_Play_URL = $row;
$string = file_get_contents($Google_Play_URL);
$dom = new DOMDocument();
@$dom->loadHTML($string);
$anchors = $dom->getElementsByTagName('a');
$i = 0;
foreach ($anchors as $anchor)
{
$i++;
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.'")';
$this->db->query($query);
}
}
}
}
}
當我發送多個值,該代碼只能保存一個數據。它假設保存多個數據。你能看到問題嗎?
您正在重置循環內的$ result,將其用於新的查詢結果 –
@MarkBaker我已經更改了$ result - > $ result_data,但它仍然只保存第一個數據。 – Jake