Welcome Guest, Not a member yet? Register   Sign In
Global mysql query
#1

[eluser]epseix[/eluser]
In an attempt to try and narrow my query down, I'm still very new to Codeigniter framework...

I want to define global variables (eg. in autoloaded helpers) and use global mysql queries throughout my site - but I don't understand how to do the latter (global mysql queries).

I understand the concept of defining single variables in a helper:

Code:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('get_title'))
{
    function get_title($var = 'My website title')
    {
        return $var;
    }  
}

... and I understand the concept of creating a single mysql query in a model, loading it in a controller and using it in a view file (with a foreach loop):

Model
Code:
<?php
    function get_recommended_link()
    {
  $query = $this->db->query('SELECT * FROM links1 ORDER BY RAND() LIMIT 1');
  return $query->result();
    }

Controller
Code:
<?php
class Home extends CI_Controller {

    public function index()
    {
  $this->load->model('Default_model');
  ...
  $data['recommended_link'] = $this->Default_model->get_recommended_link();
  $this->load->view('page_view', $data);
    }

View
Code:
<h2>Recommended Link</h2>
    <ul>
   &lt;?php if (isset($recommended_link)):?&gt;
    &lt;?php foreach ($recommended_link as $row):?&gt;
     <li><a href="&lt;?=$row-&gt;url?&gt;">&lt;?=$row->link?&gt;</a></li>
    &lt;?php endforeach;?&gt;
   &lt;?php endif;?&gt;
    </ul>

How (and where) do I create a mysql query that can be autoloaded (or whatever) and used anywhere on my site - without the need to load it in every controller?

Is there a simpler way to do all of the above?
#2

[eluser]Ckirk[/eluser]
You can autoload the model, if that's what you're getting at.
At the bottom of application/config/autoload.php you'll see the models option

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

The model will be available to all controllers without having to load it then. Does that help?
#3

[eluser]epseix[/eluser]
[quote author="Ckirk" date="1372251809"]You can autoload the model, if that's what you're getting at.
At the bottom of application/config/autoload.php you'll see the models option

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

The model will be available to all controllers without having to load it then. Does that help?[/quote]

So, theoretically I can keep all global queries in a separate model and autoload it - allowing me to access it anywhere on my website? Perfect!

One more thing - and I DID warn you I was a newbie - the view example will no longer work as the controller loads the query as $recommended_link, which isn't defined anymore - it would now be $this->... ?? in the foreach loop?
#4

[eluser]Ckirk[/eluser]
It should still be defined because you still call the function in the model from the controller.


Code:
&lt;?php
class Home extends CI_Controller {

    public function index()
    {
  //$this->load->model('Default_model');  Autoloaded now
  ...
  $data['recommended_link'] = $this->Default_model->get_recommended_link();
  $this->load->view('page_view', $data);
    }

correct?
#5

[eluser]epseix[/eluser]
[quote author="Ckirk" date="1372255533"]It should still be defined because you still call the function in the model from the controller.


Code:
&lt;?php
class Home extends CI_Controller {

    public function index()
    {
  //$this->load->model('Default_model');  Autoloaded now
  ...
  $data['recommended_link'] = $this->Default_model->get_recommended_link();
  $this->load->view('page_view', $data);
    }

correct?[/quote]

Do I have to call the function in the controller?

How would I use the autoloaded function in view without having to call every function in every controller?
#6

[eluser]Syllean[/eluser]
Check out Phil Sturgeon's article CodeIgniter Base Classes: Keeping it DRY.
#7

[eluser]Ckirk[/eluser]
You could indeed have a call to the function from the view but then you're breaking the principles of MVC.
You should have all your business logic in that model and use the controller to call it. A viewer is exactly that... a view.
Syllean linked a useful post by the legendary Phil Sturgeon. It's a worth while read Smile

I use a different method to autoload though, if you're following his post. Min goes like so:

Code:
if ( function_exists("__autoload") )
{
spl_autoload_register("__autoload");
}

function MY__autoload($class)
{
if(strpos($class, 'CI_') !== 0)
{
  $file = APPPATH . 'core/'.$class.'.php';
     if (file_exists($file) && is_file($file))
     {
          @include_once($file);
  }
  
  }
}
spl_autoload_register("MY__autoload");




Theme © iAndrew 2016 - Forum software by © MyBB