Welcome Guest, Not a member yet? Register   Sign In
Having little trouble getting data from database
#1

[eluser]faxtion[/eluser]
Got the following code but does not display anything in view, neither does it give me and error, any help appreciated

Code:
Controller

class Parts extends Controller {

    function index() {
        echo 'Hello World!';
    }
    
    function view() {
        $this->load->model('Parts_model');
        $data['parts'] = $this->Parts_model->get_parts();
        $this->load->view('parts_view', $data);
    }
}
?>

Code:
Model

<?php
class Parts_model extends Model {
    
    function Parts_model()
    {
        // Call the Model constructor
        parent::Model();
    }

    function get_parts() {
        //$query = $this->db->query('SELECT title FROM parts');
        $query = $this->db->get('parts', 10);
        return $query->result();
    }

}
?>


Code:
View


<html>
<head>
<title>Rally Parts Auction</title>
</head>
<body>
<h3>Parts</h3>  

<ul>
&lt;?php foreach($parts as $part):?&gt;

<h4>&lt;?=$part['postid']?&gt;</h4>
<p>&lt;?=$part['title']?&gt;</p>
<p>&lt;?=$part['description']?&gt;</p>
<p>&lt;?=$part['category']?&gt;</p>
<p>&lt;?=$part['image']?&gt;</p>
<p>&lt;?=$part['image1']?&gt;</p>
<p>&lt;?=$part['image2']?&gt;</p>

&lt;?php endforeach;?&gt;
</ul>

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

[eluser]OES[/eluser]
It does not look like your are passing back an array.

Try this.

Code:
&lt;?php
class Parts_model extends Model {
    
    function Parts_model()
    {
        // Call the Model constructor
        parent::Model();
    }

    function get_parts() {
        //$query = $this->db->query('SELECT title FROM parts');
        $query = $this->db->get('parts', 10);
        return $query->result_array();
    }

}
?&gt;

Regards
#3

[eluser]Randy Casburn[/eluser]
Hi,

You've passed you data to your view as:

Code:
$data['parts'] = $this->Parts_model->get_parts();

and you are referencing your data in your view as:

Code:
<h4>&lt;?=$part['postid']?&gt;</h4>


part vs. parts


I hate it when I do that!

Hope that helps,

Randy
#4

[eluser]faxtion[/eluser]
Tried both solutions but still not displaying any data but I get three lots of html tags where my data should be,
#5

[eluser]Randy Casburn[/eluser]
Yea - I just noticed I was wrong...I looked right past this...

Code:
&lt;?php foreach($parts as $part):?&gt;

Sorry about that. I hope I didn't break you worse.

Please ignore my previous post.




Theme © iAndrew 2016 - Forum software by © MyBB