How to tab a PHP echo?

Asked

Viewed 1,730 times

1

I have the following code that generates a code of a insert automatic for me. My system is a kind of generator querys I’m just passing the parameters. I pass the parameters on a page and give the Submit to send the values to another page, which executes 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 that the table calls "user" and the columns are "use_user", "user_user" and "password_user"):

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 so:

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);
}

Look at the tabs which were added, how could I do that? I’ve tried using \t and show with n2lbr but it didn’t work.

1 answer

1


If you are going to display this query in the browser put it in the tag <pre> and use the \t to specify a tab in the string. You can specify if you want this formatting with an add parameter in the menu 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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.