CodeIgniter Forums

Full Version: access variable from different method within same model
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]ranjitbd[/eluser]
Code:
// this is a model class
class Holiday_model extends Model
{
  function Holiday_model()
  {
      parent::Model();
      $this->load->database(‘holiday’, TRUE);
  }

  function view_package()
  {
      $sql = “select * from package_details”;
// i want to use this $sql in the method get_package_details()mentioned below

      $query = $this->db->query($sql);
      return $query->result_array();
  }

  function get_package_details($id)
  {
// here how can i access the $sql from view_package() method mentioned above.and set it within $new_sql
    
      $new_sql = ;//please write here the code

      $row =  $new_sql.” where id = ‘$id’”;
    
      $query = $this->db->query($row);
      return $query->row_array();
  }

}
?>

El Forum

[eluser]umefarooq[/eluser]
yes you can access with in model class just you have to define variable at class level

Code:
class Holiday_model  extends Model{
public $id;
public $var;

  function get_function(){
    $this->id;
  }

  function get_function2(){
   $this->id;
   $this->var;
  }
}

in controller you can access these variables also because these are defined public

$this->Holiday_model->id = 1;

El Forum

[eluser]ranjitbd[/eluser]
[quote author="umefarooq" date="1257436282"]yes you can access with in model class just you have to define variable at class level

Code:
class Holiday_model  extends Model{
public $id;
public $var;

  function get_function(){
    $this->id;
  }

  function get_function2(){
   $this->id;
   $this->var;
  }
}

in controller you can access these variables also because these are defined public

$this->Holiday_model->id = 1;

class Holiday_model extends Model{

function get_function(){
public $id;
}

function get_function2(){
$this->id; // can i use like this. $id is declared public in the get_function method()
}
}

in controller you can access these variables also because these are defined public

$this->Holiday_model->id = 1;
[/quote]