Solved

Display yes or no based on percentage

Posted on 2014-01-21
16
162 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 109

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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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 109

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 109

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
 
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 109

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 109

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 109

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP installation issues 11 59
applying error reporting code to see paypal error messages 13 51
Can't find PHP files on account that has WordPress 3 25
write xml in vb.net 2 24
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

776 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