How to return a specific variable from within a Function?

Asked

Viewed 186 times

3

In this code I gave a return $array and I’m using the data inside the foreach, so far so good.

I would also like to use the variable $url_controle inside the file I called the function.

Question: How can I print this variable there in the other file? I gave a return $url_controle but it didn’t seem to work.

function list_all_pages() {

    $url_controle = $_SERVER['REQUEST_URI'];
    $url_controle = explode('/', $url_controle);
    $url_controle = $url_controle[2];

    global $pdo;

    $sql    = "SELECT * FROM tb_paginas WHERE tipo = '$url_controle' ORDER BY ID DESC";
    $exc    = $pdo->query($sql);
    $cnt    = $exc->rowCount();
    $array  = $exc->fetchAll(PDO::FETCH_ASSOC);

    return $array;

}

################
EM OUTRO ARQUIVO
################

$pages = list_all_pages();

foreach ($pages as $valores) {
    echo $valores['ID'];
    echo $valores['titulo'];
}

2 answers

1

You can create a return array with the desired information, then your code would look like this:

    function list_all_pages() {

        $url_controle = $_SERVER['REQUEST_URI'];
        $url_controle = explode('/', $url_controle);
        $url_controle = $url_controle[2];

        global $pdo;

        $sql    = "SELECT * FROM tb_paginas WHERE tipo = '$url_controle' ORDER BY ID DESC";
        $exc    = $pdo->query($sql);
        $cnt    = $exc->rowCount();
        $array  = $exc->fetchAll(PDO::FETCH_ASSOC);

        $retorno = array("url_controle" => $url_controle, "pages" => $array);
        return $retorno;

}

IN ANOTHER ARCHIVE

$pages = list_all_pages();
echo $pages['url_controle']; //valor que vc deseja obter

foreach ($pages['pages'] as $valores) {
    echo $valores['ID'];
    echo $valores['titulo'];
}

-1

The whole secret of Solution to this Question is in the return who must send a array informing all variables that can be used.

In the example below, you will notice that I passed an associative array which is recovered in the file where the function is called.

function list_all_pages() {

    global $pdo;

    $url_controle = $_SERVER['REQUEST_URI'];
    $url_controle = explode('/', $url_controle);
    $url_controle = $url_controle[2];

    $sql    = "SELECT * FROM tb_paginas WHERE tipo = '$url_controle' ORDER BY ID DESC";
    $exc    = $pdo->query($sql);
    $cnt    = $exc->rowCount();
    $array  = $exc->fetchAll(PDO::FETCH_ASSOC);

    // Aqui eu monto um array associativo que envia várias informações
    // Neste caso estou enviando $array e $url_controle

    return array('array' => $array, 'url_controle' => $url_controle);

}

################
USO DA FUNCTION
################

// Aqui eu armazeno todo o conteúdo da function dentro da variável $conteudo
$conteudo     = list_all_pages();

// Aqui eu armazeno o $array (SQL) dentro da variável $dados
$dados        = $conteudo['array'];

// Aqui eu armazena uma variável de $controle que vem da function
$controle     = $conteudo['url_controle'];

Now that you already have the information stored in variables, just use it where you find it convenient according to the type of each one.

For example, $url_controle is a string while $array is a data array to use within a foreach for example.

Browser other questions tagged

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