Retrieve FOR loop variable inside a FOREACH - php

Asked

Viewed 611 times

0

Hi, I have two loops in the structure of my code. The first is a FOR that mounts an array with data. And I have a foreach that runs through a BD data.

I want to use the variable that returns in FOR and use in foreach, but it doesn’t seem to be rolling.

Can someone help me?

for($i=0 ; $i < $contador; $i++)
{
    $tbnewsletterNews = Doctrine_Core::getTable('tbnews')->createQuery('a')->select('a.*')->where('a.id = ?', $param['id_noticia'][$i])->execute();
    foreach($tbnewsletterNews as $news) 
    {
      if($news->getImagem() !== '')
      {
        $noticias[] = '<div class="col-md-9" style="display:block; width:670px; margin-left:15px;"> </div>';
      }else{
       $noticias[] =  '<div class="col-md-9" style="display:block; width:670px; margin-left:15px;"> </div>';
      }
    }
  $body .= '<div class="col-md-12 col-noticias">'. $noticias[$i] . '</div> ';
} 

    foreach ($usuarios as $usuario)
    {

     $corpo = '<img src="http://www.meusite.gov.br/images/informativoHeader.jpg" width="700" height="175" border="0" style="margin:0; margin-bottom:15px;"/><br><br>
                '.$body.'
                <img src="http://www.meusite.gov.br/images/rodape.jpg" border="0"/>'.$sair;
    }

I want to use is that variable $body there, but when saved in the bank its contents is empty :T

  • There is nothing wrong with the code, at least that you can see right away. It may be that there is something wrong with the data and the variable is empty. At least the problem does not seem to be what is described in the question. In the first block, the logic of adding elements to the array and then accessing a specific position is strange, this is exactly what I wanted?

  • First, how do you initialize the body variable? Because there in the code appears a snippet of html code being concatenated to it. Another thing: if you really want to save html in the database you need you need to encode it. Try using the method htmlspecialchars() to encode before saving to the database and the method htmlspecialchars_decode() to decode at a later date.

  • yes that’s well, this logic is already working in production, but now I have to record the data in the bank instead of sending an email (as it is working now). Now in production I can fetch the $body variable inside the foreach, but now it doesn’t work anymore, um.

  • 1

    So the problem lies at another point that is not posted.

  • @Stable if you give one print_r($body) after the is what returns?

  • the variable $body is started and set at the same time, just after the foreach of the go there.

  • 1

    The problem was that the variable $counter was commented and I didn’t even see it. Now it’s all right.

Show 2 more comments
No answers

Browser other questions tagged

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