How do I get information from a function and distribute it in a view?

1

I have done a function that receives data from the database according to parameters passed by $_GET[] ... How can I use this information in another part of my system?

function db_tratcontent(){

    $slug = friendly_url(($_GET['slug_trat']));
    $tipo = 'tratamentos';

    global $pdo;

    $sql = "SELECT * FROM tb_paginas WHERE tipo = '$tipo' AND url_amigavel = '$slug'";
    $exc = $pdo->query($sql);
    $cnt = $exc->rowCount();
    $hpg = url_site().'home';

    if($cnt == 1){ $dados = $exc->fetch(); } else { exit(header("Location: $hpg")); }

}

If I put the code below to try to redeem what's inside the $ data

echo $dados['titulo'];

I'm trying to populate the treatments.php view with the data returned from this function to no avail. As I am learning, I still do not know how it works. A non-elegant way to do this is to put all the html within the view but I think this is unnecessary or does it really work?

    
asked by anonymous 15.11.2015 / 23:24

1 answer

2

Just return with return

This line

if($cnt == 1){ $dados = $exc->fetch(); } else { exit(header("Location: $hpg")); }

Change it so

if ($cnt == 1)
    return $exc->fetch();
else
    exit(header("Location: $hpg"));

To receive the result of the function

$dados = db_tratcontent();

Then you can do:

echo $dados['titulo'];

Note:

There are many conceptual and logical errors. But I'd rather not comment to avoid complicating such a simple answer.

UPDATE

Isset ()

In this% wrapper%, check first that the% wrapper index exists and validate it with filters and the validations you need for the data you expect to receive.

In the example below I will show only a filter with trim (), which removes spaces at the beginning and at the end, if they exist. But you must validate according to your business model. If you are receiving a number, filter and validate it as a number, if it is an email, filter and validate it as an email, etc.

This excerpt $slug = friendly_url(($_GET['slug_trat'])); , can change for something like this:

$slug_trat = null;

if (isset($_GET['slug_trat']))
   $slug_trat = trim($_GET['slug_trat']);

if (empty($_GET['slug_trat']))
   retun null;

$slug = friendly_url($slug_trat);

Note that you are using unnecessary parentheses slug_trat . In the example above, I removed the excess.

Still, there are conceptual errors even in this patch script I'm recommending. I just tried to be as simple as possible. But to explain concepts, design pattern, etc., is very complex and really is not feasible to explain in this post.

Global

Another point that is not wrong, but it is recommended to avoid using it, it is the "global" feature.

This also involves conceptual issues, but I will briefly explain the technical use.

The use of global, makes an object accessible from anywhere that is invoked, except in specific cases of anonymous functions with callback where it needs to invoke by reference to have "global access".

Are you confused by this last paragraph? rsrs

This is just the beginning but, in short, the use of global causes you to lose the accessibility control of an object. Such an object can be used anywhere indiscriminately, making the code difficult to debug when a debug or other various situations are required.

Note that global has nothing to do with pre-defined global variables defined by PHP as $ _SERVER, $ _GET, $ _POST, among others.

That's another conversation as complex as the one we discussed here.

header location

The use of redirection within the function is somewhat grotesque.

Try to understand how to organize the layered code, where each layer has a clear responsibility. The basic model is called the MVC. Search for MVC.

Code defaults

Naming is important and the style to use depends on the code pattern you want to apply.

Basically, avoid functions with strange names like "db_tratcontent". Names should be intuitive and as short as possible.

As an example, db_tratcontent, we can understand that db is database. But what would be tratcontent? Content handling?

Even though it seems meaningless and also blends two languages.

Keep a pattern with an internationalized language, in which case it is English.

To write better codes, search for code patterns.

Start with the PHP-FIG body, link

    
15.11.2015 / 23:45