Return a function

Asked

Viewed 154 times

5

I created a function teste() which must be completed with the While and return the data. Why when I call my function teste() within the function PageLoad_Arquivo() it does not carry?

function teste(){

    $resultadowhile = "<table class='table table-hover'><thead><tr><th>Cod.</th><Data</th><th>Última Atualização</th><th>Unidade</th><th>Departamento</th><th>Arquivo</th><th>Status</th></tr>";
    while($row = mysql_fetch_array($result)){
        $resultadowhile .= "<tr>" . 
            '<td>s<a href="cadastro_arquivo.php?id=' . $row['id_arquivo'] . '">' . $row['id_arquivo'] . '</a></td>' . 
            '<td>' . formatarDataBRASIL($row['txt_data']) . '</td>' . 
            '<td>' . $row['txt_unidade'] . '</td>' . 
            '<td>' . $row['txt_departamento'] . '</td>' . 
            '<td>' . $row['txt_arquivo'] . '</td>' . 
            '<td>' . $row['flag_status'] . '</td>' . 
        "</tr>";
    }
    $resultadowhile .= "</table>";

}

function PageLoad_Arquivo(){

    $result = mysql_query("
    select a.id_arquivo, a.txt_arquivo, a.txt_data, a.txt_caminho, d.id_departamento, d.txt_departamento, u.id_unidade, u.txt_unidade, a.flag_status from tb_arquivo a
    left join tb_departamento d on a.id_departamento = d.id_departamento
    left join tb_unidade u on a.id_unidade = u.id_unidade
    ORDER BY a.txt_data DESC
    ") or die(mysql_error());

    $relacao = "style='display:block'";
    $cadastro = "style='display:none'";
    $resultadowhile = "";

    if(mysql_num_rows($result) > 0){
        teste();
        renderForm('', '', '', '', '', '', '', '', $relacao, $cadastro, $resultadowhile);
    }else{
        $resultadowhile = "<div class=\"alert alert-warning\" role=\"alert\">Sem resultados.</div>";
        renderForm('', '', '', '', '', '', '', '', $relacao, $cadastro, $resultadowhile);
    }

}

2 answers

8

The problem is not that its function does not run. It just does nothing but mount the table string and end of the story. You need to return the string in order to receive the value.

function teste() {
    // ...
    return $resultadowhile;
}

// ...
if(mysql_num_rows($result) > 0) {
    $resultadowhile = teste();

EDIT

Complementing, the same goes for your variable $result used in the function teste. This variable does not exist. You need to pass it as parameter.

function teste($result) {
    // ...
}

// ...
if(mysql_num_rows($result) > 0) {
    $resultadowhile = teste($result);
  • Tks... was trying to do as you indicated and was giving error... actually I was doing different from your recommendation, now funfou.

8


It does, but you’re thinking that one function goes inside another as one include, and it’s not like that. A variable does not communicate from one function to another. You have to pass its value to the variable or return the value to the caller. Like this:

function teste($resultadowhile) {
    $resultadowhile .= "<table class='table table-hover'><thead><tr><th>Cod.</th><Data</th><th>Última Atualização</th><th>Unidade</th><th>Departamento</th><th>Arquivo</th><th>Status</th></tr>";
    while($row = mysql_fetch_array($result)){
        $resultadowhile .= "<tr>" . 
            '<td>s<a href="cadastro_arquivo.php?id=' . $row['id_arquivo'] . '">' . $row['id_arquivo'] . '</a></td>' . 
            '<td>' . formatarDataBRASIL($row['txt_data']) . '</td>' . 
            '<td>' . $row['txt_unidade'] . '</td>' . 
            '<td>' . $row['txt_departamento'] . '</td>' . 
            '<td>' . $row['txt_arquivo'] . '</td>' . 
            '<td>' . $row['flag_status'] . '</td>' . 
        "</tr>";
    }
    $resultadowhile .= "</table>";
    return $resultadowhile;
}

function PageLoad_Arquivo() {
    $result = mysql_query("
    select a.id_arquivo, a.txt_arquivo, a.txt_data, a.txt_caminho, d.id_departamento, d.txt_departamento, u.id_unidade, u.txt_unidade, a.flag_status from tb_arquivo a
    left join tb_departamento d on a.id_departamento = d.id_departamento
    left join tb_unidade u on a.id_unidade = u.id_unidade
    ORDER BY a.txt_data DESC
    ") or die(mysql_error());
    $relacao = "style='display:block'";
    $cadastro = "style='display:none'";
    $resultadowhile = "";
    if(mysql_num_rows($result) > 0){
        $resultadowhile = teste($resultadowhile);
        renderForm('', '', '', '', '', '', '', '', $relacao, $cadastro, $resultadowhile);
    }else{
        $resultadowhile = "<div class=\"alert alert-warning\" role=\"alert\">Sem resultados.</div>";
        renderForm('', '', '', '', '', '', '', '', $relacao, $cadastro, $resultadowhile);
    }
}

I put in the Github for future reference.

Note that in both functions there is a variable with name $resultadowhile they are completely different variables and they happen to have the same value. Just because the value of the existing variable in the function PageLoad_Arquivo() was passed to the variable in the function teste(). You can pass any value of any variable as a name as a parameter. You can pass a literal. Of course ideally in this case it should be something like string, otherwise it may not have the desired result.

Calling teste($resultadowhile) you are passing the value of this variable as argument.

Declaring the function as function teste($resultadowhile) you are receiving a parameter called $resultadowhile. This parameter is a local variable of the function teste(), it exists only within this function.

Then in order for you to pass the manipulated value of the variable to the function that called you need to return it through the command return as seen in the last line of the modified function I posted.

This return needs to be stored in a variable and can be the same $resultadowhile which was passed as argument.

Of course it is possible not to pass any argument if it is your wish but the function would be less flexible and complicated to use in other situations you already have an initial HTML text. And the use of the function is to be able to reuse in several different situations. Then you could remove the parameter and only return the value of the variable if you do not want this flexibility as shown in the Oeslei response.

There is another way to pass the value of the variable through references but in this case it is not necessary and is a somewhat advanced topic.

Browser other questions tagged

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