Welcome Guest, Not a member yet? Register   Sign In
DMCV Help - database , model, controller, view, and echo data
#1

[eluser]osxxso[/eluser]
Been going through the user_guide and having a difficult time making the connections between the database, model, controller, and view.

Basically, what I have is a simple database with one table to store company information ( title, address, city, phone, etc... )

I'm auto loading the database ( as explained in the user_guide ) and a model that I created...

autoload.php

Code:
$autoload['libraries'] = array('database');

AND

Code:
$autoload['model'] = array('Company_model');

As a simple example all I'm trying to accomplish ( at this point ) is to echo the phone number on the website that is stored in the database. This should be so simple but I'm not able to make the connections between database, model, controller, and view.

Could someone show me an example of on how to accomplish this using a model, a controller, and a view.

Thanks in advance!





#2

[eluser]Samus[/eluser]
First make sure you add in your database connection details in application/config/database.php

Code:
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'eap';

These are the main ones.

Second.
You're going to need to write some code in your model to retrieve data.

e.g

Code:
class Company_model extends CI_Model {

function get_info($id) {
$q = $this->db->get_where('table_name', array('id' => $id));
if($q->num_rows() > 0) {
return $q->row();
}
else {
return FALSE;
}
}
}

then in your controller:
Code:
class Home extends CI_Controller {
function index() {
$data['result'] = $this->Company_model->get_info(1);
$this->load->view('view_page', $data);
}
}

Code:
<?php
if(!empty($result)) { // checks to see if there are any results
echo $result->phone;
}
else {
// echo some error stating there's no results.
}

Bare in notice I used 'id' so you should create a unique column in your table to identify each record.

That's a quick example, hope you take time to look through it, also don't be afraid of using the user guide, it's very user friendly and explained well. It has all the information you'd need in getting started.
#3

[eluser]osxxso[/eluser]
Thanks Samus!

Got it working with your help.

Instead of using this function on the page:

Code:
if(!empty($company)) { // checks to see if there are any results
echo $company->phone;
}
else {
// echo some error stating there's no results.
}

I implemented the function to the controller instead:

Code:
if(!empty($company))
{
  // checks to see if there are any results
  $this->load->view('pages/'.$page, $data);
}
else
{
  // echo some error stating there's no results
}

And on the page:

Code:
<?php echo $company->phone; ?>

Seems cleaner? Let me know if this will cause any issues.



#4

[eluser]Samus[/eluser]
[quote author="osxxso" date="1334376833"]Thanks Samus!

Got it working with your help.

Instead of using this function on the page:

Code:
if(!empty($company)) { // checks to see if there are any results
echo $company->phone;
}
else {
// echo some error stating there's no results.
}

I implemented the function to the controller instead:

Code:
if(!empty($company))
{
  // checks to see if there are any results
  $this->load->view('pages/'.$page, $data);
}
else
{
  // echo some error stating there's no results
}

And on the page:

Code:
<?php echo $company->phone; ?>

Seems cleaner? Let me know if this will cause any issues.



[/quote]

Yeah that works too. But I prefer my method, because you may still have values in the $data array. And it's pretty much user friendly to display a 'There are no results' message rather than a blank page.




Theme © iAndrew 2016 - Forum software by © MyBB