CodeIgniter Forums
Data from database not showing up - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: Data from database not showing up (/thread-23865.html)



Data from database not showing up - El Forum - 10-23-2009

[eluser]GamingFusion[/eluser]
I am trying to get data to display from a database onto an page for my removeShow page but it wont.

After digging a bit i found that what ever code is below my getShows()function does not want to display. Whats wrong with it.

Heres the model function getShows()
Code:
//getShows Function
function getShows()
{
    //Get Shows Data
    $this->db->get('shows');    
}

and heres my view page

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
&lt;title&gt;&lt;?=$title?&gt;&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
<h1>&lt;?=$heading?&gt;</h1>
&lt;?=$subheading?&gt;

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

<h2><b>&lt;?=$row->title?&gt;</b></h2>
<small>Directed by: &lt;?=$row->director?&gt;</small>
<hr />
<h3><b>Show Times</b></h3>
<p>&lt;?=$this->typography->nl2br_except_pre($row->showtimes)?&gt;</p>
<h3><b>Cast</b></h3>
<p>&lt;?=$this->typography->nl2br_except_pre($row->cast)?&gt;</p>
        
&lt;?php endforeach; ?&gt;
&lt;/body&gt;
&lt;/html&gt;

and heres my controller

Code:
//removeShow Function
function removeShow()
{
    $data['title'] = 'Theater 311 | Remove a Show';
    $data['heading'] = 'Remove a Show';
    $data['subheading'] = 'Pick a Show to Remove';
    $data['query'] = $this->database->getShows();
    
    $this->load->view('removeShow', $data);
}



Data from database not showing up - El Forum - 10-23-2009

[eluser]stratton[/eluser]
GamingFusion,

Try the modification below to your Model's method:

Code:
//getShows Function
function getShows()
{
    //Get Shows Data
    $query = $this->db->get('shows');
    return $query->result();    
}

You also didn't post your entire controller so I'm going to assume that your Controller loads the model 'database' in the constructor? If not, make sure you include the following in your constructor; with the correct name of your model of course.

Code:
$this->load->model('Model_name');

Hope this helps!


Data from database not showing up - El Forum - 10-23-2009

[eluser]khagendra[/eluser]
Here is your model code
Code:
//getShows Function
function getShows()
{
    //Get Shows Data
    $this->db->get('shows');    
}

Your function getShows just retrieving the data from database but returning no result. So you have to add one more line like this
Code:
//getShows Function
function getShows()
{
    //Get Shows Data
    $query = $this->db->get('shows');
    return $query->result();
}

-- You also have to load the model in ur controller. if the class name of model is Model_name then u can load like this
Code:
class ControllerName extends Controller
{
    function ControllerName()
    {
       parent::Controller();
       $this->load->model('Model_name');
    }
}

to fetch the data, within any function of that controller u can write like this
Code:
$this->Model_name->getShows();

The data will be return as array of objects. so u can write like this
Code:
$data['show_info'] = $this->Model_name->getShows();

Finally, the fields name can be fetched as in ur view like this
Code:
$show_info->field_name;

Note: use the same name of loaded model while fetching the model function. Here is example
Code:
$this->load->model('Model_name');
    then use like this
    $this->Model_name->getShows();
    $this->model_name->getShows() // this will be wrong
  
    OR
    $this->load->model('model_name');
    $this->model_name->getShows();
    $this->Model_name->getShows() // this will be wrong
Hope this will help you solving your problem.


Data from database not showing up - El Forum - 10-24-2009

[eluser]GamingFusion[/eluser]
worked thanks man.