未定义的变量:query(foreach)

时间:2016-06-22 07:14:50

标签: php codeigniter

我收到以下错误消息

 A PHP Error was encountered
 Severity: Notice 
 Message: Undefined variable: query 
 Filename: views/home.php 
 Line Number: 121

回溯:

File: C:\wamp\www\ci\application\views\home.php
Line: 121
Function: _error_handler

File: C:\wamp\www\ci\application\controllers\Home.php
Line: 20
Function: view

File: C:\wamp\www\ci\index.php
Line: 292
Function: require_once

请帮忙!

class Home extends CI_Controller
{
function __construct() {
    parent::__construct();
    $this->load->database();
    $this->load->model("login_model", "getSearch");

    if(empty($this->session->userdata('c.contact_id'))) {
        $this->session->set_flashdata('flash_data', '<div class="alert alert-warning text-center">Ooops. You don\'t have access!</div>');
        redirect('login');
    }
}

public function index() {
    $this->load->view('home');
}

public function logout() {
    $data = ['c.contact_id', 'email'];
    $this->session->unset_userdata($data);

    redirect('login');
}

function searches(){
    $contact_id = $this->session->userdata('c.contact_id');
    $data['c.contact_id'] = $contact_id;
    $data['query'] = $this->login_model->getSearch($contact_id);
    $this->load->view('home', $data);
}
}

类Login_model扩展了CI_Model {

function __construct() {
    parent::__construct();
    $this->load->database();
}

public function validate_user($data) {
    $this->db->distinct();
    $this->db->select('c.contact_id, name, email, password, level, c.statusid');
    $this->db->from('contact c');
    $this->db->join('deliveryreg d', 'c.contact_id = d.idcontact');
    $this->db->join('ClientLogin cl', 'cl.idcontact = c.contact_id');
    $this->db->where('c.statusid = 1');
    $this->db->where('email', $data['email']);
    $this->db->where('password', $data['password']);

    $query = $this->db->get();
    return $query->num_rows();
}

public function getSearch(){
    $this->db->distinct();
    $this->db->select(" s.id AS 'searchid',s.description as 'searchname',so.id AS 'sourceid',so.description as 'service'");
    $this->db->from('contact c');
    $this->db->join('deliveryreg d', 'c.contact_id = d.idcontact');
    $this->db->join('searchsource ss', 'ss.id = d.idsearchsource');
    $this->db->join('search s', 's.id = ss.idsearch');
    $this->db->join('source so', 'so.id = ss.idsource');
    $this->db->where('c.statusid = 1'); 
    $this->db->where('email');
    $ids = array('1','5','8');
    $this->db->where_in('so.id', $ids);
    $this->db->order_by('s.description', 'so.description');

    $query = $this->db->get(); 
    return $query->result();
}

function __destruct() {
    $this->db->close();
}

}

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>PEAR | Professional Evaluation And Research</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <!-- Bootstrap 3.3.5 -->
    <link rel="stylesheet" href="<?php echo base_url();?>assets/css/bootstrap.min.css">
    <!-- custom css -->
    <link rel="stylesheet" href="<?php echo base_url();?>assets/css/custom.css">
    <!-- Bootstrap  vertical tabs -->
    <link rel="stylesheet" href="<?php echo base_url();?>assets/css/bootstrap.vertical-tabs.css">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
    <!-- Ionicons -->
    <link rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
    <!-- Theme style -->
    <link rel="stylesheet" href="<?php echo base_url();?>assets/css/AdminLTE.min.css">
    <!-- AdminLTE Skins. We have chosen the skin-blue for this starter
          page. However, you can choose any other skin. Make sure you
          apply the skin class to the body tag so the changes take effect.
    -->
    <link rel="stylesheet" href="<?php echo base_url();?>assets/css/skins/skin-green.min.css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
        <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>

  <body class="hold-transition skin-green sidebar-mini" >
    <div class="wrapper">

      <!-- Main Header -->
      <header class="main-header">

        <!-- Logo -->
        <a href="#" class="logo">
          <!-- mini logo for sidebar mini 50x50 pixels -->
          <span class="logo-mini"><b>PEAR</b></span>
          <!-- logo for regular state and mobile devices -->
          <span class="logo-lg"><b>PEAR</b></span>
        </a>

        <!-- Header Navbar -->
        <nav class="navbar navbar-static-top" role="navigation">
          <!-- Sidebar toggle button-->
          <a href="#" class="sidebar-toggle" data-toggle="offcanvas" role="button">
            <span class="sr-only">Toggle navigation</span>
          </a>
          <!-- Navbar Right Menu -->
          <div class="navbar-custom-menu">
            <ul class="nav navbar-nav">
              <!-- Messages: style can be found in dropdown.less-->
              <li class="dropdown messages-menu">
                <!-- Menu toggle button -->
                <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                  <span>Advanced Filtering</i></span>
                </a>

              </li>
              <li class="dropdown messages-menu">
                <!-- Menu toggle button -->
                <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                  <span>Exporting</i></span>
                </a>

              </li><!-- /.messages-menu -->

              <!-- Notifications Menu -->
              <li class="dropdown notifications-menu">
                <!-- Menu toggle button -->
                <a href="<?= site_url('home/logout') ?>">
                  <span><i class="fa fa-sign-out" aria-hidden="true"></i>Logout</span>
                </a>
              </li>
              <!-- Tasks Menu -->              
            </ul>
          </div>
        </nav>
      </header>
      <!-- Left side column. contains the logo and sidebar -->
      <aside class="main-sidebar">

        <!-- sidebar: style can be found in sidebar.less -->
        <section class="sidebar">

          <!-- Sidebar user panel (optional) -->
          <div class="user-panel">
            <div class="pull-left image">
              <img src="<?php echo base_url();?>assets/img/user-avatar.png" class="img-circle" alt="User Image">
            </div>
            <div class="pull-left info">
              <p>Hi, there!</p>
              <!-- Status -->
              <a href="#"><i class="fa fa-circle text-success"></i> Online</a>
            </div>
          </div>



          <!-- Sidebar Menu -->
          <ul class="sidebar-menu">
            <li class="header">HEADER</li>
            <!-- Optionally, you can add icons to the links -->
            <li class="treeview">           
              <a href="#"><i class="active fa fa-search"></i> <span>Searches</span> <i class="fa fa-angle-left pull-right"></i></a>
              <?php foreach ($query as $search_row){ ?>
              <ul class="treeview-menu">
                <li><a href="search">--- Select all ---</a></li>
                <li style="padding-left: 10px;"><input type="checkbox"> <span style="color: #fff; "><?php echo $search_row->searchname;?><span></li>
                <li style="padding-left: 10px;"><input type="checkbox"> <span style="color: #fff; ">Search B<span></li>
                <li style="padding-left: 10px;"><input type="checkbox"> <span style="color: #fff; ">Search C<span></li>

              </ul>
              <?php }?>
            </li>
            <!-- <li class="active"><a href="#"><i class="fa fa-link"></i> <span>Searches</span></a>
              <ul class="treeview-menu">
                <li><a href="#">Link in level 2</a></li>
                <li><a href="#">Link in level 2</a></li>
              </ul>
            </li> -->
            <!-- <li><a href="#"><i class="fa fa-link"></i> <span>Another Link</span></a></li> -->
            <li class="treeview">
              <a href="#"><i class="active fa fa-cog"></i> <span>Services</span> <i class="fa fa-angle-left pull-right"></i></a>
              <ul class="treeview-menu">
                <li><a href="#">--- Select all ---</a></li>
                <li style="padding-left: 10px;"><input type="checkbox"> <span style="color: #fff; ">Print<span></li>
                <li style="padding-left: 10px;"><input type="checkbox"> <span style="color: #fff; ">Broadcast<span></li>
                <li style="padding-left: 10px;"><input type="checkbox"> <span style="color: #fff; ">Online<span></li>
              </ul>
            </li>

            <li class="treeview">
              <a href="#"><i class="fa fa-calendar"></i> <span>Calendar</span></a>
            </li>
            <li class="treeview">
              <a href="#"><i class="fa fa-facebook"></i> <span>Facebook</span></a>
            </li>
          </ul>
         <!--  <div class="sed-icons">
            <a href="#"><i class="fa fa-floppy-o " aria-hidden="true" style="padding: 5px;"></i></a>
            <a href="#"><i class="fa fa-pencil-square-o " aria-hidden="true"></i></a>
            <a href="#"><i class="fa fa-trash " aria-hidden="true" style="padding: 5px;"></i></a>
          </div> -->
          <!-- /.sidebar-menu -->
        </section>
        <!-- /.sidebar -->
      </aside>

      <!-- Content Wrapper. Contains page content -->
      <div class="content-wrapper" id="myContent" data-spy="scroll" data-target=".my-navbar" data-offset="50">
        <!-- Content Header (Page header) -->
        <section class="content-header">
          <h1 style="padding: 25px; text-align: center;"><i class="fa fa-tachometer" aria-hidden="true"></i>DASHBOARD</h1>
        </section

        <!-- Main content -->
        <section class="content">
          <!-- Your Page Content Here -->
          <!--Inner navigation-->
          <nav class="col-md-2" id="myScrollspy">
            <ul class="nav my-navbar">
              <li><a href="#tabA" class="active">Overview</a></li>
              <li><a href="#tabB">Keystories</a></li>
              <li><a href="#tabC">Region</a></li>
              <li><a href="#tabD">Media Type</a></li>
              <li><a href="#tabE">Byline</a></li>
              <li><a href="#tabE">Trends</a></li>
            </ul>
          </nav>
          <div class="col-md-10">
            <div class="tab-a" id="tabA">
              <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sequi labore omnis adipisci nisi quibusdam consequuntur officiis repellendus tempora doloremque eum, maxime, vel, reiciendis consectetur consequatur obcaecati, saepe voluptates. Minus, soluta.</p>
            </div>
            <div class="tab-b" id="tabB">
              <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sequi labore omnis adipisci nisi quibusdam consequuntur officiis repellendus tempora doloremque eum, maxime, vel, reiciendis consectetur consequatur obcaecati, saepe voluptates. Minus, soluta.</p>
            </div>
            <div class="tab-c" id="tabC">
              <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sequi labore omnis adipisci nisi quibusdam consequuntur officiis repellendus tempora doloremque eum, maxime, vel, reiciendis consectetur consequatur obcaecati, saepe voluptates. Minus, soluta.</p>
            </div>
            <div class="tab-d" id="tabD">
              <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sequi labore omnis adipisci nisi quibusdam consequuntur officiis repellendus tempora doloremque eum, maxime, vel, reiciendis consectetur consequatur obcaecati, saepe voluptates. Minus, soluta.</p>
            </div>
            <div class="tab-e" id="tabE">
              <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sequi labore omnis adipisci nisi quibusdam consequuntur officiis repellendus tempora doloremque eum, maxime, vel, reiciendis consectetur consequatur obcaecati, saepe voluptates. Minus, soluta.</p>
            </div>
          </div>


          <!--Inner navigation End-->

        </section><!-- /.content -->
      </div><!-- /.content-wrapper -->

      <!-- Main Footer -->
      <footer class="main-footer">      
        <!-- Default to the left -->
        <strong>Copyright &copy; <?php  echo date("Y");?> <a href="http://www.pear.africa.com" target="_blank">PEAR | Professional Evalution and Research</a>.</strong> All rights reserved.
      </footer>


      <!-- Add the sidebar's background. This div must be placed
           immediately after the control sidebar -->
      <!-- <div class="control-sidebar-bg"></div> -->
    </div><!-- ./wrapper -->

    <!-- REQUIRED JS SCRIPTS -->

    <!-- jQuery 2.1.4 -->
    <script src="<?php echo base_url();?>assets/js/jQuery-2.1.4.min.js"></script>
    <!-- Bootstrap 3.3.5 -->
    <script src="<?php echo base_url();?>assets/js/bootstrap.min.js"></script>
    <!-- AdminLTE App -->
    <script src="<?php echo base_url();?>assets/js/app.min.js"></script>
    <!-- Scroll effect -->
    <script src="<?php echo base_url();?>assets/js/scroll.js"></script>
    <!-- Slide effect -->
    <script src="<?php echo base_url();?>assets/js/slideEffect.js"></script>


    <!-- Optionally, you can add Slimscroll and FastClick plugins.
         Both of these plugins are recommended to enhance the
         user experience. Slimscroll is required when using the
         fixed layout. -->
  </body>
</html>

2 个答案:

答案 0 :(得分:0)

此处没有任何内容将值传递给查看页面,例如此$ data [&#39; query&#39;] =&#34;&#34;;

public function index() {
$data['query']=""; // here you made the mistake, if you want pass any value
$this->load->view('home');
}

如果要传递任何值,否则在home.php视图页面中使用$ query变量。删除它或在视图页面中写入查询并执行。

foreach ($query->result() as $search_row) {
//your functions
}

在foreach循环中使用结果函数$ query-&gt; result()

答案 1 :(得分:0)

在你的构造函数中:

$this->load->model("login_model");
/* not: $this->load->model("login_model", "getSearch"); */
/* it just creates an alias for login_model, e.g. $this->getSearch->some_model_method(...) */

由于getSearch是模型中的一种方法,现在可以执行以下操作:

$data['query'] = $this->login_model->getSearch($contact_id);

另请注意,使用index()方法仍会出现未定义的变量错误,因为您没有向$query视图提供home变量。使用当前代码,您只能通过home方法将其提供给searches()视图。我不知道你究竟想要做什么,但一个简单的解决方法是在视图中测试$query的存在:

<?php if($query){ foreach ($query as $search_row){ /* ... */ } } ?>