Welcome Guest, Not a member yet? Register   Sign In
How to call a function in CI
#1

[eluser]masentinel900[/eluser]
I did viewed that CodeIgniter Is only POO.

In the POO I need to call a function for that this runs. That will be like this:

<code>
$something -> name_function();
</code>

My problem is that I don´t know how to do for to call a function.
If a have more than two functions and I need that these works at same. How I can get it?
#2

[eluser]solid9[/eluser]
If you are inside a class you can call it like this,

Code:
$this->name_function();

For two functions you can call it like this,
Code:
$this->name_function1();
$this->name_function2();

I suggest you read the CodeIgniter docs before asking.
and the PHP OOP manual.
#3

[eluser]CroNiX[/eluser]
I think you need to differentiate between "functions" and "methods". Functions are standalone, like trim($var). Methods are "functions" within classes, and require the class to work, $this->method_name.

With CI Controllers, you can't access a controller method outside of the current controller class, unless you use HVMC or something (which isn't standard).

#4

[eluser]skunkbad[/eluser]
[quote author="masentinel900" date="1350394872"]I did viewed that CodeIgniter Is only POO.

In the POO I need to call a function for that this runs. That will be like this:

<code>
$something -> name_function();
</code>

My problem is that I don´t know how to do for to call a function.
If a have more than two functions and I need that these works at same. How I can get it?[/quote]

POO?
#5

[eluser]CroNiX[/eluser]
Programming Orientated Objects?
#6

(This post was last modified: 06-19-2017, 09:43 AM by ciadmin.)

[eluser]masentinel900[/eluser]
I´m POO. I going to show the next.

Controller Verifylogin
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class VerifyLogin extends CI_Controller
{
public function __construct()
{
 parent::__construct();
 $this->load->model('user','',TRUE);
}
function index()
{
 //This method will have the credentials validation
 $this->load->library('form_validation');
 $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
 $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
 if($this->form_validation->run() == FALSE)
 {
  //Field validation failed.  User redirected to login page
  $this->load->view('login_view');
 }
 else
 {
 //Go to private area
 redirect('home', 'refresh');
 }
}
function check_database($password)
{
 //Field validation succeeded.  Validate against database
 $username = $this->input->post('username');
 //query the database
 $result = $this->user->login($username, $password);
 if($result)
 {
  $sess_array = array();
  foreach($result as $row)
  {
   $sess_array = array(
   'id' => $row->id,
   'username' => $row->username,
   'nombre' => $row->nombre,
   'rutaimagen' => $row->rutaimagen,
   'acercademi' => $row->acercademi,
   );
   $this->session->set_userdata('logged_in', $sess_array);
  }
 return TRUE;
 }
 else
 {
  $this->form_validation->set_message('check_database', 'Invalid username or password');
  return false;
 }
}
 
}
?&gt;

Controller "Archivos"
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start(); //we need to call PHP's session object to access it through CI
class Archivos extends CI_Controller
{
public function __construct()
{
 parent::__construct();
}

function index()
{
 if($this->session->userdata('logged_in'))
 {
 $session_data = $this->session->userdata('logged_in');
 $data['nombre'] = $session_data['nombre'];
 $data['acercademi'] = $session_data['acercademi'];
 //$data['hola'] = "Diego";
 //$data['diego'] = "Chiao bambini!!";
 $this->load->view('archivos', $data);
 }
 else
 {
  //If no session, redirect to login page
  redirect('login', 'refresh');
 }
}

}
?&gt;

By last my file HTML called "archivos" where I to call the variables are inside method "Index" on the controller file "Archivos".

Here there not any problem.

Obviusly This works about The Model file "user" with th method called Login.

But is that I need in the Model file to make another called a Database different. When I create A new method in my "Archivos" controller don´t works. simply void it.

How I do for may create more methods and that works it.




Theme © iAndrew 2016 - Forum software by © MyBB