Jump to content

faulty if statement?


blueman378

Recommended Posts

hi guys,

i have this code here

<?php
//Retrieve Vote
$vote = $_GET['vote'];
$current = 49;
    $votes = 99;
function down()
{
global $current;
global $votes;
    $current--;
    $votes--;
    $new = $current/$votes;
    $new*=100;
echo "This was voted:";
    echo $new;
}
function up()
{
global $current;
global $votes;
    $current++;
    $votes++;
    $new = $current/$votes;
    $new*=100;
echo "This was voted:";
    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";
}
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 still says only one star,

any ideas?

Link to comment
https://forums.phpfreaks.com/topic/83342-faulty-if-statement/
Share on other sites

Try this mate

 

<?php
//Retrieve Vote
$vote = $_GET['vote'];
$current = 49;
    $votes = 99;
function down()
{
global $current;
global $votes;
    $current--;
    $votes--;
    $new = $current/$votes;
    $new*=100;
echo "This was voted:";
    echo $new;
}
function up()
{
global $current;
global $votes;
    $current++;
    $votes++;
    $new = $current/$votes;
    $new*=100;
echo "This was voted:";
    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";
}
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...";
}
?>

 

I haven't checked code above the if statements..

 

Regards

Liam

Link to comment
https://forums.phpfreaks.com/topic/83342-faulty-if-statement/#findComment-424020
Share on other sites

How can any of your "elseif" clauses work with this:

<?php
elseif (($new>=39.6)&&($new>=59.5)) {
?>

You are saying that both conditions have to be true -- $new greater than or equal to two numbers -- impossible. Also, your functions don't return anything, so the variable $new isn't set to anything. I think what you want is:

<?php
//Retrieve Vote
$vote = $_GET['vote'];
$current = 49;
    $votes = 99;
function down($cur,$vts)
{
    $cur--;
    $vts--;
    $new = $cur/$vts;
    $new*=100;
    return (array($cur,$vts,$new);
}
function up($cur,$vts)
{
    $cur++;
    $vts++;
    $new = $cur/$vts;
    $new*=100;
    return (array($cur,$vts,$new)
}


  //Retrieve current value

//End current value
//What vote
if ($vote == 'down') {
        list($current,$votes,$new) = down($current,$votes);
}  elseif ($vote==up) {
        list($current,$vots,$new) = up($current,$votes);
} else {
    echo "Hmm... your vote wasnt cast for some reason";
}
$errmsg = '';
if ($new <= 19.5) $star = 1;
   elseif ($new >= 19.6 && $new <= 39.5) $star = 2;
   elseif ($new >= 39.6 && $new <= 59.5) $star = 3;
   elseif ($new >= 59.6 && $new <= 79.5) $star = 4
   elseif ($new >= 79.6) $star = 5;
   else
      $errmsg = "Hmm we have no vote...";
if ($errmsg != '') echo $errmsg;
else echo '<img src="star/' . $star . 'star.png" width="130" height="25">';
?>

 

Notice how I also streamlined your code.

 

 

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/83342-faulty-if-statement/#findComment-424051
Share on other sites

$new isn't defined. Return the calculated value from the up() and down() functions

 

<?php
//Retrieve Vote
$vote = $_GET['vote'];
$current = 49;
$votes = 99;

function down()
{
    global $current;
    global $votes;
        $current--;
        $votes--;
        $new = $current/$votes;
        $new*=100;
        return $new;
}

function up()
{
    global $current;
    global $votes;
        $current++;
        $votes++;
        $new = $current/$votes;
        $new*=100;
        return $new;
}


  //Retrieve current value

//End current value
//What vote
if ($vote==down) {
        $new = down();                                   // assign value to $new
}  elseif ($vote==up) {
        $new = up();                                     // assign value to $new
} else {
    echo "Hmm... your vote wasnt cast for some reason";
    
}
echo "This was voted: $new";


?>

Link to comment
https://forums.phpfreaks.com/topic/83342-faulty-if-statement/#findComment-424052
Share on other sites

hey ken, thanks for that after some minor errors it worked perfectly :)

<?php
//Retrieve Vote
$vote = $_GET['vote'];
$current = 49;
    $votes = 99;
function down($cur,$vts)
{
    $cur--;
    $vts--;
    $new = $cur/$vts;
    $new*=100;
    return (array($cur,$vts,$new));
}
function up($cur,$vts)
{
    $cur++;
    $vts++;
    $new = $cur/$vts;
    $new*=100;
    return (array($cur,$vts,$new));
}


  //Retrieve current value

//End current value
//What vote
if ($vote == 'down') {
        list($current,$votes,$new) = down($current,$votes);
}  elseif ($vote==up) {
        list($current,$vots,$new) = up($current,$votes);
} else {
    echo "Hmm... your vote wasnt cast for some reason";
}
$errmsg = '';
if ($new <= 19.5) $star = 1;
   elseif ($new >= 19.6 && $new <= 39.5) $star = 2;
   elseif ($new >= 39.6 && $new <= 59.5) $star = 3;
   elseif ($new >= 59.6 && $new <= 79.5) $star = 4;
   elseif ($new >= 79.6) $star = 5;
   else
      $errmsg = "Hmm we have no vote...";
if ($errmsg != '') echo $errmsg;
else echo '<img src="star/' . $star . 'star.png" width="130" height="25">';
?>

Link to comment
https://forums.phpfreaks.com/topic/83342-faulty-if-statement/#findComment-424568
Share on other sites

ok guys so this is working now,

<hr>
<h1>Rating:</h1>
<?php
//Retrieve Vote
$vote = $_GET['vote'];
$current = 1;
    $votes = 1;
function down($cur,$vts)
{
    $cur--;
    $vts++;
    $new = $cur/$vts;
    $new*=100;
    return (array($cur,$vts,$new));
}
function up($cur,$vts)
{
    $cur++;
    $vts++;
    $new = $cur/$vts;
    $new*=100;
    return (array($cur,$vts,$new));
}


  //Retrieve current value

//End current value
//What vote
if ($vote == 'down') {
        list($current,$votes,$new) = down($current,$votes);
}  elseif ($vote==up) {
        list($current,$vots,$new) = up($current,$votes);
} else {
    echo "Hmm... your vote wasnt cast for some reason";
}
$errmsg = '';
if ($new <= 19.5) $star = 1;
   elseif ($new >= 19.6 && $new <= 39.5) $star = 2;
   elseif ($new >= 39.6 && $new <= 59.5) $star = 3;
   elseif ($new >= 59.6 && $new <= 79.5) $star = 4;
   elseif ($new >= 79.6) $star = 5;
   else
      $errmsg = "Hmm we have no vote...";
if ($errmsg != '') echo $errmsg;
else echo 'This was Rated '. $new .' from '. $votes.' votes<br> so is a <img src="star/' . $star . 'star.png" 

width="260" height="50"> Game';
?>

 

but there is only one problem with it,

all games start as 1 star,

is there any way i could make this so they all start as three star?

Link to comment
https://forums.phpfreaks.com/topic/83342-faulty-if-statement/#findComment-424577
Share on other sites

well heres my final code so it adds does the database stuff, and i you have not voted then it shows the voting thing,

 

<hr>
<h1>Rating:</h1>
<?php
//Retrieve Vote
  //Retrieve current value
$game = $_GET['game'];
$vote = $_GET['vote'];
    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
function down($cur,$vts)
{
global $database;
global $game;
    $cur--;
    $vts++;
    $new = $cur/$vts;
    $new*=100;
$q1 = "UPDATE " . Games . " SET RId = '$cur'
WHERE gName = '$game'";
$result = $database->query($q1) or die("Error: " . mysql_error());
$q2 = "UPDATE " . Games . " SET Votes = '$vts'
WHERE gName = '$game'";
$result = $database->query($q2) or die("Error: " . mysql_error());
    return (array($cur,$vts,$new));
}
function up($cur,$vts)
{
global $database; 
global $game; 
  $cur++;
    $vts++;
    $new = $cur/$vts;
    $new*=100;
$q1 = "UPDATE " . Games . " SET RId = '$cur'
WHERE gName = '$game'";
$result = $database->query($q1) or die("Error: " . mysql_error());
$q2 = "UPDATE " . Games . " SET Votes = '$vts'
WHERE gName = '$game'";
$result = $database->query($q2) or die("Error: " . mysql_error());
    return (array($cur,$vts,$new));
}
function none($cur,$vts)
{
global $database; 
global $game; 
    $new = $cur/$vts;
    $new*=100;
    return (array($cur,$vts,$new));
}


  //Retrieve current value

//End current value
//What vote
if ($vote == 'down') {
        list($current,$votes,$new) = down($current,$votes);
}  elseif ($vote==up) {
        list($current,$votes,$new) = up($current,$votes);
} else { 
            list($current,$votes,$new) = none($current,$votes);
echo "<b>Have your say:</b> <a href='showgame.php?game=".$game."&vote=up'>
<img src='images/good.png'></a> | <a href='showgame.php?game=".$game."&vote=down'>
<img src='images/bad.png'></a><br><hr>";
}
$errmsg = '';
if ($new <= 19.5) $star = 1;
   elseif ($new >= 19.6 && $new <= 39.5) $star = 2;
   elseif ($new >= 39.6 && $new <= 59.5) $star = 3;
   elseif ($new >= 59.6 && $new <= 79.5) $star = 4;
   elseif ($new >= 79.6) $star = 5;
   else
      $errmsg = "Hmm we have no vote...";
if ($errmsg != '') echo $errmsg;
else echo 'This was Rated '. $new .' from '. $votes.' votes<br> so is a <img src="star/' . $star . 'star.png" width="260" height="50"> Game';
?>

Link to comment
https://forums.phpfreaks.com/topic/83342-faulty-if-statement/#findComment-424602
Share on other sites

$q1 = "UPDATE " . Games . " SET RId = '$cur'

WHERE gName = '$game'";

$result = $database->query($q1) or die("Error: " . mysql_error());

$q2 = "UPDATE " . Games . " SET Votes = '$vts'

WHERE gName = '$game'";

$result = $database->query($q2) or die("Error: " . mysql_error());

 

You only need 1 query

 

<?php
$q1 = "UPDATE Games
        SET 
            RId = '$cur', 
            Votes = '$vts'
        WHERE gName = '$game'";
$result = $database->query($q1) or die("Error: " . mysql_error());

?>

 

Link to comment
https://forums.phpfreaks.com/topic/83342-faulty-if-statement/#findComment-424604
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.