Welcome Guest, Not a member yet? Register   Sign In
I can not pass array loop for view
#1

I can not pass array loop for view.
 
each result has one or more positions, however pass in view one position.
I used, example:
$data['atualiza'] = $totaliza;

$this->load->view(myView, $data);
this no work, only the first array value;





foreach ($vetor as $xvet => $valuevetor)
    {
            
                

    
    //TOTALIZA
    foreach ($complementar->result() as $key => $value)
    {
    $totaliza = $this->db->query("select professor,   sum(cargah) as soma from cmcg_registros where id_disciplina = '$idsic' and id_professor =". $valuevetor."group by professor")->result();
    }
    

    

    foreach ($complementar->result() as $key => $value)
    {
    for ($i=0; $i < $key ; $i++)
    {
    $consulta = $this->db->query("select professor, id_disciplina, cargah, cargah_local from cmcg_registros where id_disciplina = '$idsic' and id_professor =". $valuevetor."group by professor, id_disciplina, cargah, cargah_local")->result();
    }
    }

    echo 'TOTALIZA';

    echo '<pre>';
    print_r($totaliza);
    echo '</pre>';
    echo '<br><hr>';
    echo '<pre>';
    print_r($consulta);
    echo '</pre>';

    }



show

TOTALIZA
Array
(
   [0] => stdClass Object
       (
           [professor] => LUIZ AUGUSTO DA SILVA OLIVEIRA
           [soma] => 90
       )

)



Array
(
   [0] => stdClass Object
       (
           [professor] => LUIZ AUGUSTO DA SILVA OLIVEIRA
           [id_disciplina] => 19
           [cargah] => 5
           [cargah_local] => 701
       )

   [1] => stdClass Object
       (
           [professor] => LUIZ AUGUSTO DA SILVA OLIVEIRA
           [id_disciplina] => 19
           [cargah] => 5
           [cargah_local] => 702
       )

   [2] => stdClass Object
       (
           [professor] => LUIZ AUGUSTO DA SILVA OLIVEIRA
           [id_disciplina] => 19
           [cargah] => 5
           [cargah_local] => 703
       )

)

Thanks guys!
Reply
#2

You have loop inside a loop. $consulta will ever have the last value of the last query.
PHP Code:
foreach ($complementar->result() as $key => $value) {
    for (
$i=0$i $key $i++) { 
        
$consulta $this->db->query(...)->result();
    }


As simple fix you can keep most of the code logic, but add results to existing persistent array:
PHP Code:
$consulta = [];
foreach (
$vetor as $xvet => $valuevetor) {
    
// ...
    
foreach ($complementar->result() as $key => $value) {
        for (
$i=0$i $key $i++) { 
            
$consulta array_merge($consulta$this->db->query(...)->result());
        }
    }


I would suggest finding a way to get all the data back in a single query tho, if possible, putting queries in couple of nesting loops is very time-consuming.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB