Welcome Guest, Not a member yet? Register   Sign In
Cannot Pass More than two variable to view
#1

Hello,
 I try to get total of price column in personal assets table. But I have below error.. 

A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: employee/inventory.php
Line Number: 61
Backtrace:
File: C:\wamp64\www\MY\admin\application\views\employee\inventory.php
Line: 61
Function: _error_handler

File: C:\wamp64\www\MY\admin\application\controllers\Employee.php
Line: 223
Function: view

File: C:\wamp64\www\MY\admin\index.php
Line: 315
Function: require_once


My Model: 

PHP Code:
<?php

Class Employee_model extends CI_model
{
    public function 
__construct()
    {
        
parent:: __construct();
    }

public function 
view_data($id)
 
   {
 
       $query $this->db->query("SELECT
               sgh.employee.id,
               sgh.employee.employee_name,
               sgh.employee.designation,
               sgh.employee.join_date,
               sgh.employee.b_day,
               sgh.employee.address,
               sgh.employee.mobile_number,
               sgh.employee.photo,
               sgh.company.company_name,
               sgh.employee.company_id
               FROM
               sgh.company,
               sgh.employee WHERE sgh.employee.company_id=sgh.company.id AND sgh.employee.id='
$id'");
 
       $employee_data $query->row();
 
       return $employee_data;

 
   }

 
   public function view_inventory($id){
     
   $query $this->db->query("SELECT sgh.inventory.id,
                                    sgh.inventory.item_name,
                                    sgh.inventory.price,
                                    sgh.inventory.qty,
                                    sgh.inventory.serial_number,
                                    sgh.inventory.product_key,
                                    sgh.employee.id,
                                    sgh.employee.employee_name
                                  FROM sgh.inventory,
                                  sgh.employee WHERE sgh.inventory.employee_id=sgh.employee.id AND sgh.employee.id='
$id'");
     
   $item_data $query->result_array();
 
       return $item_data;
 
   }

 
   function employee_spends($id)
 
   {
 
       $query $this->db->query(" SELECT SUM(price) FROM inventory WHERE employee_id='$id'");
 
       $row $query->row();
 
       return $row;
 
   }


This My Controller
PHP Code:
function personal_inventory($id)
 
   {
 
       $employee_data["row"] = $this->Employee_model->view_data($id);
 
       $inventory_data["item"] = $this->Employee_model->view_inventory($id);
 
       //Total spends for current employee
 
       $inventory_data["total"] = $this->Employee_model->employee_spends($id);
 
       var_dump$inventory_data["total"]);
 
      // var_dump( $inventory_data);
 
       if ($inventory_data["item"]== null) {
 
           $this->load->view('includes/header');
 
           $this->load->view('includes/top_header');
 
           $this->load->view('includes/left_nav');
 
           $this->load->view('error_page/no_records'$employee_data);
 
           $this->load->view('includes/footer');
 
           $this->load->view('includes/settings');
 
       } else {
 
           $this->load->view('includes/header');
 
           $this->load->view('includes/top_header');
 
           $this->load->view('includes/left_nav');
 
           $this->load->view('employee/inventory'$inventory_data);
 
           $this->load->view('includes/footer');
 
           $this->load->view('includes/settings');
 
       }
 
   

This is my view
PHP Code:
<!-- Content WrapperContains page content -->
<
div class="content-wrapper">
 
   <!-- Content Header (Page header) -->
 
   <section class="content-header">
 
    <h2>Personal Inventory Area</h2>
 
   </section>
 
   <!-- Main content -->
 
   <section class="content">
 
       <div class="row">
 
           <div class="col-xs-12">
 
               <div style="margin-bottom: 10px;" >
 
                       <a href="<?= base_url() ?>employee/employee_list/" class="btn btn-info btn-lg">Back
                            to 
List</a>

 
               </div>
 
               <div class="box">
 
                   <div class="box-header">
 
                       <h3 class="box-title">Inventory Items</h3>
 
                   </div>
 
                   <!-- /.box-header -->
 
                   <div class="box-body">
 
                       <table id="example1" class="table table-bordered table-striped table-hover">
 
                           <thead>
 
                           <tr>
 
                               <th>Item Name</th>
 
                               <th>Price</th>
 
                               <th>Quantity</th>
 
                               <th>Serial Number</th>
 
                               <th>Product Key</th>

 
                           </tr>
 
                           </thead>
 
                           <tbody>
 
                           <?php
                            foreach 
($item as $row){
 
                               ?>
                                <tr>
                                    <td><?=$row["item_name"]?></td>
                                    <td class="text-left">Rs. <?=$row["price"]?>.00</td>
                                    <td><?=$row["qty"]?></td>
                                    <td><?=$row["serial_number"]?></td>
                                    <td><?=$row["product_key"]?></td>
                                </tr>
                                <?php
                            
}
 
                           ?>

                            </tbody>
                            <tfoot>
                            <tr>
                                <th>Item Name</th>
                                <th>Price</th>
                                <th>Quantity</th>
                                <th>Serial Number</th>
                                <th>Product Key</th>
                            </tr>
                            </tfoot>
                        </table>
                    </div>
                    <div>
                        <P>Total Spends is <?=$row->price?></P>
                    </div>
                    <!-- /.box-body -->
                </div>
                <!-- /.box -->
            </div>
            <!-- /.col -->
        </div>
        <!-- /.row -->
    </section>
    <!-- /.content -->
</div>
<!-- /.content-wrapper --> 
Reply
#2

This is line 61:
PHP Code:
<P>Total Spends is <?=$row->price?></P> 

It gives an error because $row is an array, not an object.

So change it into:
PHP Code:
<p>Total Spends is <?=$row["price"];?></p> 
like the way you've done in line 38 to 43.
Reply
#3

(04-21-2018, 02:25 PM)Wouter60 Wrote: This is line 61:
PHP Code:
<P>Total Spends is <?=$row->price?></P> 

It gives an error because $row is an array, not an object.

So change it into:
PHP Code:
<p>Total Spends is <?=$row["price"];?></p> 
like the way you've done in line 38 to 43.
I have added this code. But it's gave only one product price "Total Spends is 128000" But total spend should be 139909. This correct calculation comes with var_bump like "object(stdClass)#26 (1) { ["SUM(price)"]=> string(6) "139909" }".  Have you any idea, Why does not come this correct sum value to the view? But its came to header of the view as var_dump. Looks my screenshot.  

Attached Files Thumbnail(s)
   
Reply
#4

Okay, I see it now. The $row->price or $row["price"] on line 61 should be the total amount.
I think this will do the trick:
PHP Code:
<p>Total Spends is <?=$total;?></p> 

You're passing the array $inventory_data to the view. One of the elements in this array is "total", right? Use that to display the total amount.
Reply
#5

(04-21-2018, 11:56 PM)Wouter60 Wrote: Okay, I see it now. The $row->price  or $row["price"] on line 61 should be the total amount.
I think this will do the trick:
PHP Code:
<p>Total Spends is <?=$total;?></p> 

You're passing the array $inventory_data to the view. One of the elements in this array is "total", right? Use that to display the total amount.



Hello Wouter60,

Now I got this error. 

A PHP Error was encountered
Severity: 4096
Message: Object of class stdClass could not be converted to string
Filename: employee/inventory.php
Line Number: 61
Backtrace:
File: C:\wamp64\www\MY\admin\application\views\employee\inventory.php
Line: 61
Function: _error_handler

File: C:\wamp64\www\MY\admin\application\controllers\Employee.php
Line: 223
Function: view

File: C:\wamp64\www\MY\admin\index.php
Line: 315
Function: require_once
Reply
#6

(04-21-2018, 11:56 PM)Wouter60 Wrote: Okay, I see it now. The $row->price  or $row["price"] on line 61 should be the total amount.
I think this will do the trick:
PHP Code:
<p>Total Spends is <?=$total;?></p> 

You're passing the array $inventory_data to the view. One of the elements in this array is "total", right? Use that to display the total amount.



Hello Wouter60,

Now I got this error. 

A PHP Error was encountered
Severity: 4096
Message: Object of class stdClass could not be converted to string
Filename: employee/inventory.php
Line Number: 61
Backtrace:
File: C:\wamp64\www\MY\admin\application\views\employee\inventory.php
Line: 61
Function: _error_handler

File: C:\wamp64\www\MY\admin\application\controllers\Employee.php
Line: 223
Function: view

File: C:\wamp64\www\MY\admin\index.php
Line: 315
Function: require_once
Reply
#7

Check your model, and change the function employee_spends() :

PHP Code:
function employee_spends($id)
{
    
$query $this->db->query("SELECT SUM(price) as total FROM inventory WHERE employee_id=" $id);
    if (
$query->num_rows() == 0) {
        return 
0;
    }
    else {
        return 
$query->row()->total;
    }

Reply
#8

(04-22-2018, 10:08 AM)Wouter60 Wrote: Check your model, and change the function employee_spends() :

PHP Code:
function employee_spends($id)
{
    
$query $this->db->query("SELECT SUM(price) as total FROM inventory WHERE employee_id=" $id);
    if (
$query->num_rows() == 0) {
        return 
0;
    }
    else {
        return 
$query->row()->total;
    }
}

Hey Wouter60

Its workThank your support.. 
Reply
#9

You have to call it <?=$row->price?> as in array format.
Reply
#10

PHP Code:
// Returns an Object:

result()       - ( $row->title; )
row()          - ( $row->title; )

// Returns an Array:

result_array() - ( $row['title']; )
row_array()    - ( $row['title']; ) 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB