JU1CY
JU1CY

Reputation: 123

PHP include html by option

I need to include html, not from file, just simple code. I have main content included by:

<?php
$content = array(
    '001'=>'content/001_001.php',
    '002'=>'content/001_002.php',
    '003'=>'content/001_003.php'
);
if(in_array($_GET['show'], array_keys($content))) {
    include($content[$_GET['show']]);
} else {
    include('content/001_001.php');
}
?>

And on a side i'd like to include simple html, but that's more like 3 buttons in each category, so cloning lots of *.html & *.php files won't be right and clean work.

In case of opened page: ?show=001, on a side would be added <div>001</div>; ?show=002, on a side would be added <div>002</div> and etc.

Upvotes: 0

Views: 107

Answers (1)

Jesse Buitenhuis
Jesse Buitenhuis

Reputation: 26

If I understand your question correctly, you want to load HTML code from an array rather than from a file. You can do this by changing your include to echo.

<?php
    $content = array(
        '001'=>'<div><a href="#">001</a></div>',
        '002'=>'<div>002</div>',
        '003'=>'<div>003</div>'
    );
    if(!empty($_GET['show']) && isset($content[$_GET['show']])) {
        echo $content[$_GET['show']];
    } else {
        echo $content['001'];
    }
?>

Upvotes: 1

Related Questions