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!
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.
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.
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.
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.
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.
<?php
require_once 'kal_generator.php';
kal_generator("14/09/2011");
kal_generator("21/09/2011");
?>
<?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";
}
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With