Table not fetching data from database in codeignit

2019-08-28 02:16发布

问题:

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

Message: Undefined variable: query

Filename: views/index.php

Line Number: 277

Backtrace:

File: C:\xampp\htdocs\1\application\views\index.php Line: 277 Function: _error_handler

I did initialize controller in index too.. please help me.

回答1:

Why are you using data['query'] you have only one value, try this

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

I think it helps you



回答2:

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


回答3:

I have a feeling this is because $data has not been defined before you used it. Try this:-

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


回答4:

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. 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.