Calling an Array from a Public Codeigniter View function

Asked

Viewed 262 times

0

I have this public service in Controller:

public function faturar() {

    $this->load->library('form_validation');
    $this->data['custom_error'] = '';


    if ($this->form_validation->run('receita') == false) 
    {
        $this->data['custom_error'] = (validation_errors() ? '<div class="form_error">' . validation_errors() . '</div>' : false);
    } 
    else 
    {
        $vencimento = $this->input->post('vencimento');
        $recebimento = $this->input->post('recebimento');

        try {

            $vencimento = explode('/', $vencimento);
            $vencimento = $vencimento[2].'-'.$vencimento[1].'-'.$vencimento[0];

            if($recebimento != null){
                $recebimento = explode('/', $recebimento);
                $recebimento = $recebimento[2].'-'.$recebimento[1].'-'.$recebimento[0];

            }
        } catch (Exception $e) {
           $vencimento = date('Y/m/d'); 
        }

        $data = array(
            'descricao' => set_value('descricao'),
            'valor' => $this->input->post('valor'),
            'clientes_id' => $this->input->post('clientes_id'),
            'data_vencimento' => $vencimento,
            'data_pagamento' => $recebimento,
            'baixado' => $this->input->post('recebido'),
            'cliente_fornecedor' => set_value('cliente'),
            'forma_pgto' => $this->input->post('formaPgto'),
            'tipo' => $this->input->post('tipo')
        );

        if ($this->os_model->add('lancamentos',$data) == TRUE) { 

            $os = $this->input->post('os_id'); 

            $this->db->set('faturado',1);
            $this->db->set('valorTotal',$this->input->post('valor'));
            $this->db->where('idOs', $os);
            $this->db->update('os'); 

            $this->session->set_flashdata('success','OS faturada com sucesso!');
            $json = array('result'=>  true);
            echo json_encode($json);
            die();
        } else {
            $this->session->set_flashdata('error','Ocorreu um erro ao tentar faturar OS.');
            $json = array('result'=>  false);
            echo json_encode($json);
            die();
        }
    }

    $this->session->set_flashdata('error','Ocorreu um erro ao tentar faturar OS.');
    $json = array('result'=>  false);
    echo json_encode($json);

}

And at View I have this code:

<li>
    <span><h5>Técnico Responsável</h5></span>
    <span><strong>Nome: </strong> <?php echo $result->nome?></span> <br/>
    <span><strong>OS: </strong><?php echo $result->idOs ?></br></span>
    <span><strong>Data Inicial: </strong><?php echo $dataInicial ?></br></span>
    <span><strong>Data Final: </strong><?php echo $dataFinal ?></br></span>
    <span><strong>Data da Impressão desse documento: </strong><?php echo date('d/m/Y'); ?></br></span>
    <span><strong><h4>Status da OS: <?php echo $result->status ?></h4></strong></span>
    <?php  if(faturar(baixado)=1) { echo '<span><strong><h5>Fatura Paga</h5></strong></span>'; } ?>     
</li>

In the last line of the view, where it is written <?php if(faturar(baixado)=1), I want to call the 'downloaded' key from the $data array and check in the database whether the registration has been downloaded or not.

There’s a way for me to do it right on View, without the need to create a Model, and then on Controller and then on View?

1 answer

1


There’s something wrong there! Look:

The method faturar() is found in Controller. So first you won’t be able to access this method without loading your Controller in View, and second, this is conceptually wrong, because the View does not "see" the Controller, see picture at the end of the answer, and third even if you did this, is being passed an argument baixado pro method that is not defined in the signature.

You have not shown where the data that is being entered is coming from li, but you can understand that it comes from a query; you showed the method faturar() that just return a boolean field. You must make a select so that already delivered to the View whether the service order is downloaded or not.

inserir a descrição da imagem aqui

  • Thanks for your help Murilo. I will follow your advice and try here and anything I warn you.

Browser other questions tagged

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