How do I take information from a website and distribute it in a view?

Asked

Viewed 40 times

1

I made a function that receives data from the database as parameters passed by $_GET[] ... How can I use this information elsewhere in 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 rescue what is inside $dice out of function, does not work:

echo $dados['titulo'];

I’m trying to fill the view php treatments. with the data returned from this function without success. As I am learning, I still do not know how it works. An elegant way to do this is to put all the html inside the view but I think it’s unnecessary or so it works?

1 answer

2


Just return with the return

That line

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

trade her in for it

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

To receive function output

$dados = db_tratcontent();

Then you can do:

echo $dados['titulo'];

Observing:

There are many conceptual and also logical errors. But I prefer not to comment to avoid complicating such a simple answer.

UPDATE

Isset()

In this passage $slug = friendly_url(($_GET['slug_trat']));, check before if the index slug_trat exists and validates 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 end, if they exist. But you must validate according to your business model. If you are receiving number, type and validate as number, if it is email, type and validate as email, etc.

That stretch $slug = friendly_url(($_GET['slug_trat']));, can trade 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 (($_GET['slug_trat'])). In the above example, I removed the excess.

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

Global

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

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 you need to invoke by reference to have "global access".

Got confused with this last paragraph? rsrs

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

Remember that overall has nothing to do with the pre global scope variables defined by PHP as $_SERVER, $_GET, $_POST, among others.

There is also another conversation, as complex as what we discussed here.

header Location

The use of redirection within the function is something quite grotesque.

Try to understand how to organize code in layers, where each layer has its own well-defined responsibility. The basic model is called MVC. Search for MVC.

Code patterns

Nomenclature is important and the style to be used 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 tratcontent be? Content handling?

Even so it seems meaningless and also mixes two languages.

Maintain a standard with an internationalized language, in this case it is English.

To write better code, search by code patterns.

Start with the PHP-FIG organ, http://www.php-fig.org/

  • I would like to know about these mistakes to increase my learning and knowledge thus allowing to evolve in learning even if it was only in the comment ... Grateful.

  • 1

    added in reply. I posted in a very informal and fast way, because I do not have time. I hope it will be useful.

Browser other questions tagged

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