How to give a tab in an echo of PHP?

1

I have the following code that generates one the% auto_codes for me. My system is a kind of insert generator already ready, just step the parameters. I pass the parameters on a page and give the submit to send the values to another page, which performs the function and shows the codes already ready.

if($gM)
{
    if($gI)
    {
        foreach ($varUpper as $key) 
        {
            if(!isset($string))
            {
                $string = "'\".\$this->get$key().\"', <br>";
            }
            else
            {
               $string .= "'\".\$this->get$key().\"', <br>"; 
            }

            $resultado = substr($string, 0, -6);
        }

        echo 'protected function insert()<br>{<br>$query = "<br>INSERT INTO '.$tabela.'<br> (<br>'.implode(',<br> ', $colunas).'<br>) <br>VALUES <br>(<br>'.$resultado.'<br>)";';

        echo '<br><br>';

        echo 'return Dao::getInstancia()->execute($query);<br>}';
    }
}

I have this return (assuming the table is called "user" and the columns are "user_name", "user_user" and "user_password"):

protected function insert()
{
$query = "
INSERT INTO usuario
(
nome_usuario,
user_usuario,
senha_usuario
)
VALUES
(
'".$this->getNome_Usuario()."',
'".$this->getUser_Usuario()."',
'".$this->getSenha_Usuario()."'
)";

return Dao::getInstancia()->execute($query);
}

However, I would like the return to be like this:

protected function insert()
{
    $query = "
        INSERT INTO usuario
        (
            nome_usuario,
            user_usuario,
            senha_usuario
        )
        VALUES
        (
            '".$this->getNome_Usuario()."',
            '".$this->getUser_Usuario()."',
            '".$this->getSenha_Usuario()."'
        )";

    return Dao::getInstancia()->execute($query);
}

Notice the querys that were added, how could I do this? I've already tried using tabs and show with \t but it did not work.

    
asked by anonymous 01.12.2017 / 15:48

1 answer

1

If this query is to be displayed in the browser, put it between the <pre> tag and use \t to indicate a tab in the string. You can indicate whether you want this formatting with a parameter added in the metadata call.

protected function insert($debug=false)
{

    $debug = $debug ? array('<pre>', '</pre>') : array('','');

    $query = sprintf("%s
        INSERT INTO usuario
        (
            nome_usuario,
            user_usuario,
            senha_usuario
        )
        VALUES
        (
            '".$this->getNome_Usuario()."',
            '".$this->getUser_Usuario()."',
            '".$this->getSenha_Usuario()."'
        )%s",  ...$debug);

    return Dao::getInstancia()->execute($query);
}

Simplified example:

$t = true;
$tag = $t ? array('<pre>', '</pre>') : array('', '');
printf('%s >>texto<< %s', ...$tag);
    
01.12.2017 / 15:51