Guest

Untitled

By: a guest on Jan 28th, 2012  |  syntax: None  |  size: 2.51 KB  |  hits: 26  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. <?php
  2.  
  3. error_reporting(E_ALL);
  4.  
  5. $cars = getCarsDB();
  6.  
  7. $mk = 1;
  8. $mdl = 2;
  9. $yr = 3;
  10. $i = 1;
  11. foreach ($cars as $make => $value)
  12. {
  13.     if($make == 'BMW' || $make == 'Mercedes-Benz' || $make == 'Audi' || $make == 'Porsche' || $make == 'Volkswagen')
  14.     {
  15.         $mk = $i;
  16.         echo "INSERT INTO drupal_term_data (vid,name,description) VALUES ('2','$make', '$make');
  17.         ";
  18.         echo "INSERT INTO drupal_term_hierarchy (tid,parent) VALUES ('" . $i++ . "','0');
  19.         ";
  20.         //$mk++;
  21.     }
  22.     else
  23.     {
  24.         continue;
  25.     }
  26.    
  27.     foreach ($value as $model => $value1)
  28.     {
  29.         $mdl = $i;
  30.         echo "INSERT INTO drupal_term_data (vid,name,description) VALUES ('2','$model', '$model');
  31.         ";
  32.         echo "INSERT INTO drupal_term_hierarchy (tid,parent) VALUES ('" . $i++ . "','" . $mk . "');
  33.         ";
  34.         foreach ($value1 as $year)
  35.         {
  36.             echo "INSERT INTO drupal_term_data (vid,name,description) VALUES ('2','$year', '$year');
  37.             ";
  38.            
  39.             echo "INSERT INTO drupal_term_hierarchy (tid,parent) VALUES ('" . $i++ . "','" . $mdl . "');
  40.             ";
  41.         }
  42.         $mdl++;
  43.     }
  44.     $mk++;
  45. }
  46.  
  47. function getCarsDB()
  48. {
  49.     $file = file_get_contents('http://scripts.kbb.com/kbb/ymmData.axd?VehicleClass=UsedCar');
  50.    
  51.     $file = explode("\n",$file);
  52.    
  53.     $patternMake = '/ymUsed_\[\d{4}\]\s*=\s*\'([^\']+)\'/';
  54.     $patternModel = '/ymmUsed_\["(\d+)~(\d+)"\]\s*=\s*"([^"]+)"/';
  55.    
  56.     foreach($file as $row)
  57.     {
  58.         if(preg_match($patternMake,$row,$matched))
  59.         {
  60.             $tmpMakes = explode(',',$matched[1]);
  61.             foreach($tmpMakes as $str)
  62.             {
  63.                 list($id,$name) = explode("|",$str);
  64.                 $arrMakes[$id] = $name;
  65.             }
  66.         }
  67.        
  68.         unset($str);
  69.        
  70.         if(preg_match($patternModel,$row,$matched))
  71.         {
  72.             $year = $matched[1];
  73.             $make_id = $matched[2];
  74.             $models = $matched[3];
  75.             $tmpModels = explode(',',$models);
  76.             foreach($tmpModels as $str)
  77.             {
  78.                 list($id,$model_name) = explode("|",$str);
  79.                 $make_name = $arrMakes[$make_id];
  80.                 $arrModels[$make_name][$model_name][$year] = $year;
  81.             }
  82.         }
  83.     }
  84.    
  85.     ksort($arrModels);
  86.     foreach ($arrModels as &$make)
  87.     {
  88.         ksort($make);
  89.         foreach ($make as &$model)
  90.         ksort($model);
  91.     }
  92.    
  93.     return $arrModels;
  94. }