2

i have a problem. I write this function tu recover a previous branch from my DB entity, and i want that the function return a string with I can see this information. this is the code:

function stampaOggettoCollezione($tipoOggetto, $id, $connection, $percorso="")
{
    $i=0;

    switch($tipoOggetto)
    {
        case 'casaeditrice':
            return $percorso;
            break;

        case 'storia':
            $dati = array("storia", "IDCasaEditrice", "casaeditrice");
            break;

        case 'testata':
            $dati = array("testata", "IDStoria", "storia");
            break;

        case 'albo':
            $dati = array("albo", "IDTestata", "testata");
            break;
    }

            $sql = "SELECT * FROM ".$dati[0]." WHERE ID=$id";
            $result=mysqli_query($connection,$sql);
            $IDSuperiore=mysqli_fetch_array($result)[$dati[1]];

            $sql = "SELECT * FROM ".$dati[2]." WHERE ID=$IDSuperiore";
            $result = mysqli_query($connection, $sql);
            $nomeSuperiore=mysqli_fetch_array($result)['nome'];

            $percorso = $percorso . $nomeSuperiore;

            stampaOggettoCollezione($dati[2], $IDSuperiore, $connection, $percorso);
}

i'm sure that code works, i try some echo in the code to check the final path. My problem is that i can't write the return like a string in my webpage. What do you think about? there's some problem with the declaration? Thanks

6
  • 1
    What do you mean by "write the return like a string in my webpage"? It's not really clear what you're asking. Commented Dec 18, 2015 at 1:52
  • sorry, is my problem with english. I use that function in a webpage, to write a path. if i try to use $var=stampaOggettoCollezione(...); echo $var; i can't view an output. Commented Dec 18, 2015 at 1:54
  • 1
    That should work, assuming a value is returned. Currently only one case in the function actually returns a value. So unless that case is invoked, nothing will be returned. Commented Dec 18, 2015 at 1:55
  • Replace stampaOggettoCollezione($dati[2], $IDSuperiore, $connection, $percorso); } with return stampaOggettoCollezione($dati[2], $IDSuperiore, $connection, $percorso); } Commented Dec 18, 2015 at 2:03
  • thanks Terminus, now works. Why? :/ Commented Dec 18, 2015 at 2:07

2 Answers 2

1

I'm not sure I understand because the english is difficult to understand, but it seems that all you need to do is write:

return $result;

at the end of the function, or whichever string variable you want to return. Then use echo stampaOggettoCollezione(arguments...) after the function to have this value echoed.

Sign up to request clarification or add additional context in comments.

2 Comments

al the function, for the db structure, assuming the case 'casaeditrice' and go out. I don't understand why i don't have output. For the recursive function i must have the return only in the 'final' case, not at the end of function.
Then use return after recursive stampaOggettoCollezione(...) like return false; This means if nothing was returned it will be false. Otherwise return the value you want. What variable do you want to return as the 'final' case?
1

Replace

stampaOggettoCollezione($dati[2], $IDSuperiore, $connection, $percorso);

with

return stampaOggettoCollezione($dati[2], $IDSuperiore, $connection, $percorso);

You have to pass $percorso all the way back out of all the calls to stampaOggettoCollezione and back to the original call. Read more about recursion on the Internets.

Alternatively, I believe you could achieve the same effect if you change your function to build a query that joins on tables. It will remove the need for recursion, reduce the number of database calls, and should work without changing anything but the function itself.

// dynamically queries more tables based on what the inital table to query is
function stampaOggettoCollezione($tipoOggetto, $id, $connection, $percorso="")
{
    $selectQuery = "SELECT ";
    $columnsToSelect = "";
    $fromQuery = "";
    $whereClause = " WHERE ";

    switch($tipoOggetto) {
        case 'albo':
            $columnsToSelect .= ", a.nome AS aNome";
            $fromQuery = " LEFT JOIN albo a ON t.ID = a.IDTestata";
        case 'testata':
            $columnsToSelect .= ", t.nome AS tNome";
            $fromQuery = " LEFT JOIN testata t ON s.ID = t.IDStoria" . $fromQuery;
        case 'storia':
            $columnsToSelect .= ", s.nome AS sNome";
            $fromQuery = " LEFT JOIN storia s ON ce.ID = s.IDCasaEditrice" . $fromQuery;
        case 'casaeditrice':
            $columnsToSelect .= ", ce.nome AS ceNome";
            $fromQuery = " FROM casaeditrice ce" . $fromQuery;
            break;
    }
    // remove the first `, `
    $selectQuery .= substr($columnsToSelect, 2);

    switch($tipoOggetto)
    {
        case 'casaeditrice':
            $whereClause .= "c.ID = $id";
            break;
        case 'storia':
            $whereClause .= "s.ID = $id";
            break;
        case 'testata':
            $whereClause .= "t.ID = $id";
            break;
        case 'albo':
            $whereClause .= "a.ID = $id";
            break;
    }

    $qry = $selectQuery . $fromQuery . $whereClause;

    $result=mysqli_query($connection,$sql);
    $nomeSuperiore= "";
    $nomes = mysqli_fetch_array($result);
    // might take some work to get the output in the right order
    foreach($nomes as $nome)
        $nomeSuperiore .= $nome;

    return $nomeSuperiore;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.