Welcome Guest, Not a member yet? Register   Sign In
When exactly is the database loaded?
#1

[eluser]jwindhorst[/eluser]
I'm creating an application for which I would like to be able to pull some user defined configuration settings from the DB to be accessed globally. I was going to add them to the config file but it would appear I don't have the DB at that point.

Thanks!
#2

[eluser]jwindhorst[/eluser]
Maybe I'm asking the wrong question. I want users to be able to customize settings like LOGO_IMAGE_PATH, and then save that path in the DB. I want to be able to access this path always, globally, without hitting the DB in every controller/view, and I don't want it to be a constant that would require the user to edit a file.

Hope that is more clear.
#3

[eluser]Colin Williams[/eluser]
I would get over the idea of hitting the DB on every page load. If it really does concern you, you can use CI's built-in query caching.
#4

[eluser]jwindhorst[/eluser]
Yea, I was just thinking about that as well, but there is another hurdle with my plan to take over the world.

I want to set CSS values based on DB settings. So I moved stylesheet.css to stylesheet.php and use:
Code:
<?php header("Content-type: text/css"); ?>
at the top of the css file. Then call it like normal with it's new name:
Code:
<link rel="stylesheet" type="text/css" media="screen" href="style.php">

The PHP is firing inside of the CSS file, but I don't seem to have access to CI in there. Any thoughts on the new and improved problem? I did try adding get_defined_constants() and get_defined_vars() inside the php/css file but they only get the big php stuff, nothing application specific.

Thanks!
#5

[eluser]Colin Williams[/eluser]
Well, if you want to hit a CI page, take what you have in style.php and put it in a Controller file, and point your URI to that Controller.
#6

[eluser]jwindhorst[/eluser]
True, but then it's not global to the application. Currently the css file is in /assets/css/template_name/ directory. Would moving it to /system/application/views/template_name/css/ get my CI core back?
#7

[eluser]Colin Williams[/eluser]
No. It has to be run through CI. So, you'd have application/controllers/style.php

Code:
class Style extends Controller {

  function index()
  {
    // Your php/css here
  }

}

and the URI would be http://www.example.com/index.php/style
#8

[eluser]jwindhorst[/eluser]
Right, but then it is all echo'd out in the controller right? Making for ugly inline styles in the source which I was trying to avoid. I'm not sure if it can be done any other way, but I would much prefer a more elegant solution.
#9

[eluser]Colin Williams[/eluser]
You really are failing to connect the dots I'm showing you.

Code:
class Style extends Controller {

  function index()
  {
    $this->output->set_header("Content-type: text/css");
    // This is way over-simplified, which is probably going to trip you up again
    $css_data = $this->db->get('css_data');
    $this->load->view('style', $css_data);
  }

}

Code:
<link rel="stylesheet" type="text/css" media="screen" href="/index.php/style">
#10

[eluser]jwindhorst[/eluser]
Perhaps the reason I wasn't seeing it that way is because I have my own version of the view function in MY_Loader to grab header and footer templates and load numerous views between:
Code:
function view($views, $vars = array(), $return = FALSE)
    {  

        // pass any header vars on to the header    
        $header_vars = (isset($vars['header_data'])) ? $vars['header_data'] : array();

        // pass any footer vars on to the header    
        $footer_vars = (isset($vars['header_data'])) ? $vars['header_data'] : array();

        $doc = $this->_ci_load(array('_ci_view' => 'templates/' . TEMPLATE . '/layout/header', '_ci_vars' => $this->_ci_object_to_array($header_vars), '_ci_return' => $return));

        // if it's an array we need to loop through them
        if(is_array($views))
            foreach($views as $view)
                $doc .= $this->_ci_load(array('_ci_view' => 'templates/' .TEMPLATE . "/".$view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
        else
            $doc .= $this->_ci_load(array('_ci_view' => 'templates/'.TEMPLATE."/".$views, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));

        $doc .= $this->_ci_load(array('_ci_view' => 'templates/'.TEMPLATE.'/layout/footer', '_ci_vars' => $this->_ci_object_to_array($footer_vars), '_ci_return' => $return));
        return $doc;
    }

But I could likely create a work around in there for the css files.




Theme © iAndrew 2016 - Forum software by © MyBB