CodeIgniter Forums
what is worng with this foreach and for loops - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: what is worng with this foreach and for loops (/showthread.php?tid=33056)



what is worng with this foreach and for loops - El Forum - 08-13-2010

[eluser]BaRzO[/eluser]
Hi all,

I am having a problem with my code.
My controller.
Code:
<?php

class Welcome extends Controller {

    var $settings;

    function Welcome() {
        parent::Controller();
        $this->load->database();
        $this->load->library('theme_lib');
        $this->load->helper('url');
    }

    function index() {
        $this->theme_lib->_add_js("/* js comment */"); // this works
        for ($i=0; $i<=5; $i++) {
           $this->theme_lib->_add_js("/* js comment */"); // does not work
        }
    }
}

and my theme_lib
Code:
&lt;?php

class Theme_lib {

    var $CI;
    var $_embed_js = '';

    function Theme_lib() {
        $this->CI = & get_instance();

        log_message('debug', __CLASS__ . " Class Initialized");
    }
    function _add_js($js, $type = 'embed') {
        if ($type == 'embed') {
            $this->_embed_js .= $js . "\n";
        } else {
            $this->_js[] = $js;
        }
    }
}

Why this _add_js function does not work in for or foreach loops ?
Pls Help...
Thnks.


what is worng with this foreach and for loops - El Forum - 08-13-2010

[eluser]cahva[/eluser]
Seems to work fine.
Code:
function index()
    {
        $this->load->library('theme_lib');
        $this->theme_lib->_add_js("/* js comment */"); // this works
        for ($i=0; $i<=5; $i++) {
           $this->theme_lib->_add_js("/* js comment */"); // does not work
        }

        echo $this->theme_lib->_embed_js;
    }
..prints 7x "/* js comment */"


what is worng with this foreach and for loops - El Forum - 08-13-2010

[eluser]BaRzO[/eluser]
Yes i did test it the code. It's works but in my app there is something goes wrong.
I am still looking all code but i did find it yet.
Thnks