public function actionIndex($labelid = 1) { //查找所有的标签 $labels = Labels::find()->asArray()->all(); //根据标签ID查找问题 $label = Labels::find()->where(['labelid' => $labelid])->one(); $questions = $label->questions; //提问表 $session = Yii::$app->session; $askform = new AskForm(); //添加问题 if (!empty($session['user'])) { if ($askform->load(Yii::$app->request->post()) && $askform->validate()) { $ask = new Questions(); $ask->labelid = $labelid; $maxid = Questions::find()->where(['labelid' => $labelid])->orderBy('questionid desc')->one(); if (empty($maxid)) { $maxid = 1; } else { $maxid = $maxid['questionid']; } $ask->questionid = $maxid + 1; $ask->host = $session['username']; $ask->title = $askform->title; $ask->question = $askform->question; $ask->save(); return $this->redirect(Yii::$app->request->getUrl()); } } else { echo "未登录"; } return $this->render('index', ['labelid' => $labelid, 'labels' => $labels, 'questions' => $questions, 'askform' => $askform]); }
public function getLabel() { return $this->hasOne(Labels::classname(), ['labelid' => 'labelid']); }
} ?> <br> <?php echo Html::encode('标签:'); foreach ($labels as $label) { ?> <?php echo Html::tag('a', Html::encode($label['name']), ['href' => '?r=forum/index&labelid=' . $label['labelid']]); } ?> <?php echo '<br>'; $l = Labels::find()->where(['labelid' => $labelid])->one(); echo '首页>' . $l->name . '>'; ?> <?php foreach ($questions as $question) { ?> <li> <?php echo Html::tag('a', Html::encode($question['title']), ['href' => '?r=forum/answer&labelid=' . $question['labelid'] . '&questionid=' . $question['questionid']]); ?> </li> <?php } ?>