Snippets

Create an account or login to be able to add, comment and rate snippets.

Navigation

Refine Tags

Snippets tagged "city" Snippets tagged "city"

Batch script to populate city and state tables

Add this to your schema.yml

  state:
    id:
    name:          varchar(50)
  city:
    id:
    state_id:
    name:          varchar(50)
 

Create a batch/load_citystates.php

define('SF_ROOT_DIR',    realpath(dirname(__FILE__).'/..'));
define('SF_APP',         'frontend');
define('SF_ENVIRONMENT', 'dev');
define('SF_DEBUG',       true);
 
require_once(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php');
 
sfContext::getInstance();
 
// initialize database manager
$databaseManager = new sfDatabaseManager();
$databaseManager->initialize();
 
echo 'Loading States - ';
 
$page = file_get_contents('http://www.citytowninfo.com/places');
 
preg_match_all('@<tr><td><a href="http://www.citytowninfo.com/places/[^"]*"><strong>([a-z-A-Z ]*)</strong></a></td><td>[^<]*</td><td><a href="http://www.citytowninfo.com/places/[^/]*/[^"]*">[^<]*</a></td></tr>@i', $page, $states, PREG_SET_ORDER);
 
echo count($states). "\n";
 
for ($i = 0; $i < count($states); $i++)
{
    $state = new State();
    $state->setName($states[$i][1]);
    $state->save();
 
    $page  = file_get_contents('http://www.citytowninfo.com/places/' . str_replace(' ', '-', strtolower($state->getName())) . '/alphabetically/a-d');
    $page .= file_get_contents('http://www.citytowninfo.com/places/' . str_replace(' ', '-', strtolower($state->getName())) . '/alphabetically/e-h');
    $page .= file_get_contents('http://www.citytowninfo.com/places/' . str_replace(' ', '-', strtolower($state->getName())) . '/alphabetically/i-l');
    $page .= file_get_contents('http://www.citytowninfo.com/places/' . str_replace(' ', '-', strtolower($state->getName())) . '/alphabetically/m-p');
    $page .= file_get_contents('http://www.citytowninfo.com/places/' . str_replace(' ', '-', strtolower($state->getName())) . '/alphabetically/q-u');
    $page .= file_get_contents('http://www.citytowninfo.com/places/' . str_replace(' ', '-', strtolower($state->getName())) . '/alphabetically/v-z');
 
    preg_match_all('@<tr><td><a href="/places/[^/]*/[^/]*"><strong>([a-z-A-Z ]*)</strong></a></td><td>[^<]*</td><td>[^<]*</td></tr>@i', $page, $cities, PREG_SET_ORDER);
?>
Getting cities for <?php echo $state->getName(); ?> - <?php echo count($cities); ?>
 
<?php
    for ($j = 0; $j < count($cities); $j++)
    {
        $city = new City();
        $city->setName($cities[$j][1]);
        $city->setStateId($state->getId());
        $city->save();
    }
}
 
by Brandon Nason on 2008-02-27, tagged batch  city  state 
(1 comment)