1

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 to view it in html table.

Controller

class edit_content extends CI_Controller {

    function edit_content()
    {
        parent::__construct();
        $this->load->model('editcontent_model');
        $this->load->helper('url');
        $this->load->library('acl');
        $this->data = $this->editcontent_model->get_contents();
    }
}

View

<table>
    <tr>
        <th>Content</th>
    </tr>

    <?php foreach($this->data  as $r): ?>
        <tr>
            <tr><?php echo $r['content']; ?>
        </tr>
    <?php endforeach; ?>

<table>

Model

class editcontent_model extends CI_Model {
    var $CI; 

    function editcontent_model(){
        parent::__construct();
    }

    function get_contents() {
        $this->db->select('content');
        $this->db->from('contents');
        $query = $this->db->get();
        return $result = $query->result();
        $this->load->view('edit_content/edit_content', $result);
    }
}
3
  • Your main problem is that you are trying to pass data to and load the view from your model. Instead you want to do this in your controller. Commented Aug 1, 2013 at 7:55
  • What version of Codeigniter are you using? If it is the latest version you may need to re-write your classes constructors. Commented Aug 1, 2013 at 7:57
  • See complete solution: c-sharpcorner.com/UploadFile/225740/… Commented Jul 5, 2017 at 10:00

3 Answers 3

5

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();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

$data['name'] = $this->acl->getUsername($this->session->userdata['user_id']); $content['result'] = $this->editcontent_model->get_contents(); $this->load->view('edit_content/edit_content', $data, $content);am having code as above it is viewing only $data and if i gave $content first then only $content is viewing how to view both
0

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.

1 Comment

$data['name'] = $this->acl->getUsername($this->session->userdata['user_id']); $content['result'] = $this->editcontent_model->get_contents(); $this->load->view('edit_content/edit_content', $data, $content);am having code as above it is viewing only $data and if i gave $content first then only $content is viewing how to view both

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.