Welcome Guest, Not a member yet? Register   Sign In
Controller not recieving my parameter
#1

[eluser]Joures[/eluser]
So i'm on my second day developing with CI and ran in to a little problem
i can't solve.

So, i got this anchor leading to localhost/folder_containing_ci/subcategories/display/1
where the number 1 is and id from the parent of that subcategory.

The thing is, when i click the link this error occurs: Undefined property: Subcategories::$id
And the display function looks like this:

Code:
<?php

class Subcategories extends Controller {
    
    public function display($id) {
        
     // fetching subcategories depending on $id
    }
}

Everywhere i look it says 'CI will pass down the parameter automatically'.

What am i doing wrong?
#2

[eluser]jalalski[/eluser]
Is there some code in the display function that we're not seeing? The error 'Undefined property:' indicates that there is somewhere a $this->id being used.
#3

[eluser]Rolly1971[/eluser]
try this:

Code:
public function display() {
     $id = $this->uri->segment(3);
     // fetching subcategories depending on $id
}

ofc change the number to what it is depending on your full uri
#4

[eluser]cahva[/eluser]
Try to get this to work without using $this->uri->segment(3) as that should work.

So what does this get you? Its almost the same but with constructor:
Code:
class Subcategories extends Controller {

    function __construct()
    {
        parent::__construct();
    }

    public function display($id = FALSE)
    {
        echo $id;
    }
}
#5

[eluser]Joures[/eluser]
Yeah, ther is some code you're not seeing

Code:
<?php

class Subcategories extends Controller {
    
    public function display($id) {
        
     // fetching subcategories depending on $id
     $vars['categories'] = Doctrine_Query::create()
         ->select('s stuff')
         ->from('Stuff s')
         ->where('s.Parent_id = ?', $this->id)
         ->execute();
    }
}
I've tried your solutions, and it works, as long as i'm referencing $id alone, instead of $this->id. Any ideas as to why that is?
#6

[eluser]cahva[/eluser]
Well $this->id is not the same as $id. $id has scope only in that method and is entirely different variable. So use just $id.
#7

[eluser]Joures[/eluser]
Allright, then i know why. Thanks for the help folks Smile




Theme © iAndrew 2016 - Forum software by © MyBB