Php 重定向到yii2中的错误页面

Php 重定向到yii2中的错误页面,php,html,yii2,Php,Html,Yii2,我在yii2还是新手 因此,制作一个简单的项目,在yii2中使用MVC,该项目将输出检查结果 困扰我的是,在数据库中找不到数据时,如何重定向到主页面 另一个,我得到了未定义的偏移量:0,我在谷歌上搜索到不匹配数组和数据非空 无论如何,以下是这些代码: 控制器:StudentController.php public function actionCall() { $result = $_POST['semester']; $result_explode = e

我在yii2还是新手

因此,制作一个简单的项目,在yii2中使用MVC,该项目将输出检查结果

困扰我的是,在数据库中找不到数据时,如何重定向到主页面

另一个,我得到了
未定义的偏移量:0
,我在谷歌上搜索到
不匹配数组
数据非空

无论如何,以下是这些代码:

控制器: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)
{

视图: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>

时尚搜索框
sem1
sem2
sem3
sem4


detail.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 "&nbsp;". $result[0]['student_postcode']; echo "&nbsp;".$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>
    <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>
表1 in detail.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 "&nbsp;". $result[0]['student_postcode']; echo "&nbsp;".$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>
    <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>

纳米
阿拉马特
凯马苏坎
编号:KP
塞西
塔胡恩·阿卡德米克
不,Matrik
法库蒂
节目
学期
平贾曼

表2 in detail.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 "&nbsp;". $result[0]['student_postcode']; echo "&nbsp;".$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>
    <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>

比尔
柯德
Subsek
克雷迪特
格雷德
马塔
总克雷迪特
朱姆拉果酱

请逐个尝试以下步骤

  • 检查$u POST['sement'],确保它不是空的

  • 检查$model->getDetails($send)是否返回任何结果

  • 注意:如果找不到结果,find()->…asArray()->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]['anything'] 您将得到未定义的偏移量:0错误。 所以,如果结果为空,则呈现另一个视图。在您的情况下,您呈现的是同一个视图的“详细信息”,一个包含$data,另一个不包含$data。 因此,很有可能得到未定义的偏移量:0错误

    谢谢

    如果getDetails函数没有返回数据,请重定向到主页:

    if ( ($data = $model->getDetails($send)) != null ) {
    
      return $this->render('detail', ['result' => $data]);
    
    } else {
       // redirect no data
       return $this->redirect(['site/index']);
    }
    

    您好,欢迎来到SO。您可以添加您的sql表吗?还有,您是否使用了Gii?请告诉我您在哪一行获得了未定义的偏移量:0欢迎@Jørgen。我没有使用Gii,我使用了6个表。如何发布所有表?@RK12 Hi。在views\student\detail.php第17行:像这样更改您的if语句,然后尝试:if(!empty($data['result')&$data)['result']=$model->getDetails($send))谢谢你的建议。我会尝试。现在这个项目暂停,直到我的主管进一步通知。谢谢你的建议。我会尝试。现在这个项目暂停,直到我的主管进一步通知。尝试一下,让我们知道。选择一个不要让问题没有答案。