代码noob中的错误无法修复它们

时间:2013-03-13 07:37:17

标签: codeigniter variables foreach php

我正在开发一个显示数据库表中数据的小应用程序。我得到2个错误,不知道为什么会有,我无法弄清楚,因为我仍然是一个菜鸟。我猜它有点蠢,请帮忙。

错误

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: querys


Severity: Warning

Message: Invalid argument supplied for foreach()

这些错误位于我的视图页面中。

查看

    <?php foreach ($queries as $row): ?>
    <table>

       <th>Name</th>
       <th>Surname</th>
       <th>phone</th>
       <th>email</th>

       <td><?php echo $row->name; ?></td>
       <td><?php echo $row->surname; ?></td>
       <td><?php echo $row->phone; ?></td>
       <td><?php echo $row->email; ?></td>

   </table>  
    <?php endforeach; ?>

CONTROLLER

  function display($offset = 0)
    {
           $limit = 20;
            $this->load->model('info_model');

            $results = $this->info_model->search($limit, $offset);

            $data['queries'] = $results['rows'];
            $data['num_results'] = $results['num_rows'];


            $this->load->view('info_view',$data);


    }

MODEL

 function search ($limit, $offset){

          //results query 
          $q = $this->db->select('ID, name, surname, phone, email');
          $this->db->from('tblinfo');
          $this->db->limit($limit, $offset);

          $ret['rows'] = $q->get()->result();
          //count query
          $q = $this->db->select('COUNT(*) as count', FALSE )
          ->from('tblinfo');

          $tmp = $q->get()->result();

          $ret['num_rows'] = $tmp[0]->count;
          return $ret;
      }

修改

我通过插入

修复了foreach错误
   <?php if(is_array($queries)): ?>
   <?php endif; ?>

我得到的唯一错误是

   A PHP Error was encountered

    Severity: Notice

    Message: Undefined variable: queries

5 个答案:

答案 0 :(得分:5)

$data['query'] = $results['rows'];

应该是

$data['querys'] = $results['rows']; // although correct spelling is "queries"

然后,您可以在视图中访问$querys

答案 1 :(得分:1)

问题似乎出现在您的模型search()方法中,这可能不会返回任何结果。我会按如下方式重写它以利用codeigniters活动记录类:

function search ($limit, $offset){

    //results query 
    $this->db->select('ID, name, surname, phone, email');
    $q =  $this->db->get('tblinfo', $limit, $offset);

    $ret['rows'] = $q->result();

    //count query
    $ret['num_rows'] = $this->db->count_all('tblinfo');

    return $ret;
}

您可以使用Codeigniters active record class仅使用$this->db->count_all('table_name');

来计算特定表格中的所有记录

答案 2 :(得分:0)

可能会稍微更改您的搜索代码

$q = $this->db->select('ID, name, surname, phone, email')
              ->from('tblinfo')
              ->limit($limit, $offset);

答案 3 :(得分:0)

我个人认为一个函数应该只执行一个任务。现在,您正在尝试在一个函数中执行两个任务(返回记录并返回记录计数)。

我会像这样重写我的模型:

function search($limit, $offset)
{
  $this->db->select('ID, name, surname, phone, email');
  $this->db->from('tblinfo');
  $this->db->limit($limit, $offset);
  return $this->db->get();
}

你的控制器看起来像这样:

function display($offset = 0)
{
  $this->load->model('info_model');
  $results = $this->info_model->search(20, $offset);

  $data['queries'] = $results->result();
  $data['num_results'] = $this->db->count_all('tblinfo'); // Alternatively you could make a separate model function that does this

  $this->load->view('info_view', $data);
}

答案 4 :(得分:0)

将您的型号代码更改为此

function search ($limit, $offset){

      //results query 
      $this->db->select('ID, name, surname, phone, email');
      $this->db->from('tblinfo');
      $this->db->limit($limit, $offset);
      $q = $this->db->get();
      $ret['rows'] = $q->result();
      //count query
      $q1 = $this->db->select('COUNT(*) as count', FALSE )
      ->from('tblinfo');

      $tmp = $q1->get()->result();

      $ret['num_rows'] = $tmp[0]->count;
      return $ret;
  }