简体   繁体   English

表未从Codeigniter中的数据库中获取数据。

[英]Table not fetching data from database in codeigniter..?

Model 模型

function allDocs()
    {
        $query = $this->db->select('*')->from('docs')->get();
        return $query->result();
    }

Controller 控制者

public function viewauction()
    {
        $this->load->model('Crud_model');
        $data['query'] = $this->Crud_model->allDocs();   
        $this->load->view('index', $data);
    }

View 视图

<tbody>

                  <?php foreach($query as $row): ?>
                  <tr>   
                      <td><?php echo $row->doc_id; ?></td>
                      <td><?php echo $row->RefNo; ?></td>
                      <td><?php echo $row->MyNo; ?></td>
                      <td><?php echo $row->RegNo; ?></td>
                      <td><?php echo $row->LetterDate; ?></td>
                      <td><?php echo $row->LetterReceivedFrom; ?></td>
                      <td><?php echo $row->Subject; ?></td>
                      <td><?php echo $row->Title; ?></td>
                      <td><?php echo $row->Officer_InCharge; ?></td>
                  </tr>
                  <?php endforeach; ?>
</tbody>

I tried this..but I dont get any data for "query". 我试过了..但是我没有得到“查询”的任何数据。

Error Shows A PHP Error was encountered Severity: Notice 错误显示遇到PHP错误严重性:注意

Message: Undefined variable: query 消息:未定义的变量:查询

Filename: views/index.php 文件名:views / index.php

Line Number: 277 行号:277

Backtrace: 回溯:

File: C:\\xampp\\htdocs\\1\\application\\views\\index.php Line: 277 Function: _error_handler 文件:C:\\ xampp \\ htdocs \\ 1 \\ application \\ views \\ index.php行:277功能:_error_handler

I did initialize controller in index too.. please help me. 我也没有在索引中初始化控制器..请帮助我。

Why are you using data['query'] you have only one value, try this 您为什么使用data ['query']只有一个值,请尝试

public function viewauction()
    {
        $this->load->model('Crud_model');
        $query' = $this->Crud_model->allDocs();   
        $this->load->view('index', $query);
    } 

I think it helps you 我认为这对您有帮助

You can try This code hope it will help you. 您可以尝试以下代码,希望对您有所帮助。

Change In Model: 型号变更:

function allDocs()
{
    $query = $this->db->select('*')->from('docs')->get();
    return $query->result();
}

to

function allDocs()
{
    $query = $this->db->select('doc_id, RefNo, MyNo, RegNo, LetterDate, LetterReceivedFrom, Subject, Title, Officer_InCharge')->from('docs')->get();
    $result = $query->result();        
    return $result;
}

I have a feeling this is because $data has not been defined before you used it. 我感觉这是因为在使用$data之前尚未定义它。 Try this:- 尝试这个:-

public function viewauction()
{
    $this->load->model('Crud_model');
    $data = [
        'query' => $this->Crud_model->allDocs(),
    ];
    $this->load->view('index', $data);
}

You should load the controller whenever the specific view is loaded. 只要加载了特定的视图,就应该加载控制器。

public function dashboard()
{
        $this->load->model('Crud_model');
        $data = $this->Crud_model->allDocs();
        $this->load->view('index',['data'=> $data]); 
}

Here, Dashboard is the controller function in which I load the page in which I have to pass the data from database. 在这里,仪表板是控制器功能,我在其中加载必须从数据库传递数据的页面。 I have written codes to fetch data through model in viewauction() method here. 我在这里编写了代码,以通过viewauction()方法中的模型获取数据。 Thereby I failed to load this controller whenever I load the view page. 因此,无论何时加载视图页面,我都无法加载该控制器。

My code for fetching is all correct, and only thing I missed was that I didnt load in correct View Page. 我的提取代码都是正确的,唯一错过的是我没有加载正确的“查看页面”。

Moreover I changed this line as well 而且我也改变了这条线

$this->load->view('index',['data'=> $data]);

Thank You Guys for all your suggestions. 谢谢你们的所有建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM