Welcome Guest, Not a member yet? Register   Sign In
Will CI detect connection itself?
#1

(This post was last modified: 05-17-2016, 07:24 AM by chan15. Edit Reason: typo )

There is a helper need to check something by connect to database in a loop, for example:

view

PHP Code:
<?php foreach ($datas as $data): ?>
<div><?php echo check_data($data->id); ?></div>
<?php endforeach ?>

helper

PHP Code:
<?php

if (!function_exists('check_data')) {
 
  function check_data($id)
 
  {
 
      $ci =& get_instance();
 
      $ci->load->model('Members''members'true);
 
      
       
// check logic is in model
 
      if ($ci->members->check_result()) {
 
          return 'yes'
 
      }
 
      
       
return false;
 
  }


I don't like to connect to database in construct or in autoload, I like to declare it in where need it, but I was wonder if CI smart enough to check if the connection is exists, or it will do the connection thing as much as data exists as example.
Reply
#2

I'm not sure I completely understand what you mean by "check the existing connection" but codeigniter needs to have the database loaded beforehand.

Is there any particular reason you don't like loading the database with the framework?
Codeigniter is simply one of the tools you need to learn to be a successful developer. Always add more tools to your coding arsenal!
Reply
#3

(05-17-2016, 07:43 PM)albertleao Wrote: I'm not sure I completely understand what you mean by "check the existing connection" but codeigniter needs to have the database loaded beforehand.

Is there any particular reason you don't like loading the database with the framework?

I loading the database with the framework, I mean I don't like to load it in __construction, if I have three methods in a controller, I will load database in each of them, even connect to the same database.

I supposed $this->load->model('Members') means to build a MySQL connection, so I was wonder if I call a helper with $this->load->model('Members') 10 times will do 10 times MYSQL connection, that's my question.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB