Displaying record from database in Codeigniter


models/blog_model.php

<?php
class Blog_model extends CI_Model {

function __construct()
{
parent::__construct();
}
function getListing()
{
$sql = “select * from entries where 1=1”;
$Q = $this->db->query($sql);
return $Q->result();
}
}
?>

============================================================

Controllers/blog.php

<?php
class Blog extends CI_Controller {

function index()
{

$this->load->model(‘Blog_model’);
$data[“listing”]=$this->Blog_model->getListing();
$this->load->vars($data);
$this->load->view(‘blogview’);
}

}
?>

==========================================================

views/blogviews.php

<html>
<head>
<title>Demo</title>
</head>
<body>

<ul>
<?php

foreach ($listing as $row)
{
echo “<li>”.$row->title.” “. $row->content.”</li/>”;
}
?>
</ul>
</body>
</html>

Advertisements

2 thoughts on “Displaying record from database in Codeigniter

  1. Here is My code which worked for me

    Model
    db_1.php
    load->database(‘krishh’);
    $query=$this->db->query(“SELECT * FROM employee”);
    echo “Name Designation”;
    foreach ($query->result_array() as $row)
    {
    echo $row[‘name’].” “.$row[‘designation’].””;
    }
    }
    }
    ?>

    View:
    db_view.php

    Databas_View

    Controller
    db_controller.php

    load->database(‘krishh’);
    $this->load->model(‘Db_1’);
    $data[‘name’]=$this->Db_1->My_Model();
    $this->load->vars($data);
    $this->load->view(‘db_view’);
    }
    }
    ?>

    Note:Database name is ‘krishh’ which is connected via database.php file in the config folder of CI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s