-1

Hihow r u all?

I m trying to calculate basic salary with Particular employee_id but when i m trying to .. give me fatal error..

Fatal error: Call to a member function num_rows() on a non-object in D:\wamp\www\template\application\models\salary.php on line 112

my code is: model

<?php
class Salary extends Model
{   
    /*
    Determines if a given person_id is a profile
    */
    function exists($salary_id)
    {
        $this->db->from('salary_scale');
        $this->db->where('id',$salary_id);
        $this->db->where('deleted',0);      
        $query = $this->db->get();

        return ($query->num_rows()==1);
    }
    /*
    Determines if a given employee_id is a employee
    */
    function exists_employee($employee_id)
    {
        $this->db->from('grade_history');
        $this->db->where('employee_id',$employee_id);
        $query = $this->db->get();

        return ($query->num_rows()==1);
    }

    /*
    Returns all the suppliers
    */
    function get_all()
    {
        $this->db->from('salary_scale');
        $this->db->where('deleted', 0);
        $this->db->order_by("name", "asc");
        return $this->db->get();
    }

        /*
         *
         *  Gets information about a particular employees salary
     */
    function grade_rules_info($salary_grade)
    {
        $this->db->from('salary_scale_rules');
        $this->db->where('salary_grade',$salary_grade);
        $query = $this->db->get();

        if($query->num_rows()==1)
        {
            return $query->row();
        }
        else
        {
            //Get empty base parent object, as $item_id is NOT an item
            $salary_obj=new stdClass();

            //Get all the fields from items table
            $fields = $this->db->list_fields('salary_scale_rules');

            foreach ($fields as $field)
            {
                $salary_obj->$field='';
            }

            return $salary_obj;
        }
    }

        /*
         *
         *  Gets information about a particular employees salary
     */
    function get_info($employee_id)
    {
        $this->db->from('allowance');
        //$this->db->join('deductions', 'deductions.eid = allowance.eid');
        $this->db->where('employee_id',$employee_id);
        $query = $this->db->get();

        if($query->num_rows()==1)
        {
            return $query->row();
        }
        else
        {
            //Get empty base parent object, as $item_id is NOT an item
            $salary_obj=new stdClass();

            //Get all the fields from items table
            $fields = $this->db->list_fields('items');

            foreach ($fields as $field)
            {
                $salary_obj->$field='';
            }

            return $salary_obj;
        }
    }

        /**
         * Gets information about a particular employees salary
         *
     **/
    function get_grade_info($employee_id)
    {
        $this->db->from('grade_history');
        $this->db->where('employee_id',$employee_id);
        $query = $this->db->get();

        if($query->num_rows()==1)
        {
            return $query->row();
        }
        else
        {
            //Get empty base parent object, as $employee_id is NOT an employee
            $grade_obj=new stdClass();

            //Get all the fields from items table
            $fields = $this->db->list_fields('grade_history');

            foreach ($fields as $field)
            {
                $grade_obj->$field='';
            }

            return $grade_obj;
        }
    }

        /**
     * Inserts or updates configuration data
         *
     */

    function save_grade(&$data, $employee_id=false)
    {
        if (!$employee_id)
        {
            if($this->db->insert('grade_history', $data))
            {
                $data['id']=$this->db->insert_id();
                return true;
            }
            return false;
        }

        $this->db->where('id', $employee_id);
        return $this->db->update('grade_history', $data);
    }

        // ------------------------ End of save_salary_cinfig function -----------------------------


        /*
         * Basic pay salary calculation
         */
        function basic_pay($employee_id)
        {
            // Get grade information from grade_history table
            // Get Particular person grade ifnormation by employ_id
            $grade_info = $this->get_grade_info($employee_id);

            $salary_grade           = $grade_info->salary_grade;
            $no_of_increment        = $grade_info->number_of_increment;

            // Get Grade rules information about particular grade by passing $salary_grade number

            $grade_rules            = $this->grade_rules_info($salary_grade);

            $basic_amount           = $grade_rules->basic_amount;
            $increment              = $grade_rules->increment;
            $max_no_of_increment    = $grade_rules->number_of_increment;
            $max_amount             = $grade_rules->max_amount;

            return $basic_pay = $basic_amount + $increment*$no_of_increment;


        }
}
?>

controller

<?php
require_once ("secure_area.php");
class Salaries extends Secure_area
{
    function __construct()
    {
        parent::__construct('salaries');
    }

    function index()
    {
        $data['controller_name']=strtolower($this->uri->segment(1));
        $data['form_width']=$this->get_form_width();
        $data['manage_table']=get_profile_manage_table($this->Profile->get_all(),$this);
        $this->load->view('salaries/manage',$data);
    }

    /*
    Returns profile table data rows. This will be called with AJAX.
    */
    function search()
    {
        $search=$this->input->post('search');
        $data_rows=get_profile_manage_table_data_rows($this->Profile->search($search),$this);
        echo $data_rows;
    }

    /*
    Gives search suggestions based on what is being searched for
    */
    function suggest()
    {
        $suggestions = $this->Profile->get_search_suggestions($this->input->post('q'),$this->input->post('limit'));
        echo implode("\n",$suggestions);
    }

    /*
    Loads the profile form
    */
    function view($employee_id=-1)
    {
        //$data['salary_summary_info']  =$this->Salary->get_info($salary_id);
        //$data['salary_deductions_summary_info'] =$this->Salary->get_deductions_info($salary_id);

                $employee_id = array('' => '-- Select Employee ID --');
                foreach($this->Profile->get_employee_id()->result_array() as $row)
        {
            $employee_id[$row['employee_id']]= $row['employee_id'];
        }
                $data['employee_id'] = $employee_id;
               // $data['selected_employee_id'] = $this->Profile->get_info($employee_id)->employee_id;

                $data['basic_pay'] = $this->Salary->basic_pay($employee_id);

        $this->load->view("salaries/salary_summary_form", $data);
    }
        //--------------------------------------End view function-----------------------------------

    /*
    Loads the profile form
    */
    function grade_view($employee_id=-1)
    {
        $data['salary_grade_info']  = $this->Salary->get_grade_info($employee_id);

                $data['basic_pay'] = $this->Salary->basic_pay($employee_id);

        $this->load->view("salaries/grade_view", $data);
    }
        //--------------------------------------End view function-----------------------------------


    /*
    Inserts/updates a profile
    */
    function save_salary_grade($id=-1)
    {
        $grade_data = array(

                    'eid'                   =>$this->input->post('employee_id'),
                    'salary_grade'          =>$this->input->post('salary_grade'),
                    'number_of_increment'   =>$this->input->post('number_of_increment'),
                    'comments'              =>$this->input->post('comments'),
                    'date'                  =>date('Y-m-d H:i:s')

        );

        if($this->Salary->save_grade($grade_data, $id))

        {   //New profile
            if($id==-1)
            {
                echo json_encode(array('success'=>true,'message'=>$this->lang->line('profiles_successful_adding').' '.
                $grade_data['salary_grade'].' '.$grade_data['number_of_increment'],'id'=>$grade_data['eid']));
            }
            else //previous profile
            {
                echo json_encode(array('success'=>true,'message'=>$this->lang->line('profiles_successful_updating').' '.
                $grade_data['salary_grade'].' '.$grade_data['number_of_increment'],'id'=>$grade_data['eid']));
            }
        }
        else//failure
        {   
            echo json_encode(array('success'=>false,'message'=>$this->lang->line('profiles_error_adding_updating').' '.
            $grade_data['salary_grade'].' '.$grade_data['number_of_increment'],'id'=>$grade_data['id']));
        }
    }

    /*
    This deletes profiles from the profiles table
    */
    function delete()
    {
        $profiles_to_delete=$this->input->post('ids');

        if($this->Profile->delete_list($profiles_to_delete))
        {
            echo json_encode(array('success'=>true,'message'=>$this->lang->line('profiles_successful_deleted').' '.
            count($profiles_to_delete).' '.$this->lang->line('profiles_one_or_multiple')));
        }
        else
        {
            echo json_encode(array('success'=>false,'message'=>$this->lang->line('profiles_cannot_be_deleted')));
        }
    }

    /*
    get the width for the add/edit form
    */
    function get_form_width()
    {           
        return 900;
    }
}
?>

and view

<?php
echo form_open('salaries/save/'.$employee_id, array('id'=>'salary_summary_form'));

//echo form_open('salaries/save/', array('id'=>'salary_summary_form'));
?>
<div id="required_fields_message"><?php echo $this->lang->line('common_fields_required_message'); ?></div>
<ul id="error_message_box"></ul>
<fieldset id="salary_allowance_info">
<legend><?php echo $this->lang->line("salaries_allowance_info"); ?></legend>

    <div class="field_row clearfix">
    <?php echo form_label($this->lang->line('salaries_employee_id').':', 'employee_id', array('class'=>'required')); ?>
            <div class='form_field'>
                <?php  echo form_dropdown('employee_id', $employee_id);?>
            </div>
    </div>

    <div class="field_row clearfix">
    <?php echo form_label($this->lang->line('salaries_allowance_basic_salary').':', 'basic_salary', array('class'=>'required')); ?>
            <div class='form_field'>
            <?php echo form_input(array(
                    'name'=>'basic_salary',
                    'id'=>'basic_salary',
                    'value'=>$basic_pay
                    ));
            ?>
            </div>
    </div>

    <div class="field_row clearfix">
    <?php echo form_label($this->lang->line('salaries_allowance_house_rent').':', 'house_rent',array('class'=>'required')); ?>
            <div class='form_field'>
            <?php echo form_input(array(
                    'name'=>'house_rent',
                    'id'=>'house_rent',
//                    'value'=>$salary_summary_info->house_rent
                    ));
            ?>
            </div>
    </div>



<?php
echo form_submit(array(
    'name'=>'submit',
    'id'=>'submit',
    'value'=>$this->lang->line('common_submit'),
    'class'=>'submit_button float_right')
);

?>
</fieldset>
<?php
echo form_close();
?>

pls help me if any one

mu is too short
  • 426,620
  • 70
  • 833
  • 800
masud010
  • 1
  • 1
  • 1
  • 1
  • 1
    Probably, one of the `db->get()` calls doesn't return an object. You're not telling what database wrapper you're using, so we can't tell why from the code you show. What does the database wrapper's documentation say on the method's return value? – Pekka May 01 '11 at 07:01
  • 1
    @Pekka웃 why do people never post what framework/library they're using and expect us to guess that? – Eduard Luca Apr 24 '13 at 02:32

2 Answers2

0

You don't test if your call to $db -> get() succeeded. I don't know the details of your $db class, but I suspect it only returns something if the call to it was successful. If the query fails does $db -> get() still return something?

Try doing a var_dump on what you get out of $db -> get() so you can see if it's returning what you think it's returning.

GordonM
  • 31,179
  • 15
  • 87
  • 129
0

Fatal error: Call to a member function num_rows() on a non-object... is usually because there were no results return from a given query or possibly from a bad query.

Andy Fleming
  • 7,655
  • 6
  • 33
  • 53
  • It usually means the value (database connection object here) is `undefined`. I think 'no results from the query' is a little misleading. – AlexP Oct 21 '12 at 16:48
  • 1
    I don't think it's misleading. The variable is `$query`, which is supposed to hold the results from the query. If the database object was null, the error would be from accessing `get()`, not `num_rows()`. –  Dec 21 '12 at 00:40