How to retrieve data from the database and display it in a html table using Codeigniter

前端 未结 3 589
余生分开走
余生分开走 2021-01-16 04:47

anyone please help me to retrieve the db data and how to view it in html table.is the coding i given is correct or not if not can you please say how i have to give. in order

相关标签:
3条回答
  • 2021-01-16 05:10

    Do this in your edit_content function

    $result=$this->editcontent_model->get_contents();
    $this->load->view('edit_content/edit_content', $result);
    

    and in your view, access the content like:

    <?php foreach($result  as $result): ?>
      <tr>
    <tr><?php echo $result[0]->content; ?>
    
     </tr>
     <?php endforeach; ?> 
    

    You have to do this beacuse the $result is an associative array not a simple one. Hope this helped you.

    0 讨论(0)
  • 2021-01-16 05:22

    Put this in the end of your edit_content function in your controller

    $this->load->view('edit_content/edit_content', $result);
    

    Loading of the view should be done in the controller. From what I see, your model have returned the result so the last line is not executed.

    0 讨论(0)
  • 2021-01-16 05:28

    Try this:

    <?php
    #mycontroller
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class edit_content extends CI_Controller    {
    
        public function __construct(){
            parent::__construct();
        }
    
        function edit_content()
        {
            $data   = array();
            $this->load->model('editcontent_model');
            $this->load->helper('url');
            $this->load->library('acl');
            $data['result'] = $this->editcontent_model->get_contents();
            $this->load->view('edit_content/edit_content', $data);
        }
    }
    ?>
     <!-- myview -->
    <table>
    <tr>
    <th>Content</th>
    
    </tr>
    <?php foreach($result  as $r): ?>
    <tr><?php echo $r->content; ?>
    
        </tr>
    <?php endforeach; ?>
    </table>
    <?php
    #mymodel
    class editcontent_model extends CI_Model{
        function get_contents() {
            $this->db->select('content');
            $this->db->from('contents');
            $query = $this->db->get();
            return $result = $query->result();
        }
    }
    
    0 讨论(0)
提交回复
热议问题