【问题标题】:Codeigniter data not match error pop up messageCodeigniter 数据不匹配错误弹出消息
【发布时间】:2017-11-23 03:05:51
【问题描述】:

我试图做一个错误弹出消息,但在我从谷歌做了很多研究之后,所有的教程都对我不起作用。在这个项目中,我使用“rn”搜索并发布“RN、姓名、出生日期、年龄、性别、种族、宗教”。现在我想为“rn”设置验证。例如:如果 'rn' 与数据库中的数据不匹配,则会出现错误消息。 (抱歉我的英语不好,因为英语不是我的母语)。
这是控制器:

<?php

class Patient extends CI_Controller {

    public function __construct()
    {
        parent::__construct();

        $this->load->helper('form');

        $this->load->model('Patient_model');
    }

    public function index()
    {
        $data['content'] = 'patient/search_form';
        $data['title']="SEARCH";
        $data['sub']='SEARCH PATIENT';
        $this->load->view("design/index",$data);

    }

    public function execute_search()
    {

        $search_term = $this->input->post('search'); 

        $data['results'] = $this->Patient_model->get_results($search_term);

        $this->load->view('patient/search_results',$data);

    }


}


这是模型:

<?php

class Patient_model extends CI_Model {

    public function get_results($search_term='default')
    {
        // Use the Active Record class for safer queries.
        // $this->load->helper('share_function');
        $this->db->select('lifeline.pesakit.rn,lifeline.pesakit.nama,lifeline.pesakit.tarikhlahir,lifeline.jantina.nama as jantina,lifeline.agama.nama as agama,lifeline.bangsa.nama as bangsa');
        $this->db->from('lifeline.pesakit as pesakit');
        $this->db->join('lifeline.jantina','lifeline.jantina.kod = lifeline.pesakit.jantina');
         $this->db->join('lifeline.agama','lifeline.agama.kod = lifeline.pesakit.agama');
          $this->db->join('lifeline.bangsa','lifeline.bangsa.kod = lifeline.pesakit.bangsa');
        $this->db->where('rn',alphaToNumber($search_term));

        $query = $this->db->get('');

        // Return the results.
        return $query->result_array();
    }



}

这是 search_form.php 的视图:

    <?php $this->load->view('template/header');?>

    <div class="container">
    <div class="panel panel-info">
          <div class="panel-body">
    <?php
        echo form_open('index.php/patient/execute_search');

        echo form_input(array('name'=>'search'));

        echo form_submit('search_submit','Submit');


    ?>
        </div>
        </div>
    </div>
    <?php $this->load->view(

'template/footer');?>

这是 search_results.php 的视图:

<?php $this->load->view('template/header');?>
<div class="container">
<div class="panel panel-info">
      <div class="panel-heading">Patient Demographic</div>
      <div class="panel-body">
        <div class="form-group">
        <?php
            $no=0;
            foreach ($results as $row):
            $no++;
            ?>
            <table align="center" length="200" border='0' cellpadding='5' cellspacing='6' style="font-family:arial;">

            <tr align='left' >
                <th>RN</th> <th> :</th> <th style="font-weight: normal;"><?php echo numberToAlpha($row['rn']);?></th>
                <th>Name</th> <th>:</th> <th style="font-weight: normal;"><?php echo $row['nama'];?></th>
                <th>Date.of.Birth</th> <th>:</th> <th style="font-weight: normal;"><?php echo dateFormat($row['tarikhlahir']);?></th>
                <th></th><th></th><th></th>
            </tr>

            <tr align='left'>
                <th>Age </th><th> :</th> <th style="font-weight: normal;"><?php echo calculateCurrentAge ($row['tarikhlahir']);?></th>
                <th>Gender</th> <th>:</th> <th style="font-weight: normal;"><?php echo $row['jantina'];?></th>
                <th>Race</th> <th>:</th> <th style="font-weight: normal;"><?php echo $row['bangsa'];?></th>
                <th>Religion</th> <th> :</th> <th style="font-weight: normal;"><?php echo $row['agama'];?></th>
            </tr>

            <?php endforeach ?>
            </table>
        </div>
    </div>
</div>
<?php $this->load->view('template/footer');?>


请大家帮助我,因为我对 codeigniter 来说是全新的,我所学到的都是从 mr.google 那里学到的,我周围没有人能帮忙。谢谢你。

【问题讨论】:

  • rn 是您在表格中的列吗?

标签: php codeigniter


【解决方案1】:

基本上,如果没有找到结果,您的 get_results() 函数将返回一个空数组,并且 foreach 循环将“失败”。因此,您必须检查 count($results) &gt; 0 是否意味着您必须检查是否有任何结果:

<?php $this->load->view('template/header');?>
<div class="container">
    <div class="panel panel-info">
        <div class="panel-heading">Patient Demographic</div>
        <div class="panel-body">
            <div class="form-group">
                <?php
                if (count($results) > 0):
                $no = 0;
                foreach ($results as $row):
                $no++;
                ?>
                <table align="center" length="200" border='0' cellpadding='5' cellspacing='6' style="font-family:arial;">

                    <tr align='left'>
                        <th>RN</th>
                        <th> :</th>
                        <th style="font-weight: normal;">
                            <?php echo numberToAlpha($row['rn']);?>
                        </th>
                        <th>Name</th>
                        <th>:</th>
                        <th style="font-weight: normal;">
                            <?php echo $row['nama'];?>
                        </th>
                        <th>Date.of.Birth</th>
                        <th>:</th>
                        <th style="font-weight: normal;">
                            <?php echo dateFormat($row['tarikhlahir']);?>
                        </th>
                        <th></th>
                        <th></th>
                        <th></th>
                    </tr>

                    <tr align='left'>
                        <th>Age </th>
                        <th> :</th>
                        <th style="font-weight: normal;">
                            <?php echo calculateCurrentAge ($row['tarikhlahir']);?>
                        </th>
                        <th>Gender</th>
                        <th>:</th>
                        <th style="font-weight: normal;">
                            <?php echo $row['jantina'];?>
                        </th>
                        <th>Race</th>
                        <th>:</th>
                        <th style="font-weight: normal;">
                            <?php echo $row['bangsa'];?>
                        </th>
                        <th>Religion</th>
                        <th> :</th>
                        <th style="font-weight: normal;">
                            <?php echo $row['agama'];?>
                        </th>
                    </tr>
                </table>
                <?php
                endforeach;
                else:
                ?>
                <p>No results found</p>
                <?php endif; ?>
            </div>
        </div>
    </div>
    <?php $this->load->view('template/footer');?>

注意:我在 foreach 循环中移动了 &lt;/table&gt; 括号,否则您没有终止表格。我不确定您为什么要为每个 patient 创建一个新表,但我该判断谁;)。如果您不打算使用它,也无需启动计数器 ($no)。

【讨论】:

  • 如果我希望“未找到结果”消息成为弹出消息,我该怎么办?
  • 你将不得不使用 AJAX 和 jquery,而且一切对你来说都变得有点复杂(并且超出了你的问题范围)。如果您真的想这样做,我建议您对其进行研究,并且如果您无法弄清楚使用标签ajaxjquery 将其发布在堆栈上。
猜你喜欢
  • 1970-01-01
  • 2018-08-19
  • 1970-01-01
  • 2018-04-27
  • 1970-01-01
  • 1970-01-01
  • 2014-07-30
  • 2020-10-08
  • 1970-01-01
相关资源
最近更新 更多