Return a function

2

I created a teste() function that must be populated with While and return the data. When I call my function teste() within the function PageLoad_Arquivo() it does not load?

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

}
    
asked by anonymous 11.02.2015 / 20:26

2 answers

6

It loads yes, but you're finding that one function goes into another as a include , and it's not like this. A variable does not communicate from one function to another. You have to pass the value of it to the variable or return the value to the caller. So:

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

}

Note that although in both functions there is a variable named $resultadowhile they are completely different variables and that by chance has the same value. Just because the value of the existing variable in the PageLoad_Arquivo() function was passed to the variable in the teste() function. You can pass any value of any variable as a parameter name. 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 reciting a parameter called $resultadowhile . This parameter is a local variable of the teste() function, it only exists within this function.

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

This return must be stored in a variable and can be the same $resultadowhile that was passed as an argument.

Of course you can not pass any argument if it is your wish but the function would be less flexible and would be difficult to use in other situations if you already had an initial HTML text. And the use of the function is precisely to be able to reuse in several different situations. So you could remove the parameter and just return the value of the variable if you do not want this flexibility as shown in Oeslei's 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.

    
11.02.2015 / 20:47
6

The problem is not that your function does not run. It just does nothing but mount the table string and end 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

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

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

// ...
if(mysql_num_rows($result) > 0) {
    $resultadowhile = teste($result);
    
11.02.2015 / 20:45