xKushGene xKushGene - 5 months ago 8
SQL Question

Function in echo why does it bugs?

I read some topics here and in other forums and find a way to set a function in a echo.
But it doesnt show my stuff.
My Code

echo "<tr>
<td>
Choose people:<br>
",choose_kunde(),"
</td>
</tr>";


my function:

function choose_kunde()
{
require './config/config.inc.php';
if($result2 = mysqli_query($db, "SELECT kunden_id, vname, nname FROM kunden"))
{
echo "<select name='kunde_a'>";
while($adr = mysqli_fetch_assoc($result2))
{
echo "<option value='".$adr['vname']."|".$adr['nname']."|".$adr['kunden_id']."'>".$adr['vname']." ".$adr['nname']."</option>";
}
echo "</select>";
}
else
{
echo "Daten konnten nicht aus der Datenbank gelesen werden.<br>";
echo mysqli_error($db);
}
}

Answer

You need to change your function to return string instead of echoing it.

example:

function choose_kunde()
{
    $output = '';
    require './config/config.inc.php';

    if($result2 = mysqli_query($db, "SELECT kunden_id, vname, nname FROM kunden"))
    {
        $output .= "<select name='kunde_a'>";

        while($adr = mysqli_fetch_assoc($result2))
        {
            $output .= "<option value='".$adr['vname']."|".$adr['nname']."|".$adr['kunden_id']."'>".$adr['vname']." ".$adr['nname']."</option>";
        }

        $output .= "</select>";
    }
    else
    {
        $output .= "Daten konnten nicht aus der Datenbank gelesen werden.<br>";    
        $output .= mysqli_error($db);
    }

    return $output;
}

and then concatenate it

echo "<tr> <td> Choose people:<br> " . choose_kunde() ." </td> </tr>";

Still you need work on the code to make it more clean.