我正在创建一个网站,我一直在尝试连接到数据库,我真的找不到问题。我只需要从我的数据库“bcjobs”中获取所有“job_desc”,然后用户可以单击“job_desc”让他能够查看工作的全部详细信息。这是我的代码
模型:
<?php
class Jobs_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function get_job($slug = FALSE){
if($slug === FALSE){
$query = $this->db->get('employer_postjob');
return $query->result_array();
}
$query = $this->db->get_where('employer_postjob',array('job_desc'=>$slug));
return $query->row_array();
}
}
?>
控制器:
<?php
class Jobs extends CI_Controller {
public function __contruct() {
parent::__construct();
$this->load->model('jobs_model');
}
public function index() {
$data['job'] = $this->jobs_model->get_job();
$data['title'] = 'Job List';
$this->load->view('templates/header', $data);
$this->load->view('templates/navigation', $data);
$this->load->view('joblist/index', $data);
$this->load->view('templates/footer', $data);
}
public function view($slug) {
$data['job_item'] = $this->jobs_model->get_job();
if (empty($data['job_item'])) {
echo "no data";
}
$data['title'] = $data['job_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('templates/navigation', $data);
$this->load->view('joblist/view', $data);
$this->load->view('templates/footer', $data);
}
}
?>
应用程序/视图/joblist/index.php:
<?php foreach ($job as $job_item): ?>
<h2><?php echo $job_item['title'] ?></h2>
<div id="main">
<?php echo $job_item['text'] ?>
</div>
<p><a href="joblist/<?php echo $job_item['slug'] ?>">View article</a></p>
<?php endforeach ?>
应用程序/views/joblist/view.php:
<?php
echo '<h2>'.$job_item['title'].'</h2>';
echo $job_item['text'];
路线:
$route['jobs/(:any)'] = 'joblist/$1';
$route['jobs'] = 'joblist';
$route['(:any)'] = 'pages/$1';
$route['default_controller'] = 'pages';