Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts

    while loop problem

    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 Code:
    <?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);
        }
        
        }
    ?>
    Last edited by Jon W; 01-10-2010 at 03:21 AM.

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    you don't need a loop and don't need two query. You can replace this:
    PHP Code:
            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:
    PHP Code:
    $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

  • #3
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    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
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #4
    Regular Coder
    Join Date
    Nov 2009
    Location
    Hamilton, New Zealand
    Posts
    126
    Thanks
    0
    Thanked 17 Times in 17 Posts
    Quote Originally Posted by JAY6390 View Post
    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.
    Affordable Web Design (New Zealand Based)
    Internet Marketing Guru
    PHP/mySQL Expert
    -------------------------------------------


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •