Show echo within a variable in php

1

Hi, I'm doing a web page in php but I'm using a template that includes it to the php and the page I modify it in php only that sometimes I need to resort to html, for that I show the content from the template. I do not know if it is the best way to make a web page I do not have much experience in PHP. Well, what I'm trying to do is that the echo be shown in content but I did not get anything new. Code:

Php code where I design the different page to the template:

<?php

require_once 'database.php';
$database_connection = database_connect();

$title='hola';

$content='<div>';

//user input
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
*** Lo muestra fuera del template ****
echo $perPage = isset($_GET['per-page']) && $_GET['per-page'] <= 50 ? (int)$_GET['per-page'] : 5;

$content .= '</div>';


include 'Template.php';
?>

Template php code:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title><?php echo $title; ?></title>
        <link rel="stylesheet" type="text/css" href="Styles/Stylesheet.css" />
    </head>
    <body>
        <div id="wrapper">
            <div id="banner">             
            </div>

            <nav id="navigation">
                <ul id="nav">
                    <li><a href="index.php">Home</a></li>
                    <li><a href="tool.php">Coffee</a></li>
                    <li><a href="manager.php">Shop</a></li>
                    <li><a href="#">About</a></li>
                </ul>
            </nav>

            <div id="content_area">
                <?php echo $content; ?>
            </div>

            <div id="sidebar">

            </div>

            <footer>
                <p>All rights reserved</p>
            </footer>
        </div>
    </body>
</html>

Any suggestions are welcome. Thanks for your help.

    
asked by Perl 13.09.2016 в 19:56
source

3 answers

1

You will not have to do an echo, since there are different pages and it does not recognize the variable, you should put in this case, on the page where you have the html put a include("ruta-al-fichero-que-quieres-añadir"); or require_once("lo-mismo")

Include ()

Require_once ()

I recommend that you read the functions to be more informed:)

Then you will recognize the variables of the other file.

    
answered by 13.09.2016 в 20:02
1

I recommend you use a function that replaces the content of the html and after having processed the HTML file you print it with the echo $ html;

Two functions are used, the first loads the html template:

function carga_plantilla($nombre=''){
    $ruta='./'.$nombre.'.html';
    return file_get_contents($ruta);
}

The second function replaces the content of HTML with variables:

function reemplazo_dinamico($html='', $valor='', $static=''){
    $html = str_replace('{'.$static.'}', $valor, $html);
    return $html;
}

Your PHP file would look like this:

    <?php

        /* CARGA EL HTML */
        function carga_plantilla($nombre=''){
            $ruta='./'.$nombre.'.html';
            return file_get_contents($ruta);
        }
        /* REEMPLAZA LO QUE SE ENCUENTRA ENTRE CORCHETES */
        function reemplazo_dinamico($html='', $valor='', $static=''){
            $html = str_replace('{'.$static.'}', $valor, $html);
            return $html;
        }

        $title='hola';

        $content='<div>PRUEBAS</div>';

        /* FUNCION QUE CARGA LA PLANTILLA TEMPLATE */
        $html = carga_plantilla('template');
        /* FUNCION QUE REEMPLAZA {title} por el contenido de la variable $title*/
        $html = reemplazo_dinamico($html,$title,'title');
        /* FUNCION QUE REEMPLAZA {content} por el contenido de la variable $content*/
        $html = reemplazo_dinamico($html,$content,'content');
        echo $html;
        ?>

And your HTML file:

<!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>{title}</title>
            <link rel="stylesheet" type="text/css" href="Styles/Stylesheet.css" />
        </head>
        <body>
            <div id="wrapper">
                <div id="banner">             
                </div>

                <nav id="navigation">
                    <ul id="nav">
                        <li><a href="index.php">Home</a></li>
                        <li><a href="tool.php">Coffee</a></li>
                        <li><a href="manager.php">Shop</a></li>
                        <li><a href="#">About</a></li>
                    </ul>
                </nav>

                <div id="content_area">
                    {content}
                </div>

                <div id="sidebar">

                </div>

                <footer>
                    <p>All rights reserved</p>
                </footer>
            </div>
        </body>
    </html>

In the HTML template you can add {key_names} which you then replace with $ key_variables, in this case {title} {content}. This method is the current basis of many frameworks

    
answered by 14.09.2016 в 18:51
0

The problem is that you are doing an echo, you must fill the variable $ content with the data that you want to show inside the template, since you are printing the variable cotent in the template.

echo $perPage = isset($_GET['per-page']) && $_GET['per-page'] <= 50 ? (int)$_GET['per-page'] : 5;

You should rather fill in the variable, doing something like:

$content= isset($_GET['per-page']) && $_GET['per-page'] <= 50 ? (int)$_GET['per-page'] : 5;

Greetings, I hope it helps you.

    
answered by 13.09.2016 в 22:18