1

i am developing web application by using yii framework, i have a table called "studentinformation" and shown below

id  name   mothername   phone        classid   parentname      email
1. xxxxx     asdf      9658741230       2        pqrs        [email protected]   

this is my another table

classid  classname
    1.      class1
    2.      class2
    3.      class3

i am trying to import excel file data into database and In my excel file i gave classname instead of classid and convert from classname to classid and store into database everything working fine. this is my excel file columns with data

 id  name   mothername   phone         classid    parentname      email
 1.  xxxxx    asdf       9658741230     class10      pqrs      [email protected] 

My Requirement:- if i give wrong classname in my excel file which means that classname is not there in my database, at that time it should display one alert like "your class name is wrong" for example In above excel table i gave classid is "class10" but class10 is not there in my database table at that it should display some alert instead of this error "Trying to get property of non-object" here i am trying but i am not getting could you please help me..

 $classdet = Classdetails::model()->findByAttributes(array('classname'=>'Class10','School_Id'=>1),'Status=1');
    if($classdet->classname != 'Class10')
    {
        echo '<script language="javascript">';
        echo 'alert("your classname is wrong")';
        echo '</script>';
    }
    else{
        print_r($classdet);
        }

1 Answer 1

1

If the class does not exist, you can't access it's attributes. Therefore $classdet->classname causes Trying to get property of non obeject error. So you should change your code into this:

$classdet = Classdetails::model()->findByAttributes(array('classname'=>'Class10','School_Id'=>1),'Status=1');
if($classdet == null)
{
    echo '<script language="javascript">';
    echo 'alert("your classname is wrong")';
    echo '</script>';
}
else{
    print_r($classdet);
    }
Sign up to request clarification or add additional context in comments.

1 Comment

ya i got it thanks but i have one doubts how to display alert inside my try catch block because i have this function inside my try catch block could you please tell me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.