-1

I'm creating a website and I've been trying to connect to the database, I really can't find the problem. all I need is to get all "job_desc" from my database "bcjobs" and after a user can click a "job_desc" for him to be able to see the full details of a job. here's my code

Model:

<?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();
        }

}

?>

Controller:

<?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);
    }

}

?>

application/views/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 ?>

application/views/joblist/view.php:

<?php
echo '<h2>'.$job_item['title'].'</h2>';
echo $job_item['text'];

routes:

$route['jobs/(:any)'] = 'joblist/$1';
$route['jobs'] = 'joblist';
$route['(:any)'] = 'pages/$1';
$route['default_controller'] = 'pages';
tereško
  • 58,060
  • 25
  • 98
  • 150
pic-a
  • 13
  • 1
  • 6
  • 1
    what's the issue? Are you getting results? Are you getting an error? Did you hook up your database in the `config/database.php` file? – brightintro May 29 '13 at 02:41
  • yes I did configured the database already it displays error 404 page not found – pic-a May 29 '13 at 02:51
  • Does the DB user you're trying to connect as have permission to connect remotely? – J.D. Pace May 29 '13 at 02:55
  • Comment out `$this->load->database()` and see if you get expected PHP errors. Drop some of these `echo __LINE__, "
    \n"; exit();` throughout the code to attempt to isolate the problem.
    – J.D. Pace May 29 '13 at 02:59
  • can you please provide the url that you are trying to access. Any extra information you provide can only benefit you. People want to help, but you have to help them help you. – brightintro May 29 '13 at 03:05
  • J.D.Pace I'm just using localhosti tried what you said but the error still dint change. ekims here is the url I'm accessing http://localhost/bcjobs_final/index.php/jobs – pic-a May 29 '13 at 03:06
  • I don't think this is a error because of database connection. Can you try to create a new empty controller... and run it. IF the database can not acccess, CI will display the db can not connect error for you. – JamesN May 29 '13 at 03:23

1 Answers1

0

Based on your comment it looks like your routes are incorrect.

Here is a little overview on CI routes

$route['journals'] = "blogs";

url -> localhost/index.php/journals

The above example would redirect to the blogs class anytime the word journals was found in the first uri segment. CI Reference

In your case, you are looking for jobs in the first segment, but incorrectly routing it to a non-existent class (assuming this since you didn't post anything about a joblist controller).

So you have two options; either rename your controller, or modify your routes.

Option 1: Rename your controller from Jobs to Joblist. And update the route:

$route['jobs/(:any)'] = 'joblist/view/$1';

Option 2:

For your url: localhost/bcjobs_final/index.php/jobs

$route['jobs/(:any)'] = 'jobs/view/$1';
brightintro
  • 1,016
  • 1
  • 11
  • 16
  • Thanks for that ekims it did work but another error came out "Call to a member function get_job() on a non-object in C:\wamp\www\bcjobs_final\application\controllers\jobs.php on line 11" I'll try to resolve this one for now thanks and God Bless – pic-a May 29 '13 at 06:08
  • In your model change the construct to read `$this->load->library('database');` – brightintro May 29 '13 at 14:09