Welcome Guest, Not a member yet? Register   Sign In
Some questions from novice..
#1

[eluser]scor[/eluser]
Hi all. I hope you'll help me to find a solution. Also.
There is my class:
Code:
class User extends Controller {
        
    function __construct(){
      parent::__construct();
        $session_id=$this->session->userdata('session_id'); //The error called this line
        if(!empty($session_id)){
            $user_id=$this->session->userdata('user_id');
            if (!is_numeric($user_id) OR $user_id < 1)
                header('Location:'.site_url('/'));            
        }
        else
            header('Location:'.site_url('/'));    
    }    
      
    function links($link_id=0){    
        parent::Controller();
        
        if(is_numeric($link_id) && $link_id >= 1){    

            $q = Doctrine_Query::create()->from('Links')->where('id  = '.$link_id);
            $ret=$q->execute();
            $data['data']['link_id'] = $ret[0]['id'];
            $data['data']['link'] = $ret[0]['link'];
            
            $q = Doctrine_Query::create()->from('ActiveLink')->where('link_id  = '.$data['data']['link_id'].' AND user_id='.$this->session->userdata('user_id'));
            $data['data']['user_links']=$q->execute();  //Error. If $data['data']['user_links'] is empty, then all is ok, else there is an error.
            
            $data['tpl']='user/user_links_info';
            $this->load->view('header');
            $this->load->view('user/user', $data);
            $this->load->view('footer');            
        }        
        else{
            $q = Doctrine_Query::create()->from('Links')->orderBy('id DESC');
            $data['data']['links'] = $q->execute();
  
            $data['tpl']='user/user_links';
            $this->load->view('header');
            $this->load->view('user/user', $data);
            $this->load->view('footer');
        }
    }
        
}
1. My autoload.php loaded session library. There is a foreach construction in a view. When data is empty (foreach doesn't work) - it's all ok, but if there is any data in the array, then appears an error:
Quote:A PHP Error was encountered
Severity: Notice
Message: Undefined property: User::$session
Filename: controllers/user.php
Line Number: 6

Fatal error: Call to a member function userdata() on a non-object in /home/virtwww/http/system/application/controllers/user.php on line 6
Why is it so?

2. If I try to use
Code:
if(isset($this->session->userdata('session_id'))
it appears an error
Quote:Fatal error: Can't use method return value in write context
Where am I wrong?
#2

[eluser]TheFuzzy0ne[/eluser]
First of all, I'm not sure why you're calling parent::Controller() from your links method, I think it's unnecessary.

As for your first error, it sounds like you might not be running PHP 5. Try this instead:

Code:
class User extends Controller {
        
    function User(){
        parent::Controller();
        $session_id=$this->session->userdata('session_id'); //The error called this line
        if(!empty($session_id)){
            $user_id=$this->session->userdata('user_id');
            if (!is_numeric($user_id) OR $user_id < 1)
                header('Location:'.site_url('/'));            
        }
        else
            header('Location:'.site_url('/'));    
    }    
      
    function links($link_id=0){    
        
        if(is_numeric($link_id) && $link_id >= 1){    

            $q = Doctrine_Query::create()->from('Links')->where('id  = '.$link_id);
            $ret=$q->execute();
            $data['data']['link_id'] = $ret[0]['id'];
            $data['data']['link'] = $ret[0]['link'];
            
            $q = Doctrine_Query::create()->from('ActiveLink')->where('link_id  = '.$data['data']['link_id'].' AND user_id='.$this->session->userdata('user_id'));
            $data['data']['user_links']=$q->execute();  //Error. If $data['data']['user_links'] is empty, then all is ok, else there is an error.
            
            $data['tpl']='user/user_links_info';
            $this->load->view('header');
            $this->load->view('user/user', $data);
            $this->load->view('footer');            
        }        
        else{
            $q = Doctrine_Query::create()->from('Links')->orderBy('id DESC');
            $data['data']['links'] = $q->execute();
  
            $data['tpl']='user/user_links';
            $this->load->view('header');
            $this->load->view('user/user', $data);
            $this->load->view('footer');
        }

As for your second error, you can't use isset() on a function, plus you shouldn't need to, as the session class returns FALSE if the specified key is not set.

EDIT: Come to think about it, if you weren't running PHP 5, then __construct wouldn't be called. Try loading the session class from within the constructor. I suspect you might not be using the correct syntax in your autoload.php.
#3

[eluser]scor[/eluser]
Tehre is this code in my autoload.php
Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$autoload['libraries'] = array('session');
$autoload['helper'] = array('url');
$autoload['plugin'] = array('doctrine');
$autoload['config'] = array();
$autoload['language'] = array();
$autoload['model'] = array();

I checked my PHP's version through phpinfo() function and it was 5.2.6.
I thought, if it was a library's error, then it appears every time I run a script. It's unusually, that there is no error, if an array is empty... =\
#4

[eluser]TheFuzzy0ne[/eluser]
Have you tried loading the session class from within the constructor? Also, does your log file say that the session class has been loaded?
#5

[eluser]scor[/eluser]
I solved my problem. There was an error in doctrine query $q = Doctrine_Query::create()->from('ActiveLink')->where('link_id = '.$data['data']['link_id'].' AND user_id='.$this->session->userdata('user_id')); and it returned a wrong answer. And it recorded in $data['data']['user_links']. I didn't know why, but if $data['data']['user_links'] included an doctrined error hash, there was this error... It worked perfectly, if $data['data']['user_links'] included valid data.
#6

[eluser]TheFuzzy0ne[/eluser]
Glad you got it sorted.




Theme © iAndrew 2016 - Forum software by © MyBB