Hey im开发者_Go百科 using codeigniter and i got this error
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: query
Filename: views/nyheder_view.php
Line Number: 2 A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/nyheder_view.php
Line Number: 2
my controller :
<?php
class Nyheder extends Controller {
function index()
{
$data['content'] = 'nyheder_view';
$this->load->view('includes/template', $data);
}
function vis()
{
//parent::controller();
$this->load->model('nyheder_model');
$data['query'] = $this->nyheder_model->load_nyheder();
$this->load->view('nyheder_view', $data);
}
}
?>
my view
<?php foreach($query as $row) : ?>
<h1><?php echo $row->overskrift; ?></h1>
<p><?php echo $row->indhold; ?></p>
<?php endforeach;?>
I don't see anything immediately wrong here. Could you put this line in just before you load your nyheader_view:
die(var_export($data));
And let us know what you see.
I would guess that $this->nyheder_model->load_nyheder() returns false or null.
try var_dump($this->nyheder_model->load_nyheder()) before loading your view.
Problem with the sql query in your nyheder model maybe?
I would agree that problem is probably somewhere in your model...or maybe it's something even more obvious like the database option is not activated...Try:
function __construct(){
parent::Controller();
$this->load->database();
}
ok going to guess that load_nyheder() is either returning an object or an array so check the plumbing first by hardcoding and array something a bit like this:
$data['query'] = array(
'keystr1' => 'valuestr1',
'keystr2' => 'valuestr2',
'keystr3' => 'valuestr3'
);
$this->load->view('nyheder_view', $data);
NB: If you put an object as $data it will be converted to an array.
Then in your view "nyheder_view" do something more like this:
<?php foreach($query as $someKey=>$someValue):?>
<h1><?php echo $someKey; ?></h1>
<p><?php echo $someValue; ?></p>
<?php endforeach;?>
Get it all working with hardcoded test (fixture) data first, then try feeding $data['query'] with your your load_nyheder() and see if you get similar problems. That way you know where the problem is.
Try this, I think you're missing the result() after $query...
<?php foreach($query->result() as $row) : ?>
<h1><?php echo $row->overskrift; ?></h1>
<p><?php echo $row->indhold; ?></p>
<?php endforeach;?>
精彩评论