• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 188
  • Last Modified:

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?

        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

Open in new window


how exactly can I accomplish something this simple using PHP?
0
XK8ER
Asked:
XK8ER
  • 6
  • 5
  • 5
3 Solutions
 
GaryCommented:
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
0
 
XK8ERAuthor Commented:
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..
0
 
Ray PaseurCommented:
Yes, you can store the value in a file or in a data base.  Here are some "getting started" resources for PHP:
http://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...
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
XK8ERAuthor Commented:
normally this is how I write to text
$file = fopen("test.txt","w");
echo fwrite($file,"Hello World. Testing!");
fclose($file);

Open in new window


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);

Open in new window

0
 
Ray PaseurCommented:
Something like this...

<?php // RAY_temp_mplungjan.php
error_reporting(E_ALL);


// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28344494.html

/* PROBLEM DEFINITION
        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
*/


// ACTIVATE THESE STATEMENTS AND ASSIGN VALUES FROM SOME DATA SOURCE
/* 
   $iNo  = ?
   $iYes = ?
*/

// COMPARE VALUES
if ($iNo > ($iYes * 0.42))
{
    // INCREMENT THE "YES" COUNT
    $iYes++;
    return FALSE; // FOR THE 70%
}
else
{
    $iNo++;
    return TRUE;  // FOR THE 30%
}

Open in new window

HTH, ~Ray
0
 
GaryCommented:
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
0
 
Ray PaseurCommented:
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
    }
 

Open in new window

This would be closer to right:

    if($boolVote) { // NOTE THE OPENING CURLY BRACKET

       $trueVotes++:
       return $trueVotes * $numVotes / 100; //percent of true votes
    }
 

Open in new window

0
 
XK8ERAuthor Commented:
shall i have two text file and write to yes.txt and no.txt?
0
 
GaryCommented:
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.
0
 
Ray PaseurCommented:
0
 
XK8ERAuthor Commented:
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();
?>

Open in new window

0
 
GaryCommented:
Looks fine to me.
There is another option and that is using APC, where you can define a variable and it is accessible in any session.  APC is a caching software.
But this will depend on if it is installed and if not can you install it.
There's also memcache but I don't have any experience with it.
0
 
Ray PaseurCommented:
You can save yourself some typing...
$file = fopen("no.txt","w");
echo fwrite($file,$iNo);
fclose($file);

Open in new window

is typically written like this:
file_put_contents("no.txt",$ino);

Open in new window

0
 
XK8ERAuthor Commented:
I understand, so do you guys recommend I move this to MySQL instead of using files?

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%
	}

Open in new window

0
 
GaryCommented:
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.
0
 
Ray PaseurCommented:
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:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 6
  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now