yii2无法识别get变量

时间:2017-09-18 10:35:48

标签: php yii2

我的控制器中有$_GET['pro_id']变量,我在视图中调用它,但是有一个错误说

  

C:\ xampp \ htdocs \ reserve \ controllers \ SiteController.php中的“未定义索引:pro_id”。

这是我的sitecontroller.php

 public function actionIndex()

    { 

        if(!($_GET['pro_id']> 0)) $_GET['pro_id'] = 1;

        switch ($_GET['pro_id']) {
            case 1:
                $query1 = "select T1.dist_dis_id dis_id, T1.dist_name, ifnull(T2.stor_weight, 0) sum_weight from ( select dist_dis_id, dist_name from district_trans where dist_lang_id = 1 order by dist_name) T1 left join (select stor_dis_id, sum(stor_weight) stor_weight from storage where stor_year = ".date("Y")." group by stor_dis_id)T2 on T1.dist_dis_id = T2.stor_dis_id";
                $query2 = "select T0.sect_name, T0.sect_sec_id, ifnull(T1.stor_weight,0) sum_weight from
                  (SELECT sect_name, sect_sec_id FROM `section_trans` WHERE sect_lang_id = 1)T0
                  left join
                  (SELECT stor_sec_id, sum(stor_weight) stor_weight FROM `storage` WHERE stor_year =".date("Y")." group by stor_sec_id)T1
                  on
                  T0.sect_sec_id = T1.stor_sec_id";
                break;
            case 2:
                $query1 = "select T1.dist_dis_id dis_id, T1.dist_name, ifnull(T2.er_land_area, 0) sum_weight from ( select dist_dis_id, dist_name from district_trans where dist_lang_id = 1 order by dist_name) T1 left join (select er_dis_id, sum(er_land_area) er_land_area from `erf` where er_year = ".date("Y")." group by er_dis_id)T2 on T1.dist_dis_id = T2.er_dis_id";
                $query2 = "select T0.sect_name, T0.sect_sec_id, ifnull(T1.er_land_area,0) sum_weight from
                  (SELECT sect_name, sect_sec_id FROM `section_trans` WHERE sect_lang_id = 1)T0
                  left join
                  (SELECT er_sec_id, sum(er_land_area) er_land_area FROM `erf` WHERE er_year =".date("Y")." group by er_sec_id)T1
                  on
                  T0.sect_sec_id = T1.er_sec_id";
                break;
            case 3:
                $query1 = "select T1.dist_dis_id dis_id, T1.dist_name, ifnull(T2.pl_weight, 0) sum_weight from ( select dist_dis_id, dist_name from district_trans where dist_lang_id = 1 order by dist_name) T1 left join (select pl_dis_id, sum(pl_weight) pl_weight from `plan` where pl_year = ".date("Y")." group by pl_dis_id)T2 on T1.dist_dis_id = T2.pl_dis_id";
                $query2 = "select T0.sect_name, T0.sect_sec_id, ifnull(T1.pl_weight,0) sum_weight from
                  (SELECT sect_name, sect_sec_id FROM `section_trans` WHERE sect_lang_id = 1)T0
                  left join
                  (SELECT pl_sec_id, sum(pl_weight) pl_weight FROM `plan` WHERE pl_year =".date("Y")." group by pl_sec_id)T1
                  on
                  T0.sect_sec_id = T1.pl_sec_id";
                break;
        }

        $arrayByDistrict = Yii::$app->getDb()->createCommand($query1)->queryAll();
        $arrayBySection = Yii::$app->getDb()->createCommand($query2)->queryAll();
        return $this->render('index', [
            'arrayByDistrict' => $arrayByDistrict,
            'arrayBySection' => $arrayBySection,
        ]);
    }

这里是我调用$_get['pro_id']

的视图文件
switch ($_GET['pro_id']) {
  case 2: $text = "Экилган";break;
  case 3: $text = "Кутилаётган";break;
  default: $text = "Сақланаётган";break;
}

1 个答案:

答案 0 :(得分:0)

可能是您应该在访问该值之前检查是否设置了$ _GET(' pro_id')。在调用控制器/操作之前,您是否还没有正确分配值

 if (isset(($_GET['pro_id'])) {
   if(!($_GET['pro_id']> 0)) $_GET['pro_id'] = 1;

  .....


} else {

 // $_GET(['pro_id'] is not set 
}

另一种选择是使用post发送值,所以尝试

 if (isset(($_POST['pro_id'])) {
   if(!($_POST['pro_id']> 0)) $_POST['pro_id'] = 1;
  ......