Welcome Guest, Not a member yet? Register   Sign In
Noob Help Please!
#11

[eluser]Pascal Kriete[/eluser]
It's either get() or query(), not both. With active record you would use get(), in this case query() will return the query object directly:
Code:
// Change this
$this->db->query($sql);
$query = $this->db->get();

// To this
$query = $this->db->query($sql);

Sorry if that confused you. I was referring to AR above.
#12

[eluser]Randy Casburn[/eluser]
Change this:

Code:
function display($page){
        // $this->db->select('*')->from('site')->where('page', $page);
        $sql = "SELECT * FROM site WHERE page='$page'";
        // Echo $sql;
        $this->db->query($sql);
        $query = $this->db->get();
        return ($query->num_rows() > 0) ? $query->result() : FALSE;
    }

to

Code:
function display($page){
        // $this->db->select('*')->from('site')->where('page', $page);
        $sql = "SELECT * FROM site WHERE page='$page'";
        // Echo $sql;
        $query  = $this->db->query($sql);
        return ($query->num_rows() > 0) ? $query->result() : FALSE;
    }

$this->db->query runs the query. You don't need to $this->db->get() after it.

Try that.
#13

[eluser]Randy Casburn[/eluser]
Darn you're fast! (EYE see you've not change out your EYE yet ;-) )
#14

[eluser]Pascal Kriete[/eluser]
:lol: , you're not too slow yourself. I'm contemplating making it two eyes and possibly a nose. But then, that would be a little to ordinary for my taste Smile .
#15

[eluser]draconus[/eluser]
Now that i got this to the controller, I need to figure out how to open it up to the view

Here is the controller:
Code:
<?

class Page extends Controller {
    
    function index(){
        $this->load->model('Site');
        $data['query'] = $this->Site->display("index");
        $this->load->view('page', $data);
    }
}
?>

And the View:
Code:
<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
    <p>&lt;?php echo $body;?&gt;</p>
&lt;/body&gt;
&lt;/html&gt;

and here is my current message using the code you just provided:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: body

Filename: views/page.php

Line Number: 6
#16

[eluser]Randy Casburn[/eluser]
Rockin' and rollin' now, but I think we probably led you astray again.

We probably should have had you do a [edit]$query->result_array() since that will return an array of results. Then you can address them directly as a multi-dimensional array within your view if you'd like.

Don't be afraid to check this in your view by doing:

Code:
&lt;?php print_r('query'); ?&gt;

to see what's available for you to shout out on your view!

Randy
#17

[eluser]Randy Casburn[/eluser]
Wow...I'm tired....please look up $query->result_array() not query_array(). Sorry man. It's been a long day for me.

$query->result_array() will give you an array of your result set.

Randy
#18

[eluser]Pascal Kriete[/eluser]
[Edit: Long post fail. Typing takes too long Tongue ]

The data array keys turn into the view variables, so what you have access to right now is the $query variable. That $query variable is the array of row objects that the query returned.

There are a few ways of getting query results.
$query->result() returns an array of rows (I assumed it was more than one since you're grabbing 10).
You could loop through that, leaving everything as it is:
Code:
&lt;?php foreach($query): ?&gt;
&lt;?php echo $whatever; ?&gt;
&lt;?php endforeach; ?&gt;

Or you can use $query->row() to only get a single row. This still returns an object, so in your view you would echo $query->title, $query->body, etc (you can rename query to something more descriptive, of course).
Code:
return ($query->num_rows() > 0) ? $query->row() : FALSE;

If all you have is body and title, you could also get an array instead of an object, so you can pass it straight in to the view:
Code:
// Model
return ($query->num_rows() > 0) ? $query->row_array() : FALSE;

// Controller
// Assign it directly to the data, so the keys are preserved
$data = $this->Site->display("index");

// View
&lt;?php echo $title; ?&gt;
#19

[eluser]draconus[/eluser]
o, damn, take a look at this error:

Fatal error: Call to undefined method CI_DB_mysql_driver::query_array() in /Users/draconus/Sites/draconus/new/application/models/site.php on line 34
#20

[eluser]Randy Casburn[/eluser]
yea - read my corrections....I'm too tired. inparo has you fixed up.

Sorry 'bout that.

Randy




Theme © iAndrew 2016 - Forum software by © MyBB