3

I want to use a variable inside an HTML-String of another PHP-File template.php in my PHP-File constructor.php.

I´m searched on Stackoverflow for a workaround to include the content of the other PHP-File. I included the following code into constructor.php because its known to be more safe instead of using file_get_contents(); Source:

function requireToVar($file){
    ob_start();
    require($file);
    return ob_get_clean();
}

The rest of constructor.php looks like this:

...
    $sqli = mysqli_query($mysqli, "SELECT ...");
    if(mysqli_num_rows($sqli) > 0){
        $ii = 0;
        while ($row = $sqli->fetch_assoc()) {
            $ii++;
            if($row['dummy']=="Example"){
                $content.=requireToVar('template.php');
...

The template.php looks like this:

<?php echo "
   <div class='image-wrapper' id='dt-".$row['id']."' style='display: none;'>
   ...
   </div>
"; ?>

The constructor.php doesn´t recognize the var $row['id'] inside the string of template.php as its own variable and also doesn´t execute it. The variable definitely works for the other code in constructor.php.

If I´m copy&paste the code of template.php into constructor.php after $content.= its working like a charm. But I want to restructure my constructor.php because its getting to big and this way its easier to customize.

I don´t know how to describe this problem more exactly, but I´m hoping this title fits to my problem.

Community
  • 1
  • 1
Pandora
  • 181
  • 2
  • 14
  • have you tried to add this $row variable to the function requireToVar() as another argument? – Zgr3doo May 19 '15 at 12:27
  • This way is less dynamic because I have to customize the function for every additional variable I´m adding to the string in `template.php`. – Pandora May 19 '15 at 12:39
  • yes but you can change $row for every record using it just like that $content.=requireToVar('template.php', $row); $row doesn't exist inside requireToVar function you need to pass it there – Zgr3doo May 19 '15 at 12:41
  • Why are you using `ob_start();` and `ob_get_clean();`? Why don't you do a regular `require()` and nothing else? – Novocaine May 19 '15 at 12:47
  • @Novocaine Because this doesn´t work for me. If you write `$content.=require('template.php');` the `constructor.php` won´t get the content of the `template.php`. I included a link to the Stackoverflow-Thread where I got this snippet from into my question. – Pandora May 19 '15 at 13:15
  • @Zgr3doo Also doesn´t work for me. The only way I can think of is to replace a variable inside the string but this isn´t dynamic too. Another idea or an more specific example? I´m relatively new to PHP and maybe don´t understand everything as other more skilled peoples do. – Pandora May 19 '15 at 13:41
  • I think @Zgr3doo 's idea should work. If you need to pass in more variables, you could make it an array. That way you never need to modify the function to handle more/less variables, only the template file. – Novocaine May 19 '15 at 14:15
  • Thanks @Zgr3doo & @Novocaine, I think you two are right. You misunderstood what I called dynamic. Say theres variables `$row['id']`, `$row['name']`, `$row['example']` and so on. I have to define additional variables for every "placeholder". Currently I´m customized it to `function requireToVar($file, $rowa){` and changed the var in `template.php` to `$rowa`. – Pandora May 19 '15 at 14:40
  • @Zgr3doo: Please create an answer and I will mark it as top answer. I think your approach is the best and easiest way. Theres no more echo needed and its easy to edit (correct markup-detection in Notepad++). Please include in your example solution ` – Pandora May 19 '15 at 14:51

3 Answers3

3

Update your function

function requireToVar($file,$row_id){
    ob_start();
    require($file);
    return ob_get_clean();
}

so you can call it like that

while ($row = $sqli->fetch_assoc()) {
        $ii++;
        if($row['dummy']=="Example"){
            $content.=requireToVar('template.php',$row['id']);

and display it in template.php like that

<div class="image-wrapper" id="dt-<?php echo $row_id; ?>" style="display: none;"></div>
Zgr3doo
  • 1,695
  • 17
  • 20
0

Use MVC model and renderer functions. For example: index.php

<!DOCTYPE HTML>
<html>
    <head>
        <title><?=$title; ?></title>
    </head>
    <body>
        <h3><?=$text; ?></h3>
    </body>
</html>

Than I'll have PHP array that contains those variables:

$array = array("title"=>"Mypage", "text"=>"Mytext");

Now we will use both in renderer function

function renderer($path, $array)
{
    extract($array); // extract function turn keys into variables
    include_once("$path.php");
} 
renderer("index", $array);
DDeme
  • 1,231
  • 2
  • 12
  • 17
  • Doesn´t work for me. Can you be more specific or describe it with my example code snippets? – Pandora May 19 '15 at 13:30
  • Create 2 files in project folder. index.php and run.php 1) put html in index.php 2) put array declaration, function declaration and function call in run.php 3) run from xampp maybe or wamp or lamp -> localhost/project/run.php Write here error message if it doesn't run. If it is ok, than use this code to create your own implementations of it. – DDeme May 19 '15 at 13:52
  • That destroys the idea of "restructuring" and using the template as a single file. Its more complicated than using the plain code of the `template.php` inside of the `constructor.php`. Thank you, but this is not what I´m looking for. – Pandora May 19 '15 at 13:58
0

Your approach is rather strange, but anyway; you can access $row from within $GLOBALS array.

Write your template as:

<?php echo "
   <div class='image-wrapper' id='dt-".$GLOBALS['row']['id']."' style='display: none;'>
   ...
   </div>
"; 
?>
  • Can you tell me why this is strange? I´m using a static template for one link, lets call it landing-page. The other pages are dynamic and don´t need a static template. Using $GLOBALS doesn´t sounds like a good deal to me. – Pandora May 19 '15 at 12:42
  • Well, what DDeme has suggested seems better. But anyway, guess my solution works for you, right? – user3414333 May 20 '15 at 07:12
  • No, DDeme´s example doesn´t work for me. I have to use `ob_start();` and `ob_get_clean();` for this to work. This way I can use the function `requireToVar` with additional parameters also and I can use my template without a problem insted of writing some content into index.php. Please read the answers under my question. Zgr3doo´s suggestion is the best for me so far. – Pandora May 20 '15 at 09:43
  • I still don't understand. So my solution to use `$GLOBALS` array in your template.php to access the `$row` variable is OK or not? – user3414333 May 20 '15 at 13:05
  • After I´m reading a lil bit I think your approach isn´t as bad as I thought before. Good discussion about this topic [here](http://stackoverflow.com/questions/1557787/are-global-variables-in-php-considered-bad-practice-if-so-why). Anyway in my case its even better to use session-variables instead of global-variables to bind it to the current user. But I don´t want to use cookies and the marked top answer is the best way without globals and without cookies. Your answer is the easiest but maybe not the "best" way. – Pandora May 21 '15 at 21:16