Welcome Guest, Not a member yet? Register   Sign In
Calling db from a view gives me an error
#1

[eluser]CARP[/eluser]
Hi guys
I'm loading DB class inside a view, and it is giving me an error
PS: I just did the controller and its corresponding view. But I call DB from view only

Code:
$this->load->database();

$datapost = array(
    'NOMBRE' => $_POST["camponombre"],
    'APELLIDO' => $_POST["campoapellido"],
    'NROSOCIO' => $_POST["camponrosocio"],
    'ESTABLECIMIENTO' => $_POST["campoestablecimiento"],
    'TELEFONO' => $_POST["campotelefono"],
    'FAX' => $_POST["campofax"],
    'CALLE' => $_POST["campocalle"],
    'CALLENRO' => $_POST["campocallenro"],
    'CALLECP' => $_POST["campocallecp"],
    'POBLACION' => $_POST["campopoblacion"],
    'PROVINCIA' => $_POST["campoprovincia"],
    'EMAIL' => $_POST["campoemail"],
);
$this->db->insert('pg_cliente', $datapost);    
echo '<br>El Cliente fue agregado con exito. Total de clientes => <b>'.$this->db->count_all('pg_cliente').'</b>';

gives: Fatal error: Call to a member function on a non-object in ****\system\application\views\clients\add.php on line 107
#2

[eluser]garymardell[/eluser]
Why are you trying to load the db class in the view. Why not just do it in the model/controller.
#3

[eluser]CARP[/eluser]
Hi gazza
After seeing lot of examples, etc. now I realized that it will work well in the model

Now, an offtopic question:
In which file I can declare a $var (constant) that I can get/call in the whole app? (views, controllers, models)

Thanks
#4

[eluser]garymardell[/eluser]
Again why do you need to?

From your controller load the model.

And pass a $var to say $this->modelName->getDetails($var);

Then in the function return the data to your controller.

Then to get the data into the view,

$data['details'] = $this->modelName->getDetails($var);

$this->load->view('viewname', $data);

Then in your view your data can be found at $details .
#5

[eluser]CARP[/eluser]
Hi gazza
Yes, I did the db calling, etc. in the model, then passed data to controller, and controller passes data to the view. I got it
I also added a constant with DEFINE(...); in the main index.php file, so both qs. are solved Smile
#6

[eluser]xwero[/eluser]
FYI the SVN has an constants file, specially added for defining constants so you don't have to do it in the index.php file
#7

[eluser]CARP[/eluser]
[quote author="xwero" date="1208474236"]FYI the SVN has an constants file, specially added for defining constants so you don't have to do it in the index.php file[/quote]

Awesome! will subscribe to official CI news so I learn when a new version is released




Theme © iAndrew 2016 - Forum software by © MyBB