Solved

Display yes or no based on percentage

Posted on 2014-01-21
16
157 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
shall i have two text file and write to yes.txt and no.txt?
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 58

Expert Comment

by:Gary
Comment Utility
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
Comment Utility
0
 
LVL 1

Author Comment

by:XK8ER
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

771 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