tags:

views:

64

answers:

2

I have an Elseif statement, which gets a template name and includes the template PHP file which contains a large array, it outputs the result on the page.

$template = str_replace("-","_","{$_GET['select']}");
    if ($template == "cuatro"){
        include("templates/cuatro.php");
        echo $page_output;
    } elseif ($template == "ohlittl"){
        include("templates/ohlittl.php");
        echo $page_output;
    } else {
        echo "Sorry, template not found.";
    }

$page_output = "You've chosen $template_select[0].";

From there, I get a notice saying it couldn't find the $page_output variable.

Notice: Undefined variable: page_output in C:\ ... \template.php on line 10

It can find it if I put the variable in the included file though. But I'm trying to get this variable to remain on this page. How do I complete this?

+3  A: 

You are defining $page_output after you are echoing it. At the time you call echo $page_output it doesn't exist yet.

Try:

$page_output = "You've chosen {$template_select[0]}.";
$template = str_replace("-","_","{$_GET['select']}");
if ($template == "cuatro"){
    include("templates/cuatro.php");
    echo $page_output;
} elseif ($template == "ohlittl"){
    include(dirname(__FILE__) . "/templates/ohlittl.php");
    echo $page_output;
} else {
    echo "Sorry, template not found.";
}

Although I have no idea how you are setting $template_select and if you are aware it will always say the same template name?

An alternative approach that I believe achieves what you want:

$templates = array('cuatro', 'ohlittl');
$selectedTemplate = strtolower(str_replace("-","_",$_GET['select']));

foreach ($templates as $template)
{
    if ($template === $selectedTemplate) {
       include(dirname(__FILE__) . "/templates/" . $template . ".php");
       echo "You've chosen {$template}.";
    }
}
Graphain
After the include, I need to output some code with variables from the included chosen file. When I try, I receive undefined variable warnings, so pretty much the included file is not included. Is there a fix for that? This is pretty much the first way, and $template_select is a variable defined in the included file, so no, it will not always say the same template name. That's what I'm trying to solve.
Benny
if you replcae echo "You've chosen {$template}" with echo $aVarOnTheIncludedPage is that where you get the issue?
Graphain
Yes, then I get the warning errors.
Benny
Are you sure that every template file includes the variable you want to print out?
Graphain
Yes, I've checked multiple times.
Benny
Try the updated include use (i.e. with the dirname() bit)
Graphain
Thank you, Graphain.
Benny
Glad to hear it worked
Graphain
A: 

Either your template

  1. Outputs the text directly (echo)
  2. Stores the result in a global (e.g. $page_output) or a local variable (in the include happens inside a function, but that's transparent for the template).
  3. Returns the output (yes, includes can return values).

You seem to want option 2, yet your templates are not defining any $page_output variable. You could also output the text directly in the templates, buffer the output, and assign it to $page_output:

ob_start();
include "file.php.inc";
$page_output = ob_get_contents();
ob_end_clean();
Artefacto