Compare codeigniter array fields

Asked

Viewed 323 times

1

Good night!

I am exporting reports, in my form are sent via $_POST the fields that the user wants to export, I receive and assign to a variable all fields. I need to compare these fields with the fields coming back from the database.

Array from the form

    Array
(
    [0] => razaoSocial
    [1] => data
)

Array from the Database

Array
(
    [0] => Array
        (
            [chamado_codigo] => 1
            [data] => 2015-09-01
            [razaoSocial] => Cooperativa de Crédito
            [cliente_cidade] => Chapeco
            [tecnico_nome] => jacomasio
        )

    [1] => Array
        (
            [chamado_codigo] => 2
            [data] => 2015-09-02
            [razaoSocial] => Cooperativa de Crédito
            [cliente_cidade] => Chapeco
            [tecnico_nome] => Eduardo
        )

)

Controller

public function exportaRelatorio(){

        $this->load->model('relatorios_model');

        $filtro = $_POST['filtro'];
        $de = $_POST['de'];
        $ate = $_POST['ate'];
        $campos = $_POST['campos'];

        foreach ($campos as $campo => $valor) {
            $dados[$campo] = $valor;
        }

        $retorno = $this->relatorios_model->ChamadosCliente($filtro, 
            converteData($de), 
            converteData($ate));
}

Model

public function ChamadosCliente($filtro, $de, $ate){
        $this->db->select('chamados.id as chamado_codigo,
            chamados.data_abertura as data,
            clientes.razaoSocial,
            clientes.cidade as cliente_cidade,
            usuarios.nome as tecnico_nome');
        $this->db->from('chamados');
        $this->db->join('clientes', 'chamados.cliente_id = clientes.id');
        $this->db->join('usuarios', 'chamados.tecnico_id = usuarios.id');
        $this->db->where('clientes.id', $filtro);
        $this->db->where("chamados.data_abertura BETWEEN '$de' AND '$ate'");
        return $this->db->get()->result_array();
    }

In this case for example, I wanted to export only the fields razaoSocial and date, which was the one that the user filtered.

How can I do that?

Thank you!

  • Good evening, could you post your controller and model code, please.

  • I added comfort requested

1 answer

1


Take a look, see if this solves:

<?php
$arrFields =  Array(
    "razaoSocial",
    "data"
);

$arrData = Array(
    0 => Array (
        "chamado_codigo"    => 1,
        "data"              => '2015-09-01',
        "razaoSocial"       => 'Cooperativa de Crédito',
        "cliente_cidade"    => 'Chapeco',
        "tecnico_nome"      => 'jacomasio'
    ),
    1 => Array (
        "chamado_codigo"    => 2,
        "data"              => '2015-09-02',
        "razaoSocial"       => 'Cooperativa de Crédito',
        "cliente_cidade"    => 'Chapeco',
        "tecnico_nome"      => 'Eduardo'
    )
);

foreach ($arrData as $key => $val):
    print '<br />'. $key .' | '. $val;  
    foreach ($arrFields as $fld):
        print ' ---- '. $fld .' : '. $val[$fld];
    endforeach ;
endforeach;
?>
  • Man, you’re wild, you solved my problem. Thank you so much!

  • @Eduardopaludo if the answer helped you can give vote + also (in addition to marking as accepted). Marty: it would be possible to add an explanation in the reply in addition to the code to be more complete?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.