Skip to content
Advertisement

strings are not combining at a right formate

I have a SQL string and two functions which have string returns. I’m trying to combine them like that:

 echo "INSERT INTO product_cat( ".columnPrinter(getTables("product_cat"))." ) VALUES ( ".dataPrinter($_POST['addMenu'])." )";

but functions are not printing in the right place and they all just go to the first not in parentheses! the result is like: Result

My function is :

function dataPrinter($data){

        foreach($data as $dt){
           $names .= "'".$dt."',";
        }
        echo rtrim($names,",");
    }
 function columnPrinter($columns){

        foreach($columns as $clm){
           $names .= $clm['Field'].",";
        }
        echo rtrim($names,",");
    }

I tried using variables even though the result was the same.

Advertisement

Answer

Just return the result, don’t print it:

function dataPrinter($data){
    $names = '';    
    foreach($data as $dt){
       $names .= "'".$dt."',";
    }
    return rtrim($names,",");
}
function columnPrinter($columns){
    $names = '';
    foreach($columns as $clm){
       $names .= $clm['Field'].",";
    }
    return rtrim($names,",");
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement