Jump to content

[SOLVED] some php maths stuff


blueman378

Recommended Posts

hi guys im making a simple rating system and i need a way to work out the percentage,

when a user votes

get what i mean?

and so the script will be something like

<?php
//Retrieve Vote
$vote = $_GET['vote'];
  //Retrieve current value
$game = $_GET['game'];
    global $database;
    $q = "SELECT * FROM " . Games . "
          WHERE gName = '$game'
          ORDER BY `gName` ASC
         ";   
    $result = $database->query($q) or die("Error: " . mysql_error());
    /* Error occurred, return given name by default */
    $num_rows = mysql_numrows($result);
    if( $num_rows == 0 ){
      return 'Game Not Found!';
    }
    while( $row = mysql_fetch_assoc($result) ) {
    $current = $row[RId];
    $votes = $row[Votes];
    }
//End current value
//What vote
if ($vote = down) {
        function down($current, $votes)
{
    $current--;
    $votes--;
    $new = $current/$votes;
    $new*=100;
    echo $new;
}
down()} elseif ($vote = up) {
    function up($current, $votes)
{
    $current++;
    $votes++;
    $new = $current/$votes;
    $new*=100;
    echo $new;
}
up()
} else {
    echo "Hmm... your vote wasnt cast for some reason";
}
?>

 

would this work?

Link to comment
Share on other sites

well acctually lets scrap the db stuff for now so use;

<?php
//Retrieve Vote
$vote = $_GET['vote'];
  //Retrieve current value
$game = $_GET['game'];
    global $database;
    $q = "SELECT * FROM " . Games . "
          WHERE gName = '$game'
          ORDER BY `gName` ASC
         ";   
    $result = $database->query($q) or die("Error: " . mysql_error());
    /* Error occurred, return given name by default */
    $num_rows = mysql_numrows($result);
    if( $num_rows == 0 ){
      return 'Game Not Found!';
    }
    while( $row = mysql_fetch_assoc($result) ) {
    $current = $row[RId];
    $votes = $row[Votes];
    }
//End current value
//What vote
if ($vote = down) {
        function down($current, $votes)
{
    $current--;
    $votes--;
    $new = $current/$votes;
    $new*=100;
    echo $new;
}
down()} elseif ($vote = up) {
    function up($current, $votes)
{
    $current++;
    $votes++;
    $new = $current/$votes;
    $new*=100;
    echo $new;
}
up()
} else {
    echo "Hmm... your vote wasnt cast for some reason";
}
?>

Link to comment
Share on other sites

hmm ok well using this:

<?php
//Retrieve Vote
$current = 5;
    $votes = 22;
$vote = $_GET['vote'];
  //Retrieve current value

//End current value
//What vote
if ($vote = down) {
        function down()
{
global $current;
global $votes;
    $current--;
    $votes--;
    $new = $current/$votes;
    $new*=100;
    echo $new;
}
down();
}  elseif ($vote = up) {
    function up()
{
global $current;
global $votes;
    $current++;
    $votes++;
    $new = $current/$votes;
    $new*=100;
    echo $new;
}
up();
} else {
    echo "Hmm... your vote wasnt cast for some reason";
}
?>

i get 19.0476190476

using url test.php?vote=up

and test.php?vote=down

and test.php

they all get the same result

Link to comment
Share on other sites

ok i got it working with:

<?php
//Retrieve Vote
$vote = $_GET['vote'];
$current = 5;
    $votes = 22;
function down()
{
global $current;
global $votes;
    $current--;
    $votes--;
    $new = $current/$votes;
    $new*=100;
    echo $new;
}
function up()
{
global $current;
global $votes;
    $current++;
    $votes++;
    $new = $current/$votes;
    $new*=100;
    echo $new;
}


  //Retrieve current value

//End current value
//What vote
if ($vote==down) {
        down();
}  elseif ($vote==up) {
    up();
} else {
    echo "Hmm... your vote wasnt cast for some reason";
}
?>

but is there any way to round it to the closest whole number?

Link to comment
Share on other sites

Right, i'll be honest, I haven't read through your whole code yet cause you and yourself seem to be doing alright so far and posting faster than I can read.

 

Couldn't you use ceil() to round up a number to the next integer number...

 

http://au2.php.net/ceil

 

Sorry, I might be on the completely wrong track here but that's what i would do...

Link to comment
Share on other sites

ok thats good to hear,

i am not bothering with the rounding now,

but now i am trying to use this:

if ($new<=19.5) {
      ?><img src="star/1star.png" width="130" height="25"><?php
}  elseif ($new>=19.6&&$new>=39.5) { ?>
<img src="star/2star.png" width="130" height="25">
<?php }
elseif ($new>=39.6&&$new>=59.5) { ?>
<img src="star/3star.png" width="130" height="25">
<?php }
elseif ($new>=59.6&&$new>=79.5) { ?>
<img src="star/4star.png" width="130" height="25">
<?php }
elseif ($new>=79.6) { ?>
<img src="star/5star.png" width="130" height="25">
<?php }
else {
    echo "Hmm we have no vote...";
}

 

but no matter what $new equals it always shows as one star,

any ideas?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.