Welcome Guest, Not a member yet? Register   Sign In
variable global
#1

Hello everyone,

I wanted to create a variable with the name of my table because I always I take the mvc created to create other , eg if I have a view , a controller and a client model ready I simply rename the file and I can make the changes suppliers to view and so forth

But if I had a variable " universal " instead gave change the table name of all the queries I just trade the value of the variable , you were able to understand me ?

Thanks in advance,

An example of model

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class M_lideres extends CI_model {

    /*
     * Recupero todos os lideres da tabela.
     * Se for passado o parâmetro ID do lider, então recupero somente um lider
     */
    function exibe_dados($limite, $offset) {

        $this -> db -> limit($limite, $offset);
        $count = $this -> db -> get('lideres');
        return $count -> result();

    }

    function busca($limite, $offset, $nome) {

        $this -> db -> limit($limite, $offset);
        $this -> db -> where("nome like '%$nome%'");
        $count = $this -> db -> get('lideres');
        return $count -> result();

    }

    function retorna_dados($id = null) {
            
        if ($id != null) {
            $this->db->where("id", $id);
        }
        
        $this->db->order_by("nome");
        return $this->db->get("lideres");
            
    }
    

    // A função abaixo simplesmente salva os dados do lider na tabela.
    // Para utilizá-la, é preciso passar o id e também os dados já formatados.
    function inserir($dados = null){
         
        if ($this->db->insert("lideres", $dados))
            return true;
        else
            return false;
        
     }
    
    // A função abaixo simplesmente salva os dados do lider na tabela.
    // Para utilizá-la, é preciso passar o id e também os dados já formatados.
    function editar($id = null, $dados = null){
         
        if ($this->db->where("id", $id)->update("lideres", $dados))
            return true;
        else
            return false;
        
     }


    function deletar($id) {
        
        $this -> db -> where('id', $id);
        $this -> db -> delete('lideres');
        $resultado = 'excluido';

    }    

}

Now as I would like to stay :

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

variable global $table = 'lideres'

class M_lideres extends CI_model {

    /*
     * Recupero todos os lideres da tabela.
     * Se for passado o parâmetro ID do lider, então recupero somente um lider
     */
    function exibe_dados($limite, $offset) {

        $this -> db -> limit($limite, $offset);
        $count = $this -> db -> get('$table');
        return $count -> result();

    }

    function busca($limite, $offset, $nome) {

        $this -> db -> limit($limite, $offset);
        $this -> db -> where("nome like '%$nome%'");
        $count = $this -> db -> get('$table');
        return $count -> result();

    }

    function retorna_dados($id = null) {
            
        if ($id != null) {
            $this->db->where("id", $id);
        }
        
        $this->db->order_by("nome");
        return $this->db->get("$table");
            
    }
    

    // A função abaixo simplesmente salva os dados do lider na tabela.
    // Para utilizá-la, é preciso passar o id e também os dados já formatados.
    function inserir($dados = null){
         
        if ($this->db->insert("$table", $dados))
            return true;
        else
            return false;
        
     }
    
    // A função abaixo simplesmente salva os dados do lider na tabela.
    // Para utilizá-la, é preciso passar o id e também os dados já formatados.
    function editar($id = null, $dados = null){
         
        if ($this->db->where("id", $id)->update("$table", $dados))
            return true;
        else
            return false;
        
     }


    function deletar($id) {
        
        $this -> db -> where('id', $id);
        $this -> db -> delete('$table');
        $resultado = 'excluido';

    }    

}

Thanks ^^
Reply
#2

You can do this by setting the table name as a property of that class:

PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
M_lideres extends CI_model {

protected 
$table_name "lideres";

 
/*
 * Recupero todos os lideres da tabela.
 * Se for passado o parâmetro ID do lider, então recupero somente um lider
 */
 
function exibe_dados($limite$offset) {

 
$this -> db -> limit($limite$offset);
 
$count $this -> db -> get($this->table_name);
 return 
$count -> result();

 } 

Then change your table references as above to:

PHP Code:
$this->table_name 
Reply
#3

(This post was last modified: 04-21-2015, 03:11 PM by pompeu.)

Hello JayAdra

that's what I need

thanks for helping me but it did not work , gave an error :

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: tabela

Filename: models/m_lideres.php

Line Number: 22

( ! ) Fatal error: Cannot access empty property in E:\Vertrigo\www\sgi\system\core\Model.php on line 51
Call Stack
#    Time    Memory    Function    Location
1    0.0006    355400    {main}( )    ..\index.php:0
2    0.0017    428504    require_once( 'E:\Vertrigo\www\sgi\system\core\CodeIgniter.php' )    ..\index.php:202
3    0.0365    2908704    call_user_func_array ( )    ..\CodeIgniter.php:359
4    0.0365    2908752    Lideres->index( )    ..\CodeIgniter.php:359
5    0.0377    2935024    M_lideres->busca( )    ..\lideres.php:15
6    0.0390    2968160    CI_Model->__get( )    ..\lideres.php:22

My code modified:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class M_lideres extends CI_model {

    protected $tabela = 'lideres';
    /*
     * Recupero todos da tabela.
     * Se for passado o parâmetro ID, então recupero somente um registro
     */
    function exibe_dados($limite, $offset) {

        $this -> db -> limit($limite, $offset);
        $count = $this -> db -> get($this -> $tabela);
        return $count -> result();

    }

    function busca($limite, $offset, $nome) {

        $this -> db -> limit($limite, $offset);
        $this -> db -> where("nome like '%$nome%'");
        $count = $this -> db -> get($this -> $tabela);
        return $count -> result();

    }

    function retorna_dados($id = null) {
            
        if ($id != null) {
            $this -> db -> where('id', $id);
        }
        
        $this -> db -> order_by('nome');
        return $this -> db -> get($this -> $tabela);
            
    }
    

    // A função abaixo simplesmente salva os dados na tabela.
    // Para utilizá-la, é preciso passar o id e também os dados já formatados.
    function inserir($dados = null){
         
        if ($this -> db -> insert($this -> $tabela, $dados))
            return true;
        else
            return false;
        
     }
    
    // A função abaixo simplesmente salva os dados na tabela.
    // Para utilizá-la, é preciso passar o id e também os dados já formatados.
    function editar($id = null, $dados = null){
         
        if ($this -> db -> where('id', $id) -> update($this -> $tabela, $dados))
            return true;
        else
            return false;
        
     }

     // A função abaixo simplesmente apaga o registro na tabela.
    // Para utilizá-la, é preciso passar o id.
    function deletar($id) {
        
        $this -> db -> where('id', $id);
        $this -> db -> delete($this -> $tabela);
        $resultado = 'excluido';

    }    

}

Thanks ^^
Reply
#4

(This post was last modified: 04-21-2015, 03:32 PM by pompeu.)

I discovered the error, it is certain
Code:
$this -> table

And I was using
Code:
$this -> $table

The JayAdra friend informed me that I was wrong right here ^^

Thank you for the help ^^

Problem resolved
Reply




Theme © iAndrew 2016 - Forum software by © MyBB