Reputation: 587
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
Upvotes: 2
Views: 112
Reputation:
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; }
Upvotes: 1
Reputation: 2422
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.
Upvotes: 1