简体   繁体   中英

Display $query->result();

I'm having some trouble displaying the results of my query using a foreach loop in codeigniter. Heres my controller:

function viewall()
{
    $this->load->model('all');
    $data['query'] = $this->all->viewall();
    $this->load->view('all', $data);
}

Entire Model File:

<?php
class All extends CI_Model
{

function insert_into_db()
{
    $data = array('Error' => $this->input->post('f1'),
                  'Solution' => $this->input->post('f2')
                 );
    $this->db->insert('Errors', $data);
}

function viewall()
{
    $query = $this->db->select("Error, Solution")->from("Errors")->get();
    return $query->result();
}

}

My view (where I think the problem is)

<table class="table table-striped">
    <thead>
        <tr>
            <td><h3>Error</h3></td>
            <td><h3>Solution</h3></td>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($query->result_array() as $entry) ?>
        <tr>
            <td><?php echo $entry->Error; ?></td>
            <td><?php echo $entry->Solution;?></td>
        </tr>
        <?php endforeach ?>
    </tbody>
</table>

Controller:

function viewall()
{
    $this->load->model('all');
    $data['results'] = $this->all->viewall();
    $this->load->view('all', $data);
}

Model:

function viewall()
{
    $query = $this->db->select("Error, Solution")->from("Errors")->get();
    return $query->result();
}

View:

<table class="table table-striped">
    <thead>
        <tr>
            <td><h3>Error</h3></td>
            <td><h3>Solution</h3></td>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($results as $entry): ?>
        <tr>
            <td><?php echo $entry->Error; ?></td>
            <td><?php echo $entry->Solution;?></td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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