Jump to content

Cron not updating


Phpfr3ak

Recommended Posts

Sorry maybe a stupid question but Ive made a cron and its not updating the players cash as it should, can anyone spot why? I've looked but just cannot see it, Thanks appreciate your time for looking.

 

<?php
include("server.php");
$sql = "SELECT * FROM players";
$que = mysql_query($sql);
while($res=mysql_fetch_array($que)) {
    $sql2 = "SELECT * FROM players WHERE id = '$res[id]'";
    $que2 = mysql_query($sql2) or die(mysql_error());
    $res2 = mysql_fetch_array($que2);
    $hoes = $res2['Hoes'];
    $hobedding = $res2['MaxHoes'];
    $incomemod = $res2['HoIncome'];
    $morale = $res2['HoesMorale'];
if ($hoes < $hobedding) {
$income = (250 * $hoes * $incomemod * $morale);
} else {
if ($hoes > $hobedding) {
$income = (250 * $hobedding * $incomemod * $morale) + (125 * ($hoes - $hobedding) * $incomemod * $morale); 
}
$sql5 = "UPDATE players SET cash = cash + '$income' WHERE id = '$res[id]'";
mysql_query($sql5) or die(mysql_error()); 	
}
}
?>					

Link to comment
Share on other sites

If you indent your code properly, you can probably see that you are only performing the UPDATE query for one of the logic conditions. Also, your if(){}else{} if logic does not cover the case where $hoes is equal to $hobedding.

 

You are also needlessly re-executing the select query inside of the loop.

 

The following should be (untested) equivalent to what you are trying to do (I renamed variables to match their meaning in the table) -

 

<?php
include "server.php";
$sql = "SELECT * FROM players";
$que = mysql_query($sql);
while($res=mysql_fetch_array($que)) {
    $hoes = $res['Hoes'];
    $maxhoes = $res['MaxHoes'];
    $hoimcome = $res['HoIncome'];
    $hoesmorale = $res['HoesMorale'];
if ($hoes <= $maxhoes) {
	$income = (250 * $hoes * $hoincome * $hoesmorale);
} else {
	$income = (250 * $maxhoes * $hoincome * $hoesmorale) + (125 * ($hoes - $maxhoes) * $hoincome * $hoesmorale);
}
$sql5 = "UPDATE players SET cash = cash + $income WHERE id = {$res['id']}";
mysql_query($sql5) or die(mysql_error()); 	
}
?>

 

And you are aware that mysql can perform logic in a query. The following (untested) is all you really need to do -

 

<?php
include "server.php";
$sql5 = "UPDATE players SET cash = cash + IF(Hoes <= MaxHoes,250*Hoes*HoIncome*HoesMorale,(250*Hoes*HoIncome*HoesMorale) + (125*(Hoes-MaxHoes)*HoIncome*HoesMorale))";	
mysql_query($sql5) or die(mysql_error());
?>

 

 

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.