任何人都可以帮我弄清楚这个原因我不知道问题出在哪里。 我已经尝试了很多次,甚至搜索很多教程仍然无法正常工作。非常感谢帮助我解决我的问题。
这是我的控制器保存为Home.php
<?php
class Home extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('HomeModel');
// Your own constructor code
}
}
function index(){
$query = $this->HomeModel->getEmployees();
$data['EMPLOYEES'] = null;
if($query){
$data['EMPLOYEES'] = $query;
}
$this->load->view('index.php', $data);
}
?>
这个模型保存为HomeModel.php
<?php
class HomeModel extends Model {
function HomeModel(){
parent::Model();
}
function getEmployees(){
$this->db->select("jantina,bangsa,agama");
$this->db->from('pesakit');
$query = $this->db->get();
return $query->result();
}
}
?>
这是保存为index.php的视图
<?php
<!DOCTYPE html>
<html lang="en">
<head>
<title>Display Records From Database Using Codeigniter</title>
<link href="<?= base_url();?>css/bootstrap.css" rel="stylesheet">
</head>
<body>
<div class="row">
<div style="width:500px;margin:50px;">
<h4>Display Records From Database Using Codeigniter</h4>
<table class="table table-striped table-bordered">
<tr><td><strong>Jantina</strong></td><td><strong>Bangsa</strong></td><td><strong>Agama</strong></td></tr>
<?php foreach($EMPLOYEES as $employee){?>
<tr><td><?=$employee->jantina;?></td><td><?=$employee->bangsa;?></td><td><?=$employee->agam;?></td></tr>
<?php }?>
</table>
</div>
</div>
</body>
</html>
的config.php
$config['base_url'] = 'http://localhost/project/';
$config['index_page'] = 'index.php';
$config['uri_protocol'] = 'AUTO';
routes.php文件
$route['default_controller'] = 'home';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
database.php中
$active_group = 'main';
$active_record = TRUE;
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => '',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
$db['main']['hostname']='xx.x.xxx.xx';
$db['main']['username']='this server's username';
$db['main']['password'] ='this server's password';
$db['main']['database'] = 'database name';
$db['main']['dbdriver'] = 'mysql';
$db['main']['dbprefix'] = '';
$db['main']['pconnect'] = TRUE;
$db['main']['db_debug'] = TRUE;
$db['main']['cache_on'] = FALSE;
$db['main']['cachedir'] = '';
$db['main']['char_set'] = 'utf8';
$db['main']['dbcollat'] = 'utf8_general_ci';
$db['main']['swap_pre'] = '';
$db['main']['autoinit'] = TRUE;
$db['main']['stricton'] = FALSE;
);
请帮帮我,我完全是CI的新手。非常感谢。
答案 0 :(得分:0)
除非这是控制器中的拼写错误,否则index()函数上方会有一个额外的右括号。
field.subfield
答案 1 :(得分:0)
这里有很多错误。让我们从您的家庭控制器开始吧。
?>
。 getEmployees()
方法在失败时返回null
,我会稍微简化一下代码。 compact($employees)
功能。它与撰写['employees' => $employees]
相同。 .php
第一个参数中的$this->load->view()
。...
<?php
class Home extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('HomeModel');
}
public function index()
{
$employees = $this->HomeModel->getEmployees();
$this->load->view('index', compact('employees'));
}
}
家庭模型有很多相同的内容。
<?php
class HomeModel extends CI_Model
{
public function getEmployees()
{
$this->db->select('jantina, bangsa, agama');
$this->db->from('pesakit');
return $this->db->get()->result();
}
}
对于index.php页面。
<?php
开头标记。 <?=
等短标签。 foreach
if
,while
,foreach (...) : endforeach;
等块
...
<!DOCTYPE html>
<html lang="en">
<head>
<title>Display Records From Database Using Codeigniter</title>
<link href="<?php echo base_url();?>css/bootstrap.css" rel="stylesheet">
</head>
<body>
<div class="row">
<div style="width:500px;margin:50px;">
<h4>Display Records From Database Using Codeigniter</h4>
<table class="table table-striped table-bordered">
<tr><td><strong>Jantina</strong></td><td><strong>Bangsa</strong></td><td><strong>Agama</strong></td></tr>
<?php foreach ($employees as $employee) : ?>
<tr><td><?php echo $employee->jantina; ?></td><td><?php echo $employee->bangsa; ?></td><td><?php echo $employee->agam; ?></td></tr>
<?php endforeach; ?>
</table>
</div>
</div>
</body>
</html>
我认为config.php
和routes.php
看起来不错。
您的database.php
数组格式中出现语法错误。我只是保持它原来的格式。
<?php
$active_group = 'main';
$active_record = true;
$query_builder = true;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = '';
$db['default']['dbdriver'] = 'mysqli';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = false;
$db['default']['db_debug'] = ENVIRONMENT !== 'production';
$db['default']['cache_on'] = false;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = true;
$db['default']['stricton'] = false;
$db['main']['hostname'] = 'xx.x.xxx.xx';
$db['main']['username'] = 'this server\'s username';
$db['main']['password'] = 'this server\'s password';
$db['main']['database'] = 'database name';
$db['main']['dbdriver'] = 'mysqli';
$db['main']['dbprefix'] = '';
$db['main']['pconnect'] = true;
$db['main']['db_debug'] = true;
$db['main']['cache_on'] = false;
$db['main']['cachedir'] = '';
$db['main']['char_set'] = 'utf8';
$db['main']['dbcollat'] = 'utf8_general_ci';
$db['main']['swap_pre'] = '';
$db['main']['autoinit'] = true;
$db['main']['stricton'] = false;
作为旁注,请阅读PSR-1和PSR-2编码标准。另外,请查看PHP The Right Way。
答案 2 :(得分:-1)
REplace(如果index是您的视图页面名称)
$this->load->view('index.php', $data);
要
$this->load->view('index', $data);