Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP include file multiple times in one page

Tags:

include

php

I have a php-file called kal_test.php which gives a value to the variable $vbl. This variable is needed in the file called kal_generator.php which produces a table from that variable (I'll spare you the details). It goes like this:


[kal_test.php]

<?php
$vbl = "14/09/2011";
include ("kal_generator.php");
?>

[kal_test.php]

<?php
// Long code converts the $vbl into a 2-dimensional array called $output
// I'll spare you the details (it works fine by the way)
?>

<table>
  <tr><th>bla</th><th>blabla</th></tr>

<?php
foreach ($output as $v1) {
    echo "<tr>";
    foreach ($v1 as $v2) {
        echo "<td>$v2</td>";
    }
    echo "</tr>\n";
}
?>

</table>

This set-up works fine but I can't make two of those appear on the same page, like this:

[kal_test.php]

<?php
$vbl = "14/09/2011";
include ("kal_generator.php");
$vbl = "21/09/2011";
include ("kal_generator.php");
?>

This will give the following result:

//here comes the header

<table> // table created with $vbl = "14/09/2011"
  <tr><th>bla</th><th>blabla</th></tr>
  <tr><td>this</td><td>works</td></tr>
  <tr><td>this</td><td>works</td></tr>
</table>

//here should the second table be and also the rest of the page (footer), this is completely missing

What am I doing wrong? Thanks!

like image 974
gieldops Avatar asked Sep 13 '11 13:09

gieldops


People also ask

Can we include one PHP file multiple times?

If the file is included twice, PHP will raise a fatal error because the functions were already declared. It is recommended to use include_once instead of checking if the file was already included and conditionally return inside the included file.

HOW include multiple PHP file in HTML?

The include (or require ) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement. Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.

What is the difference between include and include_once in PHP?

The include() function is used to include a PHP file into another irrespective of whether the file is included before or not. The include_once() will first check whether a file is already included or not and if it is already included then it will not include it again.

What does Include_once mean in PHP?

The include_once keyword is used to embed PHP code from another file. If the file is not found, a warning is shown and the program continues to run. If the file was already included previously, this statement will not include it again.


1 Answers

You're likely defining a function or class in kal_generator.php. PHP aborts when you try to redefine such a function or class. Consider putting your code in a function, include that function once and then run the function instead of including a file.

kal_test.php

<?php
require_once 'kal_generator.php';
kal_generator("14/09/2011");
kal_generator("21/09/2011");
?>

kal_generator.php

<?php
function kal_generator($vbl) {
    /**
     * Here, you should be creating $output
     */
    echo <<EOF
<table>
  <tr><th>bla</th><th>blabla</th></tr>

EOF;
    foreach ($output as $v1) {
        echo "<tr>";
        foreach ($v1 as $v2) {
            echo "<td>$v2</td>";
        }
        echo "</tr>\n";
    }

    echo "</table>\n";
}
?>
like image 169
Lekensteyn Avatar answered Oct 17 '22 00:10

Lekensteyn