Snippets

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

Navigation

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 

Comments on this snippet

gravatar icon
#1 Jill Elaine on 2008-04-11 at 04:37

I am interested in using this but am concerned about how many cities will be returned. I am wondering if I could restrict the cities to only those with a larger population? I looked at the citytowninfo.com site and see they do keep information about population, but couldn't find any hints there about how to add this to my criteria? Can you help?

You need to create an account or log in to post a comment or rate this snippet.