Print an array with multiple select data, once

Asked

Viewed 440 times

0

I have a array which receives precisely 18 values. Each "key" of that array is a time and each value of this is an SQL query. I pass this array for a json file to bring the data objects I need. The problem is that this array prints multiple times and I just need it to print once with all selects.

.... action....

$selectDezenove = Doctrine::getTable('Atendimentos')->createQuery('a')
                                ->where('time(a.data_hora) =', $this->horarios ); 
    $this->horarios = array();
        $this->horarios["08:20"] = $selectDezenove;
        $this->horarios["08:40"] = $selectDezenove;
.....
.... json.php ...

 foreach($horarios as $horario => $atendimento)
{
    foreach($records as $atendimento)
    {
        if( date("H:i", strtotime($atendimento->getDataHora())) === $horario )
        {
         $list[] =
                '{'."\n".
                '   "id":'.  $atendimento->getId().','."\n".      
                ' "cell":'."\n". 
                '           ['."\n".
                    '                   "'.$atendimento->getId().'",'."\n". 
                    '                   "'.$horario.'",'."\n".
                         ++dados+++
        }
        else 
        {
            $list[] =
                '{'."\n".
                '   "id":'.$atendimento->getId().','."\n".      
                ' "cell":'."\n". 
                '           ['."\n".
                    '                   "",'."\n". 
                    '                   "'.$horario.'",'."\n". 
                   ++dados++
         }
         if($atendimento == NULL )
         {
           $list[] =
                    '{'."\n".
                    '   "id":'.$atendimento->getId().','."\n".      
                    ' "cell":'."\n". 
                    '           ['."\n".
                        '                   "",'."\n". 
                        '                   "'.$horario.'",'."\n".
                        +++dados+++
          }
        }
       }
      }
     ....... fim json .....

The problem is that my result is being printed many times, like for each select that return 1 result, is printing the list with all arrays, if I have 2 results will print 1 array 18 positions 2 times, if 3 results, 1 array with 18 positions 3 times ... and so on.

What could be wrong with that? I’m working with symfony framework 1.4, with ORM Doctrine 1.4

  • 2

    This is not directly related to your question, but consider the possibility to use json_encode instead of concatenating this bunch of strings - your code will become more efficient and more secure.

  • 1

    I wonder why not use json_encode

1 answer

1

What it prints is the information of the $Records array, what can be happening is that the $horarios array is larger than the $Records array and the same is inside the $horarios foreach. For example, the $horarios array has 2 positions, that is, its foreach will run twice. And your $horarios array that is inside that foreach only has one position and as it is inside the first foreach that loops twice it will repeat its result.

P.S.: I don’t know if I understand but I hope I helped!

Browser other questions tagged

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