Arijit Aich Arijit Aich - 2 months ago 7
MySQL Question

Prohibit PHP Function To Echo Results In Front-End, When Using For MySQL Query

Goal: To create a function that converts a string to its corresponding number. For e.g. ABCDEFGHIJ would get converted to 1234567890.

Function I Have Written:

function decrypt_code($data1){

$someArray=array($data1[0],$data1[1],$data1[2],$data1[3],$data1[4],$data1[5],$data1[6],$data1[7],$data1[8],$data1[9],$data1[10],$data1[11],$data1[12],$data1[13]); // size 7
foreach($someArray as $value1){
if($value1 == "A"){$dc1= "1";}
elseif($value1 == "B"){$dc2= "2";}
elseif($value1 == "C"){$dc3= "3";}
elseif($value1 == "D"){$dc4= "4";}
elseif($value1 == "E"){$dc5= "5";}
elseif($value1 == "F"){$dc6= "6";}
elseif($value1 == "G"){$dc7= "7";}
elseif($value1 == "H"){$dc8= "8";}
elseif($value1 == "I"){$dc9= "9";}
elseif($value1 == "J"){$dc10= "0";}

}
return $dc = $dc1.$dc2.$dc3.$dc4.$dc5.$dc6.$dc7.$dc8.$dc9.$dc10;
}


Problem: I have included a function library in the header. Now each time i am calling the function, its printing the decrypted numbers. I just want to store them & echo as and when required. This is also creating a problem when i am using the function to get the decrypted numbers and run them in a SQL Command. This is taking place in the header and the function is printing the numbers in the header part of the website because i am using the function to run a MySQL Command in the header part.

NOTE: I do not want to know how to write the function because that i have already achieved. I just do not want the function to echo the result when i am using it for running a MySQL query.

Answer

You must keep sub decrypted part in function and return whole decrypted string . This example work for more than 14 symbol. And dividing string into array str_split better solution

function decrypt_code($str){
    $someArray = str_split($str); // size is not bounded

    $decrypt_code;
    foreach($someArray as $value1){ 
        if($value1 == "A"){$decrypt_code .= "1";}
        elseif($value1 == "B"){$decrypt_code .= "2";}
        elseif($value1 == "C"){$decrypt_code .= "3";}
        elseif($value1 == "D"){$decrypt_code .= "4";}
        elseif($value1 == "E"){$decrypt_code .= "5";}
        elseif($value1 == "F"){$decrypt_code .= "6";}
        elseif($value1 == "G"){$decrypt_code .= "7";}
        elseif($value1 == "H"){$decrypt_code .= "8";}
        elseif($value1 == "I"){$decrypt_code .= "9";}
        elseif($value1 == "J"){$decrypt_code .= "0";}
    }

    return $decrypt_code;
}