Welcome Guest, Not a member yet? Register   Sign In
A simple MVC sample, but cannot work well with loading model...
#1

[eluser]mrmu[/eluser]
Hi all,
I'm a CodeIgniter beginner and I got some problems while I tried to re-write the blog example of CodeIgniter tutorial video.
I want to use "load model" to demo a simple MVC structure, but I failed, there is nothing shown on my screen but a full blank.

I'd like to post the codes and hope someone can help me!

First, I create a database called "test" with a table "entries" (includes id, title, body 3 columns) as the blog example and I also add 2 records in it.

I also set this in the config/autoload.php:
Code:
$autoload['libraries'] = array('database');

{Controller} blog.phpSadit can work if I mark B,C without A.)
Code:
<?php
class Blog extends Controller
{
  function __construct()
  {
    parent::Controller();
  }

  function index()
  {
    $data['title'] = "Title";
    $data['heading'] = "My Page";
    // $data['query'] = $this->db->get('entries'); // A
    $this->load->model('blog_model'); // B
    $data['query'] = $this->blog_model->getdata(); // C

    $this->load->view('blog_view', $data);
  }
}
?>

{Model} blog_model.php:
Code:
<?php
class Blog_model extends Model {
    function Blog_model()
    {
        parent::Model();
    }
      
    function getdata()
    {
        $query = $this->db->get('entries');
        return $query;
    }
?>

{View} blog_view.php:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
&lt;html &gt;
&lt;head&gt;
  &lt;meta http-equiv="content-type" content="text/html; charset=utf-8" /&gt;
  &lt;title&gt;&lt;?php echo $title ?&gt;&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
<h1>&lt;?php echo $heading ?&gt;</h1>

&lt;?php foreach($query->result() as $row): ?&gt;

    <h3>&lt;?php echo $row->title ?&gt;</h3>
    <p>&lt;?php echo $row->body ?&gt;</p>
    <hr>

&lt;?php endforeach; ?&gt;

&lt;/body&gt;
&lt;/html&gt;

Thank you!!
#2

[eluser]imn.codeartist[/eluser]
Instead of
Code:
$this->load->model('blog_model');
$data['query'] = $this->blog_model->getdata(); // C
Try using
Code:
$this->load->model('Blog_model');
$data['query'] = $this->Blog_model->getdata(); // C
#3

[eluser]mrmu[/eluser]
Hi dixcoder,
Thank you for reply!
But it still cannot work! :-(
#4

[eluser]imn.codeartist[/eluser]
try

Code:
&lt;?
echo '<pre>';
print_r($query);
exit;

?&gt;

before you loop the query and post the result here
#5

[eluser]mrmu[/eluser]
Hi dixcoder,
I add the code as below, but still nothing shown on, the html resource is empty! (firefox 3.5.3)

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
&lt;html &gt;
&lt;head&gt;
  &lt;meta http-equiv="content-type" content="text/html; charset=utf-8" /&gt;
  &lt;title&gt;&lt;?php echo $title ?&gt;&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
<h1>&lt;?php echo $heading ?&gt;</h1>

&lt;?
//////////////////// add here
echo '<pre>';
print_r($query);
exit;
////////////////////
?&gt;

&lt;?php foreach($query->result() as $row): ?&gt;

    <h3>&lt;?php echo $row->title ?&gt;</h3>
    <p>&lt;?php echo $row->body ?&gt;</p>
    <hr>

&lt;?php endforeach; ?&gt;

&lt;/body&gt;
&lt;/html&gt;
#6

[eluser]imn.codeartist[/eluser]
Code:
class Blog_model extends Model {
    function Blog_model()
    {
        parent::Model();
    }
      
    function getdata()
    {
        $query = $this->db->get('entries');
        return $query;
    }

print what is returning by getdata function
Code:
function getdata()
    {
        $query = $this->db->get('entries');

        echo $this->db->last_query();
        exit;
        return $query;
    }
#7

[eluser]m4d3 Gun[/eluser]
try this....

in ur model....

Quote:
Code:
&lt;?php
class Blog_model extends Model {
    function Blog_model()
    {
        parent::Model();
    }
      
    function getdata()
    {
        $data = array();
        $query = $this->db->get('entries');
        if($query->num_rows()>0){
          foreach($query->result() as $row){
            $data[] = $row;
          }
        }
        $query->free_result();
        return $data;
    }
?&gt;


and view....


Quote:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
&lt;html &gt;
&lt;head&gt;
  &lt;meta http-equiv="content-type" content="text/html; charset=utf-8" /&gt;
  &lt;title&gt;&lt;?php echo $title ?&gt;&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
<h1>&lt;?php echo $heading ?&gt;</h1>
&lt;?php foreach($query as $row): ?&gt;
    <h3>&lt;?php echo $row->title ?&gt;</h3>
    <p>&lt;?php echo $row->body ?&gt;</p>
    <hr>

&lt;?php endforeach; ?&gt;

&lt;/body&gt;
&lt;/html&gt;
#8

[eluser]mrmu[/eluser]
Hi dixcoder,
Still cannot work...nothing happend!


Hi m4d3,
Thanks for reply, but still nothing happend too.
Are they works on your mechine? :-(
#9

[eluser]imn.codeartist[/eluser]
paste your mysql table script here
and database info from config
#10

[eluser]mrmu[/eluser]
Here is SQL script:
Code:
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- database: `test`
--

-- --------------------------------------------------------

--
-- table: `entries`
--

CREATE TABLE IF NOT EXISTS `entries` (
  `id` int(16) NOT NULL,
  `title` varchar(64) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `body` varchar(128) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- data: `entries`
--

INSERT INTO `entries` (`id`, `title`, `body`) VALUES
(1, 'KHL', 'HiHi!~'),
(2, 'KKL', 'HIHIHI~');

database settings:
Code:
$active_group = "default";
$active_record = TRUE;

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "root123";
$db['default']['database'] = "test";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";

Thank you!




Theme © iAndrew 2016 - Forum software by © MyBB