...

View Full Version : while loop problem



Jon W
01-10-2010, 02:10 AM
Hello everyone. I'm currently working on a text based game and I'm working on the money script right now. The goal of the script is to add silver and for every 100 silver give them one gold. I've ran into a little problem and I can't seem to figure this out. I created a loop to loop through all of the users inside of my database and if the users silver is greater or equal to 100 it would subtract from the number and add 1 gold for that users until the users silver was less then 100 silver. I'm not completely sure what I'm doing wrong here and I was hoping someone here could help me out.

This is what I've got so far.



<?php
define("ROOT_PATH", "../../../");
include(ROOT_PATH . "common.php");
$sql = "SELECT username FROM users";
$query = mysql_query($sql);

while($users = mysql_fetch_array($query)) {
$s = "SELECT silver, gold, peasant FROM users WHERE username = '".$users['username']."'";
$cQuery = mysql_query($s);

$player = mysql_fetch_array($cQuery);

$added = round($player['peasant'] * 25 + $player['silver']);
$update = "UPDATE users SET silver = '$added' WHERE username = '".$users['username']."'";
mysql_query($update);

while($player['silver'] >= 100) {

//Update players silver
$silverNow = $player['silver'] - 100;
$updateSilver = "UPDATE users SET silver = '$silverNow' WHERE username = '".$users['username']."'";
mysql_query($updateSilver);


//Update the players gold.
$goldNow = $player['gold'] + 1;
$updateGold = "UPDATE users SET gold = '$goldNow' WHERE username = '".$users['username']."'";
mysql_query($updateGold);
}

if($_GET['setTime'] == true) {
$wait = time()+3600;

$sysSQL = "UPDATE system SET gold_wait = '".$wait."'";
mysql_query($sysSQL);
}

}
?>

oesxyl
01-10-2010, 02:25 AM
you don't need a loop and don't need two query. You can replace this:


while($player['silver'] >= 100) {

//Update players silver
$silverNow = $player['silver'] - 100;
$updateSilver = "UPDATE users SET silver = '$silverNow' WHERE username = '".$users['username']."'";
mysql_query($updateSilver);


//Update the players gold.
$goldNow = $player['gold'] + 1;
$updateGold = "UPDATE users SET gold = '$goldNow' WHERE username = '".$users['username']."'";
mysql_query($updateGold);
}

with this:


$goldNow = $player['gold'] + ($player['silver'] - ($player['silver'] % 100))/100;
$silverNow = $player['silver'] % 100;
$query = "update users set gold = $goldNow, silver = $silverNow where username = '".$users['username']."'";


best regards

JAY6390
01-10-2010, 02:56 AM
You could do this without even needing to loop through any users or even using php to process it
UPDATE users SET gold = (gold + FLOOR(silver / 100)), silver = MOD(silver, 100)
That will update each and every one of them with one simple query

Rebbu
01-10-2010, 03:47 AM
You could do this without even needing to loop through any users or even using php to process it
UPDATE users SET gold = (gold + FLOOR(silver / 100)), silver = MOD(silver, 100)
That will update each and every one of them with one simple query

Heh nice.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum