-1

I got a simple PHP code like this one:

<div id="master">
    <div id="info">
        <?php include 'division.php';
        echo $winrate;

        ?>
    </div>

    <div id="lastmatches">
        <?php include 'lastmatches.php';?>
     </div>



 </div>

</body>

As you see i want to echo $winrate, but $winrate is a variable that comes from lastmatches.php. So it will never work. Some has got an idea to echo $winrate in the div info? Im stuck and i hope you guys can help me out!

Thanks in advance!

Chiel
  • 83
  • 10
  • 1
    `$winrate` cannot be shown when it is defined in the future. Include lastmatches.php before you echo it. –  Jul 06 '14 at 19:01
  • Duplicate of http://stackoverflow.com/questions/4675932/passing-a-variable-from-one-php-include-file-to-another-global-vs-not use `global $winrate;` – Issam Zoli Jul 06 '14 at 19:02
  • But if i include it earlier its an problem with the HTML and CSS? – Chiel Jul 06 '14 at 19:02
  • Only if you echo out stuff in lastmatches.php. –  Jul 06 '14 at 19:03
  • 1
    Structure your code properly so you can get the value of `$winrate` without it also outputting a bunch of HTML. If your problem is that the two are intertwined, you need to decouple those two things. – deceze Jul 06 '14 at 19:12

2 Answers2

1

You need to include lastmatches.php before to define $winrate. But if this file outputs some content then you will want to use the caching system to output the right content at the right place.

<div id="master">
    <div id="info">
        <?php include 'division.php';
        // begin cache
        ob_start();
        include 'lastmatches.php';
        // end cache
        $lastmatchescontent = ob_get_clean();
        echo $winrate;

        ?>
    </div>

    <div id="lastmatches">
        <?php echo $lastmatchescontent; ?>
     </div>



 </div>

</body>
Valentin Mercier
  • 5,256
  • 3
  • 26
  • 50
  • I'd call it output buffering rather than caching, because it's not preserving pages to save processing time. – Dave Chen Jul 06 '14 at 19:15
  • Indeed, it's just that the first time I have heard about this method it was called `caching` so it kind of stayed in my mind. But your term is more exact. – Valentin Mercier Jul 06 '14 at 19:19
0

i suggest you to follow MVC approach, if you do not want to use framework u can do something like this: https://github.com/LPodolski/basic_php_templating

this will allow code to be more readable, by separating concerns of generating output from getting data from db, parsing it etc

LPodolski
  • 2,888
  • 4
  • 21
  • 24