Welcome Guest, Not a member yet? Register   Sign In
How to read two model functions in a view?
#1

[eluser]Sinclair[/eluser]
Hi,

I'am with some dificulty in using 2 model functions inside one view.

Model functions to use:
Code:
function getAnuncioDetalheActivo($pid_anuncio) {
        $query = $this->db->query("select e.id_anunciante, e.n_anunciante, e.nome_abrev
        from ate_anunciantes e, atw_anuncios a, atp_pag_anuncios pa
        where
        a.id_anuncio = pa.id_anuncio and
        a.id_anunciante = pa.id_anunciante and
        e.id_anunciante = a.id_anunciante and
        pa.dat_inicio <= NOW() and
        pa.dat_fim >= NOW() and
        a.id_anuncio = '".$pid_anuncio."'");
        return $query->result();
    }
    
    function getServicosAnuncioDetalheActivo($pid_anuncio) {
        $query = $this->db->query("select s.n_servico
        from atw_anuncios a, atp_pag_anuncios pa, atw_servicos_anuncios sa, atw_servicos s
        where
        a.id_anuncio = pa.id_anuncio and
        a.id_anunciante = pa.id_anunciante and
        a.id_anuncio = sa.id_anuncio and
        a.id_anunciante = sa.id_anunciante and
        sa.id_servico = s.id_servico and
        pa.dat_inicio <= NOW() and
        pa.dat_fim >= NOW() and
        a.id_anuncio = '".$pid_anuncio."'");
        return $query->result();
    }

Here is my controller:
Code:
function detalhe($pid_anunciante) {
        $this->load->model('acomp_model');
        $data['result'] = $this->acomp_model->getAnuncioDetalheActivo($pid_anuncio = $this->uri->segment(3));
        $data2['result'] = $this->acomp_model->getServicosAnuncioDetalheActivo($pid_anuncio = $this->uri->segment(3));
        $this->load->view('detalhe_view', $data);
        $this->load->view('detalhe_view', $data2);    
    }

I know thar the code in the controller is not correct, but how can I read the 2 model function to one view?

Here is the view:
Code:
foreach($result as $row) {
    echo $row->id_anunciante; echo '<br />';
    echo $row->n_anunciante;
/* echo anchor('anuncios/zona/' . $row->id_zona, $row->id_zona); */
}

If you can help me, please reply.


Best Regards,
#2

[eluser]imn.codeartist[/eluser]
[quote author="Sinclair" date="1255824516"]Hi,

I'am with some dificulty in using 2 model functions inside one view.

Model functions to use:
Code:
function getAnuncioDetalheActivo($pid_anuncio) {
        $query = $this->db->query("select e.id_anunciante, e.n_anunciante, e.nome_abrev
        from ate_anunciantes e, atw_anuncios a, atp_pag_anuncios pa
        where
        a.id_anuncio = pa.id_anuncio and
        a.id_anunciante = pa.id_anunciante and
        e.id_anunciante = a.id_anunciante and
        pa.dat_inicio <= NOW() and
        pa.dat_fim >= NOW() and
        a.id_anuncio = '".$pid_anuncio."'");
        return $query->result();
    }
    
    function getServicosAnuncioDetalheActivo($pid_anuncio) {
        $query = $this->db->query("select s.n_servico
        from atw_anuncios a, atp_pag_anuncios pa, atw_servicos_anuncios sa, atw_servicos s
        where
        a.id_anuncio = pa.id_anuncio and
        a.id_anunciante = pa.id_anunciante and
        a.id_anuncio = sa.id_anuncio and
        a.id_anunciante = sa.id_anunciante and
        sa.id_servico = s.id_servico and
        pa.dat_inicio <= NOW() and
        pa.dat_fim >= NOW() and
        a.id_anuncio = '".$pid_anuncio."'");
        return $query->result();
    }

Here is my controller:
Code:
function detalhe($pid_anunciante) {
        $this->load->model('acomp_model');
        $data['result'] = $this->acomp_model->getAnuncioDetalheActivo($pid_anuncio = $this->uri->segment(3));
        $data2['result'] = $this->acomp_model->getServicosAnuncioDetalheActivo($pid_anuncio = $this->uri->segment(3));
        $this->load->view('detalhe_view', $data);
        $this->load->view('detalhe_view', $data2);    
    }

I know thar the code in the controller is not correct, but how can I read the 2 model function to one view?

Here is the view:
Code:
foreach($result as $row) {
    echo $row->id_anunciante; echo '<br />';
    echo $row->n_anunciante;
/* echo anchor('anuncios/zona/' . $row->id_zona, $row->id_zona); */
}

If you can help me, please reply.


Best Regards,[/quote]

Here is the solution of your problem

Make some changes in your controller as follows:

Code:
function detalhe($pid_anunciante) {
        $this->load->model('acomp_model');
        $data['result'] = $this->acomp_model->getAnuncioDetalheActivo($pid_anuncio = $this->uri->segment(3));
        $data['result1'] = $this->acomp_model->getServicosAnuncioDetalheActivo($pid_anuncio = $this->uri->segment(3));
        $this->load->view('detalhe_view', $data);
        
    }

Here is the view:
Code:
foreach($result as $row) {
    echo $row->id_anunciante; echo '<br />';
    echo $row->n_anunciante;
/* echo anchor('anuncios/zona/' . $row->id_zona, $row->id_zona); */
}

Code:
foreach($result1 as $row) {
    // echo your second result here
}

Hope this will make sense to you.
#3

[eluser]Sinclair[/eluser]
Great! It is working now!

One more question, how to pass the variables from the controller to the view?

For example, this code(view) works, with the foreach:
Code:
&lt;?php
foreach($result as $row) {
    echo $row->id_anunciante; echo '<br />';
    echo $row->n_anunciante;
/* echo anchor('anuncios/zona/' . $row->id_zona, $row->id_zona); */
}

But how to print the variables outside foreach? I have made an experience that does not work:
Code:
&lt;?php
echo $data->result->id_anunciante;

My question. How to print data from variables without using foreach?

Sorry for my questions.

Best Regards,
#4

[eluser]Philo01[/eluser]
Not 100% if I'm correct, but I think:

Code:
&lt;?php

echo $result[0]['id_anunciante'];

?&gt;

Please note, this will only display the first result!
So if you have 5 parts in your array, you will only get part 1.
#5

[eluser]Sinclair[/eluser]
Hi,

Thanks for your reply.

But it is not working. I got the error:
Code:
Fatal error: Cannot use object of type stdClass as array in C:\...\detalhe_view.php on line 8

Any solution?

Best Regards,




Theme © iAndrew 2016 - Forum software by © MyBB