如何使用foreach进行连续的mysql插入?

时间:2016-01-19 04:11:59

标签: php jquery mysql arrays

我正在尝试将多个网址(url_array)插入数据库。

首先,我需要从网址获取一些信息(网址标题,网址图片,...缩写)。我正在使用SimpleHtmlDom这样做。

所以我把它放在foreach循环中。我希望它在每次迭代后插入,直到数组结束。

如果网址不正确,则应跳至数组中的下一个网址。

我还希望在最后(最后一次迭代)一些json成功消息通过jquery解析回来。

对于我在下面的代码,它只插入有时只有2或3个数组中仍有更多网址。

这是我的代码:

 $txturls = $_POST['bulkurls'];
          $urlsArray = array_map('trim', explode(',', $txturls));
          //var_dump($urlsArray);
          $i = 0;
          $len = count($urlsArray);
          foreach($urlsArray as $url){
            $curl = curl_init($url);
            curl_setopt($curl, CURLOPT_NOBODY, true);
            $result = curl_exec($curl);
            if ($result !== false) 
            {
              $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);  
              if ($statusCode == 404) 
              {
                $checkExist = "URL Not Exists";
              }
              else
              {
                 $checkExist = "URL Exists";
              } 
            }
            else
            {
              $checkExist = "URL not Exists";
            }
        if($checkExist === "URL Exists"){

         $html = SimpleHtmlDom::file_get_html($url);
            foreach($html->find('title') as $element) 
            {
                 $urltitle = $element->plaintext;
            }
            $tags = get_meta_tags($url);
            $description = $tags['description']; 
            if(strlen($description) < 1){$description = $urltitle .' '.'check title';};
                    $images = array();
        foreach($html->find('meta[property=og:image]') as $element) {
         if(!preg_match('/blank.(.*)/i', $element->content) && filter_var($element->content, FILTER_VALIDATE_URL))
            {
            $images[] = url_to_absolute($linkurl, $element->content);
            }

        }

        foreach ($images as $ext){
            //$imagesize = getimagesize(''.$ext.'');
            if (pathinfo($ext, PATHINFO_EXTENSION)) {
            $image = "<img src=\"$ext\" alt=\"$urltitle\">";
            break;
        }
        }

    $linkurl = $url;
    $checkIfUrlExist = $this->_modellinks->checkUrlExist($linkurl);
        if($checkIfUrlExist == false){
    $this->_modellinks->addBulkLinks($data['log_username'], $ipaddress, $linkurl, $urltitle, $subafrolinks, $keywords, $description, $image);

        }
    } else {
        continue;
    }
    if ($i == $len - 1) {
    // last
    $output = json_encode(array('type'=>'message', 'text' => 'Thanks for your submission! Your URL has been submitted successfully  And its under review . <strong>You will be notified when your post is published online. This usually takes less than 1 hour.<br> Here is your preview link'));
            die($output);
}
$i++;

    }

1 个答案:

答案 0 :(得分:0)

由于它插入了2-3个URL,这意味着您的插入查询以及foreach循环都运行良好。

当你说数组中还有更多的URL时,你在谈论$ urlsArray吗?

您是否检查过是否为剩余的网址设置了“$ checkExists”标志? 为此,请尝试在新数组$ arGoodUrl中将$ checkExists =='URL Exists'推送到这些URL,然后对其进行处理。那么你会有更好的主意。