if (!$rowsAffected) {
    break;
}
$total = $rowsAffected;
foreach ($resultArry as $val) {
    $campaign = new GoogleCampaign($db);
    $campaign->ga_campaign_name = "Real Estate Sale " . $val['l_geo_city'];
    $campaign->save();
    echo $c . "/" . ++$i . "/" . $total . "\r\n";
    $ag = new GoogleAdGroup($db);
    $ag->m_sMainTable = 'realestate';
    $ag->gag_l_id = $val['l_id'];
    $ag->createName();
    $ag->save();
    $ag->buildAds(array('GoogleAdRealEstateRU5', 'GoogleAdRealEstateRU6', 'GoogleAdRealEstateRU7', 'GoogleAdRealEstateRU12', 'GoogleAdRealEstateRU13'), $val);
    $ag->buildRealestateKeywordsRU($val);
    $ag->saveAds($fp, $campaign->ga_campaign_name);
    if (!$ag->is_has_ads) {
        echo "no good ads\r\n";
        continue;
    }
    $ag->m_aKeywords = array_unique($ag->m_aKeywords);
    foreach ($ag->m_aKeywords as $val2) {
        fputs($fp, "{$campaign->ga_campaign_name},,,,,," . $ag->gag_name . "," . MAX_CPC . "," . DEFAULT_MATCH_TYPE . ",{$val2}\r\n");
    }
    $c++;
    $main_db->executeSQL("SELECT *, count(*) as cnt FROM realestate\n\tWHERE \tl_deal_type = 1 and \n\t\t\tl_www_id = 1369 and \n\t\t\tl_isActive = 1 and \n\t\t\tl_d_price > 1000 and\n\t\t\tl_geo_city LIKE '%{$val['l_geo_city']}%' and\n\t\t\tl_assetType = 'Квартира'\n\tGROUP BY \n\t\tl_geo_city \n\tORDER BY \n\t\tcnt DESC \n\tLIMIT 1", $resultArry2, $rowsAffected2);
    echo $c . "/" . ++$i . "/" . $total . "\r\n";
    $ag->m_aKeywords = array();
    $ag->ads = array();
    $ag->buildAds(array('GoogleAdRealEstateRU8', 'GoogleAdRealEstateRU9', 'GoogleAdRealEstateRU10', 'GoogleAdRealEstateRU11', 'GoogleAdRealEstateRU12'), $resultArry2[0]);
 }
 $total = $rowsAffected;
 foreach ($resultArry as $val) {
     $asset_types = explode("/", $val['l_assettype']);
     foreach ($asset_types as $asset_type) {
         $val['l_assettype_en'] = assetTypeEn($asset_type);
         $val['l_assettype'] = $asset_type;
         echo $c . "/" . ++$i . "/" . $total . "\r\n";
         $ag = new GoogleAdGroup($db);
         $ag->m_sMainTable = 'realestate';
         $ag->gag_l_id = $val['l_id'];
         $ag->createName($val);
         $ag->save();
         $ag->buildAds(array('GoogleAdRealEstateRU9', 'GoogleAdRealEstateRU10', 'GoogleAdRealEstateRU11', 'GoogleAdRealEstateRU12', 'GoogleAdRealEstateRU13'), $val);
         //), $val);
         $ag->buildRealestateKeywordsRU($val);
         $ag->saveAds($fp, $campaign->ga_campaign_name);
         if (!$ag->is_has_ads) {
             echo "no good ads\r\n";
             continue;
         }
         //$ag->m_aKeywords = array_unique($ag->m_aKeywords);
         //echo "Keywords count = ". count($ag->m_aKeywords)." ";
         $kc = 0;
         foreach ($ag->m_aKeywords as $v) {
             echo $v['keyword'] . "\n\r";
             $kc++;
             $k++;
             $kk++;
             if ($k >= 2500) {
                 break;