0

我正在开发一个显示数据库表中数据的小型应用程序。我收到 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; ?>

控制器

  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);


    }

模型

 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
4

5 回答 5

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

应该

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

然后你可以访问$querys你的视图。

于 2013-03-13T07:57:56.663 回答
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活动记录类来获取特定表中所有记录的计数,仅使用$this->db->count_all('table_name');

于 2013-03-13T08:17:07.443 回答
0

也许稍微改变你的搜索代码

$q = $this->db->select('ID, name, surname, phone, email')
              ->from('tblinfo')
              ->limit($limit, $offset);
于 2013-03-13T08:15:21.450 回答
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);
}
于 2013-03-13T08:23:43.013 回答
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;
  }
于 2013-03-13T08:34:37.697 回答