XK8ER
asked on
Display yes or no based on percentage
I am trying to find a simple way to display "yes" and "no" based on a percentage. this is just an example in vb.net I can set a static and do something like this (70% and 30%).. there issue with php is that if I do something like this and then refresh its like start the app all over again so it wont work.. how can I go around that?
how exactly can I accomplish something this simple using PHP?
Static iNo As Long, iYes As Long
If iNo > iYes * 0.42 Then
iYes = iYes + 1
Return False 'this is the 70%
Else
iNo = iNo + 1
Return True 'this is the 30%
End If
how exactly can I accomplish something this simple using PHP?
If you refresh the page of course it is going to reset. If you want to persist a value between page loads then you need to store the value in cookie/session
ASKER
is it possible to store the value in a file because in cookie will only work for one user I am trying to make this work for all users..
Yes, you can store the value in a file or in a data base. Here are some "getting started" resources for PHP:
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
I'll try to give you a translated code sample as soon as the pot is off the stove...
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
I'll try to give you a translated code sample as soon as the pot is off the stove...
ASKER
normally this is how I write to text
this is some sample code I've got.
$file = fopen("test.txt","w");
echo fwrite($file,"Hello World. Testing!");
fclose($file);
this is some sample code I've got.
$numVotes = 100;
$trueVotes = 70;
$falseVotes = 30;
function addVote($boolVote){
$numVotes++;
if($boolVote)
$trueVotes++:
return $trueVotes * $numVotes / 100; //percent of true votes
}
else{
$falseVotes++;
return $falseVotes * $numVotes / 100; //percent of false votes
}
$newVote = true;
$percent = addVote($newVote);
($newVote)$strVote = "true":$strVote = "false";
echo sprintf("The vote is %s and the percent is %01.2f", $strVote, $percent);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
PHP does not have server scope variables like you have in .net
To create the file
http://ie1.php.net/file_put_contents
To get the file
http://ie2.php.net/file_get_contents
To create the file
http://ie1.php.net/file_put_contents
To get the file
http://ie2.php.net/file_get_contents
PHP uses curly bracket control structure throughout, so this code fragment fails because it has unbalanced curly brackets.
if($boolVote)
$trueVotes++:
return $trueVotes * $numVotes / 100; //percent of true votes
}
This would be closer to right: if($boolVote) { // NOTE THE OPENING CURLY BRACKET
$trueVotes++:
return $trueVotes * $numVotes / 100; //percent of true votes
}
ASKER
shall i have two text file and write to yes.txt and no.txt?
If your site is busy you want to keep read/writes to a minimum, even if it is not busy you don't want unnecessary read/writes that will only slow things down.
Stick with one file.
Stick with one file.
Procedural PHP variable scope:
http://www.php.net/manual/en/language.variables.scope.php
OOP variable (property) visibility:
http://www.php.net/manual/en/language.oop5.visibility.php
http://www.php.net/manual/en/language.variables.scope.php
OOP variable (property) visibility:
http://www.php.net/manual/en/language.oop5.visibility.php
ASKER
this is what I have and its working perfectly fine, what do you guys recommend ?
<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 'On');
ini_set('display_startup_errors', 'On');
function addVote(){
$iYes = file_get_contents('yes.txt', true);
$iNo = file_get_contents('no.txt', true);
if ($iNo > ($iYes * 0.42)) {
$iYes++; // INCREMENT THE "YES" COUNT
$file = fopen("yes.txt","w");
echo fwrite($file,$iYes);
fclose($file);
return 'yes'; // FOR THE 70%
} else {
$iNo++; // INCREMENT THE "NO" COUNT
$file = fopen("no.txt","w");
echo fwrite($file,$iNo);
fclose($file);
return 'no'; // FOR THE 30%
}
}
echo 'Vote: '.addVote();
?>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I understand, so do you guys recommend I move this to MySQL instead of using files?
something like this?
something like this?
$sql1 = mysqli_query($GLOBALS['connection'],"SELECT iYes FROM myVote");
$ret1 = mysqli_fetch_assoc($sql1) ;
$iYes = $ret1['iYes'];
$sql2 = mysqli_query($GLOBALS['connection'],"SELECT iNo FROM myVote");
$ret2 = mysqli_fetch_assoc($sql2) ;
$iNo = $ret2['iNo'];
if ($iNo > ($iYes * 0.42)) {
$iYes++;
mysqli_query($GLOBALS['connection'],"UPDATE myVote SET iYes='$iYes' ;");
return 'yes'; // FOR THE 70%
} else {
$iNo++;
mysqli_query($GLOBALS['connection'],"UPDATE myVote SET iNo='$iNo' ;");
return 'no'; // FOR THE 30%
}
For the sake of this just stick with file based, using a db gives you no benefit and would be slower than direct file access.
My guess is that there are other data base activities and this will only be a slice of the application. You might try timing the script using a data base vs using a flat file. You will not find any meaningful difference in a correctly-configured web server, If the database makes your app easier to write, I would not hesitate to use it.
But that said, the data base is not a black box. It can and will fail for reasons that are outside of your programmatic control. So you want to make the script test for errors and fail "softly" as possible. Error handling is documented in this article:
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
But that said, the data base is not a black box. It can and will fail for reasons that are outside of your programmatic control. So you want to make the script test for errors and fail "softly" as possible. Error handling is documented in this article:
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html