我在yii2中仍然是新人。
所以,制作一个在yii2中使用MVC的简单项目,其中项目是输出检查结果。
让我烦恼的是,如何在数据库中找不到数据时重定向到主页?
另一个,我得到Undefined offset: 0
我用Google搜索说mismatch array
和data not NULL
。
无论如何,这些是代码:
controller:StudentController.php
public function actionCall()
{
$result = $_POST['semester'];
$result_explode = explode('|', $result);
$sem = $result_explode[0];
$tahun = $result_explode[0]." ".$result_explode[1];
$send = array(
'id' => $_POST['id'],
'semester' => $sem ,
'tahun' => $tahun);
$model = new Student();
if(!$data['result']= $model->getDetails($send))
{
return $this->render('detail', $data);
}
else
{
return $this->render('detail');
}
}
public function actionSearch()
{
return $this->render('searchstudent2');
}
型号:Student.php
public function getDetails($send)
{
$student = student::find()
->select('s.student_name,al.level_matric_no,al.level_semester,al.level_id,s.student_mykad, s.student_address,s.student_postcode,
s.student_state,ss.subject_code,ss.subject_name,ss.subject_credit_hour,c.course_name,st.taken_session,
g.Grade_symbol,g.Grade_value,sr.semester_gpa,sr.semester_cgpa,sr.total_point,
sr.total_credit, sr.semester_count')
->from('student AS s')
->leftJoin('a_level AS al', '`s`.`student_id` = `al`.`student_id`')
->leftJoin('subject_taken AS st', '`al`.`level_id` = `st`.`level_id`')
->leftJoin('semester_result AS sr', '`al`.`level_id` = `sr`.`level_id`')
->leftJoin('grade AS g', '`g`.`grade_id` = `st`.`grade_id`')
->leftJoin('course AS c', '`al`.`level_course_offered` = `c`.`course_id`')
->leftJoin('subject AS ss', '`ss`.`subject_id` = `st`.`subject_id`')
->where(['al.level_id'=>$send['id']])
->andWhere(['sr.semester_count'=>$send['semester']])
->andWhere(['st.taken_session'=>$send['tahun']])
->asArray()
->all();
return $student;
}
查看:searchstudent2.php
<div class="container">
<div class="row">
<h2>Stylish Search Box</h2>
<div id="custom-search-input">
<?php $form = ActiveForm::begin(['action' => Url::to(['student/call']),'options' => ['method' => 'post']]) ?>
<div class="input-group col-md-12">
<select name="semester">
<option value="1|2012/2013">sem1</option>
<option value="2|2013/2014">sem2</option>
<option value="3|2014/2015">sem3</option>
<option value="4|2015/2016">sem4</option>
</select>
<br><br>
<input type="text" name="id" class="search-query form-control" placeholder="Search" />
<span class="input-group-btn">
<button class="btn btn-danger" type="submit">
<span class=" glyphicon glyphicon-search"></span>
</button>
</span>
</div>
<?php ActiveForm::end() ?>
</div>
</div>
detail.php
表1详细说明.php
<table style="width:100%">
<tr>
<th class="tg-yw4l">Nama</th>
<th class="tg-baqh" colspan="7"><?php echo $result[0]['student_name']; ?></th>
</tr>
<tr>
<td class="tg-yw4l">Alamat</td>
<td class="tg-baqh"><?php echo $result[0]['student_address']; echo " ". $result[0]['student_postcode']; echo " ".$result[0]['student_state'];?></td>
<td class="tg-yw4l" rowspan="4"></td>
<td class="tg-yw4l">Kemasukan</td>
<td class="tg-baqh"></td>
<td class="tg-yw4l" rowspan="4"></td>
<td class="tg-yw4l" colspan="2"></td>
</tr>
<tr>
<td class="tg-yw4l">No. KP</td>
<td class="tg-baqh"><?php echo $result[0]['student_mykad']; ?></td>
<td class="tg-yw4l">Sesi</td>
<td class="tg-baqh"><?php echo $result[0]['taken_session']; ?></td>
<td class="tg-yw4l">Tahun Akademik</td>
<td class="tg-yw4l"><?php echo $result[0]['taken_session']; ?></td>
</tr>
<tr>
<td class="tg-yw4l">No. Matrik</td>
<td class="tg-baqh"><?php echo $result[0]['level_matric_no']; ?></td>
<td class="tg-yw4l">Fakulti</td>
<td class="tg-baqh"></td>
<td class="tg-yw4l" colspan="2" rowspan="2"></td>
</tr>
<tr>
<td class="tg-yw4l">Program</td>
<td class="tg-baqh"><?php echo $result[0]['course_name']; ?></td>
<td class="tg-yw4l">Semester</td>
<td class="tg-baqh"></td>
</tr>
<tr>
<td class="tg-yw4l">Pinjaman</td>
<td class="tg-baqh" colspan="7"></td>
</tr>
表2详细说明.php
<tr>
<th class="tg-031e">BIL</th>
<th class="tg-031e">KOD</th>
<th class="tg-031e">SUBJEK</th>
<th class="tg-yw4l">KREDIT</th>
<th class="tg-yw4l">GRED</th>
<th class="tg-yw4l">MATA</th>
<!-- <th class="tg-yw4l">GPA/CGPA</th> -->
</tr>
<?php
$bil=0;
foreach ($result as $details) {
$bil++;
?>
<tr>
<td class="tg-031e"><?=$bil?></td>
<td class="tg-031e"><?php echo $details['subject_code']; ?></td>
<td class="tg-031e"><?php echo $details['subject_name']; ?></td>
<td class="tg-yw4l"><?php echo $details['subject_credit_hour']; ?></td>
<td class="tg-yw4l"><?php echo $details['Grade_symbol']; ?></td>
<td class="tg-yw4l"><?php echo $details['Grade_value']; ?></td>
</tr>
<?php } ?>
<tr>
<td class="tg-031e" colspan="2"></td>
<td class="tg-031e">TOTAL KREDIT</td>
<td class="tg-yw4l"><?php echo $details['total_point']; ?></td>
<td class="tg-yw4l">JUMLAH JAM KREDIT</td>
<td class="tg-yw4l" colspan="3"><?php echo $details['total_credit']; ?></td>
</tr>
答案 0 :(得分:0)
逐个尝试以下步骤
检查$ _POST [&#39; semester&#39;]并确保它不为空
检查$ model-&gt; getDetails($ send)是否返回任何结果。
注意:find() - &gt; ... asArray() - &gt; all()如果找不到结果则返回一个空数组。所以在渲染视图之前检查空结果。
if( !empty($data=$model->getDetails($send)) )
{
return $this->render('detail', ['viewData'=>$data]);
}
else
{
return $this->render('_another_view');
// or redirect to some page or do whatever
}
注意:如果您将空的$ data数组发送到视图,并且您使用$ data [0] [&#39;任何&#39;] 你会得到Undefined offset:0错误。 因此,如果结果为空,则渲染另一个视图。在你的情况下,你正在渲染相同的视图&#39;细节&#39;另一个没有$ data的$ data。 因此很有可能获得Undefined offset:0 error。
由于
答案 1 :(得分:0)
如果 getDetails 函数没有返回数据,请重定向到主页:
if ( ($data = $model->getDetails($send)) != null ) {
return $this->render('detail', ['result' => $data]);
} else {
// redirect no data
return $this->redirect(['site/index']);
}