Solved

Display yes or no based on percentage

Posted on 2014-01-21
16
160 Views
Last Modified: 2014-01-21
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
Comment
Question by:XK8ER
  • 6
  • 5
  • 5
16 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39798416
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
 
LVL 1

Author Comment

by:XK8ER
ID: 39798434
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39798456
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
 
LVL 1

Author Comment

by:XK8ER
ID: 39798463
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
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 400 total points
ID: 39798473
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
 
LVL 58

Expert Comment

by:Gary
ID: 39798479
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39798482
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
 
LVL 1

Author Comment

by:XK8ER
ID: 39798486
shall i have two text file and write to yes.txt and no.txt?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 58

Expert Comment

by:Gary
ID: 39798500
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39798520
0
 
LVL 1

Author Comment

by:XK8ER
ID: 39798526
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
 
LVL 58

Assisted Solution

by:Gary
Gary earned 100 total points
ID: 39798538
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
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 400 total points
ID: 39798557
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
 
LVL 1

Author Comment

by:XK8ER
ID: 39798616
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
 
LVL 58

Expert Comment

by:Gary
ID: 39798624
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39798750
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now