6

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!

3
  • 4
    Don't do this. Put a function in that include and call the function every time you need it. Commented Sep 13, 2011 at 13:57
  • I'd love to do that but the long code is (surprise!) really long Commented Sep 13, 2011 at 13:58
  • 1
    Who cares.... What is so hard about wrapping it up with the function keyword and a couple brackets? Perhaps you should consider breaking that function into a few functions while you're at it. Commented Sep 13, 2011 at 14:01

1 Answer 1

15

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";
}
?>
Sign up to request clarification or add additional context in comments.

6 Comments

Brad said the same, I'll try this but it will take a long time to rewrite the code. I should have thought about this earlier :(
@gieldl, what rewriting is there to do?
If it's that long, you've done something wrong. Break the code down in parts (functions, methods, whatever).
With the code we've been given in the example, then Brad is correct; there's virtually zero rewriting. However, I have had to work with badly written code in the past which worked very much as per the question, and it was very hard to rewrite so I know it can be painful. But I would still say you should do it. If the code is bad enough that it is hard to rewrite per this answer, then that is a sure sign that it needs to be rewritten!
by the way -- one other thing: if you're going to use this answer, you might want to use the include_once() function rather than include(). (it won't make any difference, but might save you from errors later if you accidentally try to include it twice)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.