コード例 #1
0
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new College();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['College'])) {
         $model->attributes = $_POST['College'];
         if ($model->save()) {
             $this->redirect(array('view', 'id' => $model->id));
         }
     }
     $this->render('create', array('model' => $model));
 }
コード例 #2
0
 /**
  * @todo 利用上面获取得的相关信息,把其写入文件或数据库,当然写入数据库之前要建立数据库
  * 
  * 结构如下: 
  * 
  * CREATE TABLE `college` (
  *	  `id` int(11) NOT NULL AUTO_INCREMENT,
  *	  `name` varchar(200) NOT NULL COMMENT '高校名称',
  *	  `type` int(11) NOT NULL COMMENT '高校类型',
  *	  `province` int(11) NOT NULL COMMENT '高校所在省份',
  *	  `homepage` varchar(200) NOT NULL COMMENT '高校主页\r\n',
  *	  PRIMARY KEY (`id`),
  *	  KEY `province` (`province`)
  *	) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
  *
  *注意:这里只是Yii版的范例,如果需要使用,则需要对应修改一下下面的代码
  */
 public static function generateCollegesInfo($output = 'file')
 {
     $str = '';
     //获取网址
     $urls = self::getCollegesLocation();
     foreach ($urls as $province => $url) {
         /**获取provinceID*/
         $criteria = new CDbCriteria();
         $criteria->addSearchCondition('region', $province);
         $region = Region::model()->find($criteria);
         //			UtilHelper::dump($region->attributes);
         $provinceID = $region->id;
         //			UtilHelper::dump($province.'-=-'.$provinceID);
         foreach ($url as $k => $v) {
             $type = $k;
             //				UtilHelper::dump($province.'-'.$k.'=>'.$v);
             $colleges = self::getColleges($v);
             //				UtilHelper::dump($colleges);
             foreach ($colleges as $name => $homepage) {
                 /**在这里把要写入数据库的信息写入文件**/
                 $str .= $province . "\t" . $type . "\t" . $name . "\t" . $homepage . "\r\n";
                 $college = new College();
                 $college->province = $provinceID;
                 $college->type = $type;
                 $college->name = $name;
                 $college->homepage = $homepage;
                 if ($college->save()) {
                     echo 'yes-' . $college->id . '<br />';
                 } else {
                     echo CHtml::errorSummary($college);
                 }
             }
         }
     }
     if ($output == 'file') {
         $fp = fopen('./public/colleges.txt', 'w+');
         fwrite($fp, $str);
         fclose($fp);
     }
 }
コード例 #3
0
 public function actionTest4()
 {
     $filename = './public/highscholls.txt';
     $school = file($filename);
     $school = array_map('check', $school);
     //		foreach ($school as $k=>$v)
     //		{
     ////			$school = str_replace($ufo, '',str_replace("\r", '',str_replace("\n", '', $v),$v),$v);
     //
     ////			$school[$k] = str_replace("\r", '',str_replace("\n", '', $v),$v);
     //			if (intval($v)||trim($v) == ''||trim($v)==' '||trim($v) ==' '||strpos(' ', $v))
     //			{
     //				unset($school[$k]);
     //			}
     //		}
     $school = array_chunk($school, 2);
     foreach ($school as $value) {
         //			$todo[$value[1]] = $value[0];
         $province = $value[0];
         $name = $value[1];
         echo $province;
         echo $name;
         /**获取provinceID*/
         $criteria = new CDbCriteria();
         $criteria->addSearchCondition('region', $province);
         $region = Region::model()->find($criteria);
         UtilHelper::dump($region->attributes);
         $provinceID = $region->id;
         $type = College::COLLEGE_TYPE_GAOZHONG;
         $college = new College();
         $college->province = $provinceID;
         $college->type = $type;
         $college->name = $name;
         if ($college->isNewRecord && $college->save()) {
             echo 'yes-' . $college->id . '<br />';
         } else {
             echo CHtml::errorSummary($college);
         }
     }
     //		UtilHelper::dump($todo);
 }