I'm working with a CSV file. when the user uploads the file. I parse the CSV then select the data from the array that I need. after that, I'm running a for loop to validate that data and saving the results in the array. but the problem is when I print the results array there's only result for 1 email and there are 4 emails. any suggestions?
$results = [];
$valid_emails = 0;
$invalid_emails = 0;
for ($i = 0; $i < $csv_array['row_count']; $i++) {
$email = $csv_array['data'][$i][$email_column];
$result = validate_email($email);
$results['Email'] = $email;
if ($result) {
$results['Result'] = 'valid';
$valid_emails++;
} else {
$results['Result'] = 'invalid';
$invalid_emails++;
}
}
echo '<pre>';
print_r($results);
echo '</pre><br>';
echo $valid_emails . '<br>';
echo $invalid_emails . '<br>';
Use $results[] to add one or more elements :
$results = [];
$valid_emails = 0;
$invalid_emails = 0;
for ($i = 0; $i < $csv_array['row_count']; $i++) {
$email = $csv_array['data'][$i][$email_column];
$result = validate_email($email);
$res['Email'] = $email;
if ($result) {
$res['Result'] = 'valid';
$valid_emails++;
} else {
$res['Result'] = 'invalid';
$invalid_emails++;
}
$results[] = $res;
}
echo '<pre>';
print_r($results);
echo '</pre><br>';
echo $valid_emails . '<br>';
echo $invalid_emails . '<br>';
You are overriding results every time in your loop, try this
$results = [];
$valid_emails = 0;
$invalid_emails = 0;
for ($i = 0; $i < $csv_array['row_count']; $i++) {
$email = $csv_array['data'][$i][$email_column];
$rowResult=[];
$result = validate_email($email);
$rowResult['Email'] = $email;
if ($result) {
$rowResult['Result'] = 'valid';
$valid_emails++;
} else {
$rowResult['Result'] = 'invalid';
$invalid_emails++;
}
$results[]=$rowResult;
}
echo '<pre>';
print_r($results);
echo '</pre><br>';
echo $valid_emails . '<br>';
echo $invalid_emails . '<br>';
$results contains only the last email validation.
You should store multiple results, and not only the last ;-)
Something like :
$results[$i]['Email'] = $email;
if ($result) {
$results[$i]['Result'] = 'valid';
$valid_emails++;
} else {
$results[$i]['Result'] = 'invalid';
$invalid_emails++;
}
if ($result) {
$results[$i] = 'valid';
$valid_emails++;
} else {
$results[$i] = 'invalid';
$invalid_emails++;
}
I need help to check if variables (from a database) are the same, like a test.
First I was using just "==", but I saw this: http://php.net/manual/en/language.operators.comparison.php , and now I'm using "===". But it still doesn't work.
My code:
$uids = array();
$trues = array();
while ($row = mysqli_fetch_assoc($result)) {
array_push($uids, $row['UID']);
} while ($row1 = mysqli_fetch_assoc($result1)) {
array_push($uids, $row1['UID']);
} while ($row2 = mysqli_fetch_assoc($result2)) {
array_push($uids, $row2['UID']);
}
for ($i = 0; $i < count($uids); $i++) {
$r = 0;
if ($row['question1'] === $row['uAnswer1']) {
$r++;
} else {
$r = $r;
} if ($row['question2'] === $row['uAnswer2']) {
$r++;
} else {
$r = $r;
} if ($row['question3'] === $row['uAnswer3']) {
$r++;
} else {
$r = $r;
} if ($row['question4'] === $row['uAnswer4']) {
$r++;
} else {
$r = $r;
} if ($row['question5'] === $row['uAnswer5']) {
$r++;
} else {
$r = $r;
} if ($row['question6'] === $row['uAnswer6']) {
$r++;
} else {
$r = $r;
} if ($row['question7'] === $row['uAnswer7']) {
$r++;
} else {
$r = $r;
} if ($row['question8'] === $row['uAnswer8']) {
$r++;
} else {
$r = $r;
} if ($row['question9'] === $row['uAnswer9']) {
$r++;
} else {
$r = $r;
} if ($row['question10'] === $row['uAnswer10']) {
$r++;
} else {
$r = $r;
}
array_push($trues, $r);
echo $uids[$i] . " [" . $r . "]<br>";
}
print_r($trues);
}
What the result is: https://hastebin.com/uxitoyoyib.php
So it actually says everything is right, but I know that it isn't. Can you help me with this?
Thanks!
Compare variables like
if ( strtolower(trim($row['question5'])) === strtolower(trim($row['uAnswer5'])) )
Remove spaces if added with strings, convert the string in lowercase to the exact match of both strings.
use
if ($row['question4'] == $row['uAnswer4']) {
//do something
}
and try to print your 2 variable.
You can use the strcmp() or strcasecmp() functions or the === operator.
I trying to make some simple library for encrypting files in PHP with OTP method. My problem is that some chars in decrypted code are different than original. I worked on it almost one week but without result. Is there problem with base64 chars or with encoding/decoding mechanism ?
Many thanks for the answers.
final class Otp
{
private static $charSet = array('+','/','0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L',
'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r',
's','t','u','v','w','x','y','z');
public static function encryptFile($originalFilePath, $encryptedFilePath, $keyFilePath)
{
if(!self::existsFile($keyFilePath) || !self::existsFile($encryptedFilePath)) {
if($originalFileData = self::existsFile($originalFilePath)) {
$originalFileBase64Data = base64_encode($originalFileData);
$originalFileBase64DataLength = strlen($originalFileBase64Data) - 1;
$originalFileBase64DataArray = str_split($originalFileBase64Data);
$encryptedData = NULL;
$encryptedDataKey = NULL;
for ($i = 0; $i <= $originalFileBase64DataLength; $i++) {
$randKey = rand(0, sizeOf(self::$charSet) - 1);
$arrayKey = array_search($originalFileBase64DataArray[$i], self::$charSet);
if($randKey > $arrayKey) {
$str = '-' . ($randKey - $arrayKey);
} elseif($randKey < $arrayKey) {
$str = ($randKey + $arrayKey);
} else {
$str = $randKey;
}
$encryptedData .= self::$charSet[$randKey];
$encryptedDataKey .= $str. ';';
}
$encryptedDataString = $encryptedData;
$encryptedDataKeyString = $encryptedDataKey;
if(!self::existsFile($keyFilePath)) {
file_put_contents($keyFilePath, $encryptedDataKeyString);
}
if(!self::existsFile($encryptedFilePath)) {
file_put_contents($encryptedFilePath, $encryptedDataString);
}
return 'OK';
} else {
return 'Source file not exists';
}
} else {
return 'Encrypted data already exists';
}
}
public static function decryptFile($encryptedFilePath, $keyFilePath, $decryptedFilePath)
{
$keyFileData = self::existsFile($keyFilePath);
$encryptedFileData = self::existsFile($encryptedFilePath);
$encryptedFileDataLength = strlen($encryptedFileData) - 1;
if($encryptedFileData && $keyFileData) {
$encryptedFileDataArray = str_split($encryptedFileData);
$keyFileDataArray = explode(';', $keyFileData);
$decryptedData = NULL;
for ($i = 0; $i <= $encryptedFileDataLength; $i++) {
$poziciaaktualneho = array_search($encryptedFileDataArray[$i], self::$charSet);
$poziciasifrovana = $keyFileDataArray[$i];
if($poziciasifrovana < 0) {
$move = $poziciasifrovana + $poziciaaktualneho;
} elseif($poziciasifrovana > 0) {
$move = $poziciasifrovana - $poziciaaktualneho;
} else {
$move = '0';
}
$decryptedData .= self::$charSet[$move];
}
if(!self::existsFile($decryptedFilePath)) {
file_put_contents($decryptedFilePath, base64_decode($decryptedData));
return 'OK';
} else {
return 'Decrypted data already exists';
}
}
}
private static function existsFile($filePath)
{
$fileData = #file_get_contents($filePath);
if($fileData) {
return $fileData;
}
return FALSE;
}
}
$originalFilePath = 'original.jpg';
$keyFilePath = 'Otp_Key_' . $originalFilePath;
$encryptedFilePath = 'Otp_Data_' . $originalFilePath;
$decryptedFilePath = 'Otp_Decrypted_' . $originalFilePath;
echo Otp::encryptFile($originalFilePath, $encryptedFilePath, $keyFilePath);
echo Otp::decryptFile($encryptedFilePath, $keyFilePath, $decryptedFilePath);
The problem seems to be only happening when $poziciaaktualneho is equal to $poziciasifrovana and so by adding another if statement on line 78 to check for this and instead set $move equal to $poziciasifrovana I was able to fix the problem. The below script should work:
final class Otp
{
private static $charSet = array('+','/','0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L',
'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r',
's','t','u','v','w','x','y','z');
public static function encryptFile($originalFilePath, $encryptedFilePath, $keyFilePath)
{
if(!self::existsFile($keyFilePath) || !self::existsFile($encryptedFilePath)) {
if($originalFileData = self::existsFile($originalFilePath)) {
$originalFileBase64Data = base64_encode($originalFileData);
$originalFileBase64DataLength = strlen($originalFileBase64Data) - 1;
$originalFileBase64DataArray = str_split($originalFileBase64Data);
$encryptedData = NULL;
$encryptedDataKey = NULL;
for ($i = 0; $i <= $originalFileBase64DataLength; $i++) {
$randKey = rand(0, sizeOf(self::$charSet) - 1);
$arrayKey = array_search($originalFileBase64DataArray[$i], self::$charSet);
if($randKey > $arrayKey) {
$str = '-' . ($randKey - $arrayKey);
} elseif($randKey < $arrayKey) {
$str = ($randKey + $arrayKey);
} else {
$str = $randKey;
}
$encryptedData .= self::$charSet[$randKey];
$encryptedDataKey .= $str. ';';
}
$encryptedDataString = $encryptedData;
$encryptedDataKeyString = $encryptedDataKey;
if(!self::existsFile($keyFilePath)) {
file_put_contents($keyFilePath, $encryptedDataKeyString);
}
if(!self::existsFile($encryptedFilePath)) {
file_put_contents($encryptedFilePath, $encryptedDataString);
}
return 'OK';
} else {
return 'Source file not exists';
}
} else {
return 'Encrypted data already exists';
}
}
public static function decryptFile($encryptedFilePath, $keyFilePath, $decryptedFilePath)
{
$keyFileData = self::existsFile($keyFilePath);
$encryptedFileData = self::existsFile($encryptedFilePath);
$encryptedFileDataLength = strlen($encryptedFileData) - 1;
if($encryptedFileData && $keyFileData) {
$encryptedFileDataArray = str_split($encryptedFileData);
$keyFileDataArray = explode(';', $keyFileData);
$decryptedData = NULL;
for ($i = 0; $i <= $encryptedFileDataLength; $i++) {
$poziciaaktualneho = array_search($encryptedFileDataArray[$i], self::$charSet);
$poziciasifrovana = $keyFileDataArray[$i];
if ($poziciasifrovana == $poziciaaktualneho) {
$move = $poziciasifrovana;
} elseif($poziciasifrovana < 0) {
$move = $poziciasifrovana + $poziciaaktualneho;
} elseif($poziciasifrovana > 0) {
$move = $poziciasifrovana - $poziciaaktualneho;
} else {
$move = '0';
}
$decryptedData .= self::$charSet[$move];
}
if(!self::existsFile($decryptedFilePath)) {
file_put_contents($decryptedFilePath, base64_decode($decryptedData));
return 'OK';
} else {
return 'Decrypted data already exists';
}
}
}
private static function existsFile($filePath)
{
$fileData = #file_get_contents($filePath);
if($fileData) {
return $fileData;
}
return FALSE;
}
}
$originalFilePath = 'original.jpg';
$keyFilePath = 'Otp_Key_' . $originalFilePath;
$encryptedFilePath = 'Otp_Data_' . $originalFilePath;
$decryptedFilePath = 'Otp_Decrypted_' . $originalFilePath;
echo Otp::encryptFile($originalFilePath, $encryptedFilePath, $keyFilePath);
echo Otp::decryptFile($encryptedFilePath, $keyFilePath, $decryptedFilePath);
Warning: I would not recommend using my solution in an enterprise setting if at all since I do not know why this fixes your script or what was
originally wrong with it and it is most likely not air tight.
Is there any better way of writing this:
if (in_array('WIN_WAITING', $statuses)) {
$this->globalStatus = 'WIN_WAITING';
} else if (in_array('IN_PLAY', $statuses)) {
$this->globalStatus = 'IN_PLAY';
} else if (in_array('WON', $statuses)) {
$this->pay($this->tickets['ticketID']);
$this->globalStatus = 'WON';
} else if (in_array('PAYEDOUT', $statuses)) {
$this->globalStatus = 'PAYEDOUT';
} else if (in_array('CLOSED', $statuses)) {
$this->globalStatus = 'CLOSED';
} else if (in_array('LOST', $statuses)) {
$this->globalStatus = 'LOST';
} else if (in_array('OPEN', $statuses)) {
$this->globalStatus = 'OPEN';
}
This should work for you:
(Here I just loop through all of your search strings and if I found it I break out the loop)
<?php
$search = ["WIN_WAITING", "IN_PLAY", "WON", "PAYEDOUT", "CLOSED", "LOST", "OPEN"];
foreach($search as $v) {
if(in_array($v, $statuses)) {
if($v == "WON") $this->pay($this->tickets['ticketID']);
$this->globalStatus = $v;
break;
}
}
?>
Maybe something like
$options = array('WIN_WAITING', 'IN_PLAY', 'WON', 'PAYEDOUT', 'CLOSED', 'LOST', 'OPEN');
for($i=0; $i<=7; $i++) {
if(in_array($options[$i], $statuses)) {
$this->globalStatus = $options[$i];
break;
}
}
Not tested, just an idea
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Im trying to make a poll in php. Im trying to gather data by writing the info to a txt file. How do I get the code to write the data to a txt file?
This all the code I have in my handler, how do I make it write to my txt file. Most of the stuff at the bottom doesn't matter yet. Try to look at the code that say if ($submit == 'submit') and what follows that.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Poll</title>
</head>
<body>
<?php
//no need for sport validation is unimportant and doesnt work
if (isset($_REQUEST['Soda'])) {
$Soda = $_REQUEST['Soda'];
} else {
$Soda = NULL;
echo '<p class="error">You forgot to select your favorite soda!</p>';
}
//This is end of soda validation
if (!empty($_REQUEST['Book'])) {
$Book = $_REQUEST['Book'];
} else {
$Book = NULL;
echo '<p class="error">You forgot to write in your favorite book!</p>';
}
//End of book validation
if (isset($_REQUEST['SOTU'])) {
$SOTU = $_REQUEST['SOTU'];
} else {
$SOTU = NULL;
echo '<p class="error">You forgot to select the two biggest issues of the state of the union address!</p>';
}
//End of SOTU validation
if (isset($_REQUEST['Soda']) && !empty($_REQUEST['Book']) && isset($_REQUEST['SOTU'])) {
echo' Thank You for filling out the survey!<br> You can see the results of the pole' . " here!<br><br> Your response has been recorded.";
} else {
echo '<p class="error">Please go ' . "back" . ' and fill out the poll!<p>';
}
//End of link responses
//Define variables and make sure file works
$submit = $_REQUEST['submit'];
$filename = 'poll_data.txt';
$handle = fopen($filename, 'a');
//next is the stuff that is to be appended
if ($submit == 'Submit') {
fopen($filename, 'w');
$newdata = $Soda . PHP_EOL;
fwrite($handle, $newdata);
} else { echo 'You didn\'t click submit';}
//Now to sort the data and present it
/*explode('PHP.EOL', $filename);
$CC = 0;
$P = 0;
$MD = 0;
$SS = 0;
$BR = 0;
$DLS = 0;
$O = 0;
foreach($filename as $value) {
if ($value = 'Coca-Cola') {
$CC = $CC + 1;
}
elseif ($value = 'Pepsi') {
$P = $P + 1;
}
elseif ($value = 'MtnDew') {
$MD = $MD + 1;
}
elseif ($value ='Sprite/Sierra-Mist') {
$SS = $SS + 1;
}
elseif ('BigRed') {
$BR = $BR + 1;
}
elseif ('DontLikeSoda') {
$DLS = $DLS + 1;
}
elseif ('Other') {
$O = $O + 1;
}
}*/
?>
this should work:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Poll</title>
</head>
<body>
<?php
//no need for sport validation is unimportant and doesnt work
if (isset($_REQUEST['Soda'])) {
$Soda = $_REQUEST['Soda'];
} else {
$Soda = NULL;
echo '<p class="error">You forgot to select your favorite soda!</p>';
}
//This is end of soda validation
if (!empty($_REQUEST['Book'])) {
$Book = $_REQUEST['Book'];
} else {
$Book = NULL;
echo '<p class="error">You forgot to write in your favorite book!</p>';
}
//End of book validation
if (isset($_REQUEST['SOTU'])) {
$SOTU = $_REQUEST['SOTU'];
} else {
$SOTU = NULL;
echo '<p class="error">You forgot to select the two biggest issues of the state of the union address!</p>';
}
//End of SOTU validation
if (isset($_REQUEST['Soda']) && !empty($_REQUEST['Book']) && isset($_REQUEST['SOTU'])) {
echo' Thank You for filling out the survey!<br> You can see the results of the pole' . " here!<br><br> Your response has been recorded.";
} else {
echo '<p class="error">Please go ' . "back" . ' and fill out the poll!<p>';
}
//End of link responses
//Define variables and make sure file works
$submit = $_REQUEST['submit'];
$filename = 'poll_data.txt';
//next is the stuff that is to be appended
if ($submit == 'Submit') {
$handle = fopen($filename, 'a');
fputs($handle, $Soda.PHP_EOL);
fclose($handle);
} else { echo 'You didn\'t click submit';}
//Now to sort the data and present it
/*explode('PHP.EOL', $filename);
$CC = 0;
$P = 0;
$MD = 0;
$SS = 0;
$BR = 0;
$DLS = 0;
$O = 0;
foreach($filename as $value) {
if ($value = 'Coca-Cola') {
$CC = $CC + 1;
}
elseif ($value = 'Pepsi') {
$P = $P + 1;
}
elseif ($value = 'MtnDew') {
$MD = $MD + 1;
}
elseif ($value ='Sprite/Sierra-Mist') {
$SS = $SS + 1;
}
elseif ('BigRed') {
$BR = $BR + 1;
}
elseif ('DontLikeSoda') {
$DLS = $DLS + 1;
}
elseif ('Other') {
$O = $O + 1;
}
}*/
?>
I think if you use
fopen($filename, 'r');
that should work.
http://www.php.net/manual/en/function.fopen.php