(11-08-2018, 09:36 AM)Pertti Wrote: Ah right, so you don't overwrite config.php but you do write a file in config folder?
I'd be a bit careful with allowing PHP to write in that folder. If you're can't easily turn these 112 queries into one simple query then yeah, I suppose any kind of caching is solution to go with, either by auto-loading it via config, or having data file generated, lets say in JSON format, that can be loaded when you need it.
Thanks for the pointer. The problem is that I dont have any better idea to store the counts. Essentially they are needed to dynamically add new categories and their corresponding sub-categories and also to catch if for the first time a sub-category has gotten any entries. The best way to automate this without excessively calling the DB at least for the first load would be to store this in a an static file which is updated from time to time serverside so I dont have to babysit it. This provides a huge speed improvement and pushed controller execution time down to aroun 0.15 seconds from 1.2 seconds. The problem is that the site is super content rich and has several 100k entries which are sorted by sub-cats and linked to main cat since the entry can belong to several main-cats.
for now i have defined it in a custom config file. i failed to create a function to create this file on the spot though. I can get the arrays just fine and all but I have little experience in creating a file and saving it. this is my attempt - but it gives no linebreaks (lack of double quotes - but double quotes didnt work either....):
It's a protected function which only runs when a security token is passed and called serverside. I omit that code:
// Load model
$this->load->model('Page_m');
// Prepare custom config file
$custom_config_data = '<\?php ';
$custom_config_data = $custom_config_data . 'defined(\'BASEPATH\') OR exit(\'No direct script access allowed\');';
// get categories
$categories= $this->Page_m->get(array('hidden' => 0, 'released' => TRUE), 'categories');
foreach ($categories as $key_fow => $value_fow)
{
$subentries = $this->Page_m->count_subnavigation($value_fow['id']);
$subentries = json_encode($subentries);
$subentries = str_replace('{', '(', $subentries);
$subentries = str_replace('"A":', '\'A\' => ', $subentries);
$subentries = str_replace('"B":', '\'B\' => ', $subentries);
$subentries = str_replace('"C":', '\'C\' => ', $subentries);
$subentries = str_replace('"D":', '\'D\' => ', $subentries);
$subentries = str_replace('"E":', '\'E\' => ', $subentries);
$subentries = str_replace('"F":', '\'F\' => ', $subentries);
$subentries = str_replace('"G":', '\'G\' => ', $subentries);
$subentries = str_replace('"H":', '\'H\' => ', $subentries);
$subentries = str_replace('}', ');', $subentries);
$subentries = str_replace('"', '', $subentries);
$custom_config_data = $custom_config_data . '$config[\'sub_navigation_entries_' . $value_fow['id'] . '\'] = '. $subentries;
}
$custom_config_data = $custom_config_data . '?>';
// Write config file
//@file_put_contents('./application/config/test_sub_navigation_entries.php', $custom_config_data);
echo $custom_config_data;
This works fairly OK but the first part of the custom_config_data doesnt show. When I run it, I get:
<\?php defined('BASEPATH') OR exit('No direct script access allowed')\;$config['sub_navigation_entries_1'] = ('A' => 1,'B' => 1,'C' => 1,'D' => 1,'E' => 1,'F' => 1,'G' => 1,'H' => 0);$config['sub_navigation_entries_2'] = ('A' => 1,'B' => 1,'C' => 1,'D' => 1,'E' => 1,'F' => 1,'G' => 1,'H' => 0);$config['sub_navigation_entries_3'] = ('A' => 1,'B' => 1,'C' => 0,'D' => 0,'E' => 0,'F' => 0,'G' => 0,'H' => 0);$config['sub_navigation_entries_4'] = ('A' => 1,'B' => 1,'C' => 1,'D' => 1,'E' => 1,'F' => 1,'G' => 1,'H' => 0);$config['sub_navigation_entries_5'] = ('A' => 1,'B' => 1,'C' => 0,'D' => 0,'E' => 0,'F' => 0,'G' => 0,'H' => 0);$config['sub_navigation_entries_6'] = ('A' => 1,'B' => 1,'C' => 1,'D' => 1,'E' => 1,'F' => 1,'G' => 1,'H' => 688);$config['sub_navigation_entries_7'] = ('A' => 1,'B' => 1,'C' => 1,'D' => 0,'E' => 0,'F' => 0,'G' => 0,'H' => 0);$config['sub_navigation_entries_8'] = ('A' => 1,'B' => 1,'C' => 1,'D' => 1,'E' => 1,'F' => 1,'G' => 1,'H' => 0);$config['sub_navigation_entries_9'] = ('A' => 1,'B' => 1,'C' => 1,'D' => 0,'E' => 0,'F' => 0,'G' => 0,'H' => 0);$config['sub_navigation_entries_10'] = ('A' => 1,'B' => 1,'C' => 1,'D' => 1,'E' => 1,'F' => 1,'G' => 1,'H' => 0);$config['sub_navigation_entries_14'] = ('A' => 1,'B' => 0,'C' => 0,'D' => 0,'E' => 8,'F' => 0,'G' => 0,'H' => 0);?>
I must be tired but I dont see why I get <\?php instead of <?php.
Also I would very much to layout the output nicely or can I skip that.