I am trying to convert a $_POST from lowercase to uppercase. The variable is an array and I have tried strtoupper but it just does not display as uppercase. What would be the correct way to achieve this result based on my code. Thanks
$array = array();
$array = $_POST['file_add'];
$outString = '';
foreach ($array as $file) {
$file = mysql_real_escape_string($file);
$sql = "SELECT custref FROM files WHERE custref = '$file'";
$result = mysql_query($sql) or die ('{"opp":"error","file":"' . mysql_error() . '"}');
// If there are dupe entries, send message to jquery
if (mysql_num_rows($result) > 0) {
$outString .= $file . ' ';
//echo $output;
}
}
if ($outString) { // if there are numbers in this string, then there are error Duplicates
$error = array('opp' => "error", 'file' => $outString); // box will have the TEXT for your Dialog Text
// IMPORTANT, changed JSON output to use an Opperation (opp) to indicate
// in javascript if there was an error OR not
$output = json_encode($error);
echo $output;
exit();
}
function getUpperPost($keepVar = false){
$return_array = array();
/* Edited on 4/1/2015 */
foreach($_POST as $postKey => $postVar){
$return_array[$postKey] = strtoupper($postVar);
}
if($keepVar){
$_POST = $return_array;
}else{
return $return_array;
}
}
This takes each object in the post, makes it an upper string and then adds it to the end of the $return_array
. At the end it returns that array. You can call this function anywhere in your code.
If you'd like, I'd love to include php's pass-by-reference system such that you could just give this an array and then it would automatically reassign those values to the values in the array, but if that's not relevant to your question, then I'll hold back. With that system it could be called like such: makeArrayUpper($array);
and the next time you access a variable in the array, it would automatically be upper case for every member.
EDIT Here's an implementation with your newly posted code.
$array = $_POST['file_add'];
$outString = '';
$sql_connection = new mysqli("dbHost", "dbUsername", "dbPassword", "dbName");
if($sql_connection->connect_errno){
die("db error");
}
foreach ($array as $file) {
$file = strtoupper($file);
$file = $sql_connection->real_escape_string($file);
$sql = "SELECT `custref` FROM `files` WHERE `custref` = '$file'";
$result = $sql_connection->query($sql) or die ('{"opp":"error","file":"' . mysql_error() . '"}');
// If there are dupe entries, send message to jquery
if ($result->num_rows > 0) {
$outString .= $file . ' ';
}
}
// Close the MySQL connection
$sql_connection->close();
if(!empty($outString)){
$error = array('opp' => "error", 'file' => trim($outString)); // box will have the text for your Dialog Text
// IMPORTANT, changed JSON output to indicate
// in javascript if there was an error or not
$output = json_encode($error);
echo $output;
exit();
}
Edit Note: I have slightly revised my previous answer because I noticed the getUpperPost()
function was not preserving the keys of the $_POST
variable. With the new revision made on April 1st, 2015, the returned (or reassigned) array will not have the same keys as the $_POST
array.
Try:
$arr = array("one","two","three"); // let's assume that your $_POST['file_add']; is this kind of array
$n = array_map(function($v) { // creating new array named $n
return strtoupper($v);
}, $arr);
print_r($n); // Array ( [0] => ONE [1] => TWO [2] => THREE )
How about just applying it to each element:
foreach ($_POST['file_add'] as &$value) {
$value = strtoupper($value);
}