How to use require/include inside the repeat loop?

Asked

Viewed 43 times

1

What is the most appropriate way to return the dynamic content of a PHP script, loaded via include within a loop? Below are more details:

pagina1.php -> returns the name of the companies registered in a database

page.php2 -> returns the information of each company according to the ID passed to the page.

for  {
  $codigo_empresa = $registro['empresa_codigo'];
  $empresa = $registro['empresa_nome'];
  include pagina2.php
}

The problem is that the content returned from the page2.php is always the same, that is, it does include only once and during the loop, just repeats the value returned from the first record.

Detail: I need to return via include, because this page is used in other parts of the system.

I did the test with include and require and they all return the same result.

  • instead of using include pq does not use GET and passes the variable?

  • So I actually use include to search for an HTML code and several other data from each company, and so I use Loop to send messages in bulk. When I did the bulk shipping, it worked perfectly, and now that I’m trying to do the bulk shipping, I’m facing this detail..

1 answer

1


The problem is that the content returned from the page 2.php is always the same,

From a single include outside the loop, since the returned value is always the same.

So that you will redeclare the same thing multiple times with the same value?

So, I actually use include to search for an HTML code and several other data of each company, and so I use Loop to send messages in bulk.

I imagine by this comment, which is not exactly the same. You must be doing something like "Hello, John, Us of the ABCD company..." and in the other "Ola, João, Nos da empresa XPTO...."

If in page2.php you have a different result based on $codigo_company or $codigo, turn the page code2.php into a class and work with objects.

So it becomes much clearer and more logical what your code is doing.

Something like:

include pagina2.php
for {
  $codigo_empresa = $registro['empresa_codigo'];
  $emp = new Empresa($codigo_empresa);
  $empresa_nome = emp->getNome();
  $empresa_endereco = emp->getEnd();
  $ok = emp->enviaEmail('João','[email protected]');
  if ( $ok !== true ) {
     echo "Erro no envio do email da empresa:" . $empresa_nome;
  }
}

If you have more code details we can help better.

Browser other questions tagged

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