Welcome Guest, Not a member yet? Register   Sign In
Static variable not showing
#1

[eluser]Pellens[/eluser]
Hi!

I'm having 3 controllers:
index.php
about.php
contact.php


I have a model called seo_model.php:

Code:
<?
    class Seo_model extends Model
    {
        static $keywords = array("keyword1","keyword2");

        static function insert_keywords($words)
        {
            Seo_model::$keywords = array_merge( Seo_model::$keywords , $words );
            Seo_model::$keywords = array_unique(Seo_model::$keywords);
        }
        
        static function keywords()
        {
            $string = "";    
            $aantal = count(Seo_model::$keywords);
            $i      = 1;
            
            foreach(Seo_model::$keywords as $word):

                $string.= $word;
                if($i < $aantal) $string.=",";
                $i++;
            
            endforeach;
            
            return $string;
        }
    }

In my controller about.php I user the following code:

Code:
&lt;?

    class About extends Controller
    {
    
        public function __construct()
        {
            parent::Controller();
            
            // LOADING MODELS HERE
        }
        
        public function index()
        {
            $data["main_content"] = "index/homepage";
            
            // SEO
                        // I add keywords to the static $keywords
            $words = array("keyword3","keyword4");
            Seo_model::insert_keywords($words);
            
            $this->load->view("index",$data);
        }
    }

In my view file index.php I just need to do this:
Code:
&lt;meta name="keywords" content="&lt;?=$this-&gt;seo_model->keywords();?&gt;"/>

(The seo_model.php is autoloaded in autoload.php)


THE PROBLEM

The seo model works: it adds the new keywords, BUT
Code:
&lt;?=$this->seo_model->keywords();?&gt;
only works if I go to www.example.com/about/index, and not if I go to www.example.com/about.

I hope I'm being clear here...
I don't want to add /index at every controller.
Anyone knows the solution?

Thanks!
#2

[eluser]Narkboy[/eluser]
Ok, couple of points:

- I'm not seeing an explict load model call in the controller; auto-loaded?

- In about.php you insert_keywords using a direct call rather than a CI call ($this->Seo_model->insert_keywords("foo)). Try to use the same calling method throughout.

- You declared all the Seo_model methods as static, which is useful if you're not creating an instance of the class, but you are. When you call the model CI instances the class as $this->Seo_model. I think that this is the reason you're getting patchy results, but I'm guessing. I avoid static with CI - private is always useful though!

/B




Theme © iAndrew 2016 - Forum software by © MyBB