Welcome Guest, Not a member yet? Register   Sign In
Need Basic MVC Example
#1

[eluser]cupidleomanoj[/eluser]
hi, am new to codeigniter as well as to MVC architecture, can any one help me to how the values are to be fetched from database and dispalyed, please i cant get it done. please give me a basic example. thanks in adavnce
#2

[eluser]oppenheimer[/eluser]
I think the <a href="http://codeigniter.com/tutorials/">video tutorials<a> are very good at this.
#3

[eluser]cupidleomanoj[/eluser]
thanks for your reply, I have tried something with CI, but i cant get the output. can you please help me. the following are the codes.

model:- (file name : Helloworld_model.php)

&lt;?php
class Helloworld_model extends CI_Model {
public function __construct() {
parent::__construct();
}
function getData() {
$query=$this->db->get('data');
if($query->num_rows() > 0) {
}
else {
return $query->result();
}
}
}
?&gt;

controller:- (filename : helloworld.php)

&lt;?php
class Helloworld extends CI_Controller {
public function __construct() {
parent::__construct();
// Your own constructor code
}
function index() {
$this->load->model('helloworld_model');
$data['result'] = $this->helloworld_model->getData();
$data['page_title'] = "CI";
$this->load->view('helloworld_view',$data);
}
}
?&gt;


view:- (filename : helloworld_view.php)

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;?=$page_title?&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?php foreach($result as $row):?&gt;
<h3>&lt;?=$row->title?&gt;</h3>
<p>&lt;?=$row->text?&gt;</p>
<br />
&lt;?php endforeach;?&gt;
&lt;/body&gt;
&lt;/html&gt;


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

I get error as


A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/helloworld_view.php

Line Number: 6

-------------------------------------------------
#4

[eluser]oppenheimer[/eluser]
Change your model with this return

Code:
return $query->result_array();
That should address the error.

However, I wasn't familiar with this syntax in your view:

Code:
&lt;title&gt;&lt;?=$page_title?&gt;&lt;/title&gt;

I usually use:

Code:
&lt;title&gt;&lt;? echo $page_title; ?&gt;&lt;/title&gt;
#5

[eluser]John_Betong_002[/eluser]
Check your model...

Code:
class Helloworld_model extends CI_Model
{

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

function getData()
{
  $query=$this->db->get(‘data’);
  if($query->num_rows() > 0)
  {
     // no value returned ???
  }else{
    return $query->result(); // ZERO rows returned
  }
}//endfunc

}//endclass
&nbsp;
&nbsp;
#6

[eluser]cupidleomanoj[/eluser]
hi friends thanks for your solutions i got my desired output. thank you...
#7

[eluser]cupidleomanoj[/eluser]
hi oppenheimer,
* your told that your are not familiar with the syntax &lt;title&gt;&lt;?=$page_title?&gt;&lt;/title&gt;, there are "short_open_tag" in PHP, it is very common as of the &lt;title&gt;&lt;?php echo $page_title; ?&gt;&lt;/title&gt;.
* To use this shortcut syntax you need to enable the short_open_tag in the phpinfo();

follow the example:

&lt;?php
$message1 = "<p>This is my primary message.</p>\n";
?&gt;
&lt;?php echo $message1; ?&gt; //normal syntax
&lt;?=$message1?&gt; //shortcut syntax




Theme © iAndrew 2016 - Forum software by © MyBB