Welcome Guest, Not a member yet? Register   Sign In
Load multiple models within same function of controller
#1

I have getting below error when I try to load two model in one view.. 
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: i_price
Filename: item/edit.php
Line Number: 75
Backtrace:
File: C:\wamp64\www\MY\admin\application\views\item\edit.php
Line: 75
Function: _error_handler

File: C:\wamp64\www\MY\admin\application\controllers\Item.php
Line: 75
Function: view

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


My Model:-

PHP Code:
function update_view($id)
 
   {
 
       $query $this->db->query("SELECT * FROM item WHERE item_id='$id'");
 
       $row $query->row();
 
       return $row;
 
   }
 
   function item_price($id)
 
   {
 
       $query $this->db->query("SELECT price FROM price WHERE item_id='$id'");
 
       $i_price $query->row();
 
       return $i_price;
 
   


Controller:-

PHP Code:
function update($id)
 
   {
 
       if ($this->session->userdata('NAME'))
 
       {
 
           $role $this->session->userdata('ROLE');
 
           if ($role >= 1)
 
           {
 
               $this->load->helper('form');
 
               $this->load->library('form_validation');
 
               if ($this->input->post()) {
 
                   $this->form_validation->set_rules('item_name''Item Name''required');

 
                   if ($this->form_validation->run() === TRUE) {

 
                       $this->Item_model->update($id);
 
                       $this->session->set_flashdata('message_name''Item Successfully Updated.</br>');
 
                       redirect('item/create');
 
                   }
 
               }
 
               $data['row'] = $this->Item_model->update_view($id);
 
               $data['$i_price'] = $this->Item_model->item_price($id);
 
               //var_dump($data['$item_price_data']); die();
 
               $this->load->view('includes/header');
 
               $this->load->view('includes/top_header');
 
               $this->load->view('includes/left_nav');
 
               $this->load->view('item/edit'$data);
 
               $this->load->view('includes/footer');
 
               $this->load->view('includes/settings');
 
           }
 
           else{
 
               $this->session->set_flashdata('message_name''You did not have permission to access this page.');
 
               redirect('user/index');
 
           }

 
       }
 
       else
        
{
 
           $this->session->set_flashdata('message_name''Your session has been expired. Please Login');
 
           redirect('login/employee_login');
 
       
View:-

Code:
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
   <!-- Content Header (Page header) -->
   <section class="content-header">
       <h1>New Stock Item</h1>
   </section>
   <!-- Main content -->
   <section class="content">
       <div class="row">
           <div class="col-xs-12">
               <div class="box">
                   <div class="box-header">
                       <h3 class="box-title">Enter Stock Item Details Here</h3>
                   </div>
                   <!-- /.box-header -->
                   <div class="box-body">
                       <div class="row">
                           <div class="col-md-12">
                               <span class="text-danger"><?php echo validation_errors() ?></span>
                               <p style="color: red; font-weight: 900;">
                                   <?php echo $this->session->flashdata('message_name');?>
                               </p>
                               <form action="<?=base_url()?>item/update/<?php echo $row->item_id?>" method="post" enctype="multipart/form-data">
                                   <div class="box-body">
                                       <div class="form-group">
                                           <label>Item Name</label>
                                           <input type="text" class="form-control" name="item_name" value="<?php echo $row->item_name ?>">
                                       </div>
                                       <!--
                                       <div class="form-group">
                                           <label>Select a Company</label>
                                           <select class="form-control select2" style="width: 100%;" name="company_id" >
                                               <option>Please Select a Company</option>
                                               <?php foreach ($company as $name){
                                           ?>
                                                   <option value="<?php echo $name->id ?>" ><?php echo $name->company_name ?></option>
                                                   <?php
                                       }
                                       ?>
                                           </select>
                                       </div>
                                       -->


                                       <div class="form-group">
                                           <div>
                                               <?php
                                               $item_image = $row->image;
                                               if (file_exists($row->image))
                                               {
                                                   echo '<img  src="'.base_url().$item_image.'" class="img-thumbnail" width="30%">';
                                               } else {
                                                   echo '<img  src="'.base_url().'uploads/items/default.jpg'.'" class= img-responsive img-circle">';
                                               }
                                               ?>
                                           </div>
                                           <label for="exampleInputFile">Photo</label>
                                           <input type="file" id="exampleInputFile" name="photo">

                                           <p class="help-block">Upload item image..</p>
                                       </div>
                                       <div
                                       <div class="form-group">
                                           <label for="exampleInputPassword1">Description</label>
                                           <script src="<?php echo base_url(); ?>assets/bower_components/ckeditor/ckeditor.js"></script>
                                           <textarea id="editor1" name="description" rows="10" cols="80">
                                               <?php echo $row->description?>
                                            </textarea>
                                           <script>
                                               CKEDITOR.replace( 'editor1' );
                                           </script>
                                       </div>
                                       <div class="form-group">
                                           <label>Item Price</label>
                                           <input type="text" class="form-control" name="price" value="<?php echo $i_price->price?>">
                                       </div>
                                   </div>
                                   <!-- /.box-body -->
                                   <div class="box-footer">
                                       <div class="row">
                                           <div class="col-md-3">

                                           </div>
                                           <div class="col-md-3">
                                               <button type="submit" class="btn btn-info btn-lg" >Update</button>

                                           </div>
                                           <div class="col-md-3">

                                               <a href="<?=base_url()?>item/all_items/" class="btn btn-info btn-lg" >Cancel</a>
                                           </div>
                                           <div class="col-md-3">

                                           </div>
                                       </div>
                                   </div>
                               </form>
                           </div>

                       </div>
                   </div>
                   <!-- /.box-body -->
               </div>
               <!-- /.box -->
           </div>
           <!-- /.col -->
       </div>
       <!-- /.row -->
   </section>
   <!-- /.content -->
</div>
<!-- /.content-wrapper -->


Any one can help me for resolve this error?
Reply
#2

Are you sure your getting the price back?

I don't see you doing any debugging to check for a price.


PHP Code:
$query $this->db->query("SELECT price FROM price WHERE item_id='$id'");
$row   $query->row();

if (isset(
$row))
{
 
   $i_price $row;
}
else
{
 
   // ERROR no Price Found!
 
   $i_price 0;
}

return 
$i_price

You should always check to make sure that your getting back the values that you want.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

When I var_dump this variable like var_dump($data['$item_price_data']); die(); I can see the price come. But when I use it like this a have get this error..
Reply
#4

Your assigning row an object for views if it is passed into a $data it is an associated array.

To view that in a CodeIgniter view you need to assign it in the Controller to a $data associated array.

Try assigning $row = row_array();

You cannot just create a variable and access it in a view because they are not global.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

Am I try this "$row = row_array();" in my model or controller ?
Reply
#6

In your Model that's were you are returning it.

function item_price
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

I think the problem lies here

PHP Code:
$data['$i_price'] = $this->Item_model->item_price($id); 

Note the `$` in the string.

Try this instead

PHP Code:
$data['i_price'] = $this->Item_model->item_price($id); 

The there will be a $i_price variable in the view  (Assuming, as pointed out by others, the model returned something.)
Reply
#8

Thanks. It's Work.. Thank you Every one..
Reply




Theme © iAndrew 2016 - Forum software by © MyBB