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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 27
  1. #1
    New Coder
    Join Date
    Sep 2005
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts

    While loop not working

    PHP Code:
    <?php
    require("config.php");
    function 
    customers_total($zone_customers$other_shops) {
    global 
    $customers;
    /* Randomize the ammount of customers to make it more realistic but still
    keep it within 20% of average daily customers. */

    $zone $zone_customers/100*17;
    $zoneb $zone_customers+$zone;
    $zonea $zone_customers-$zone;
    $zone_customers rand($zonea$zoneb);

    /* Work out the ammount of customers the opposition is going to steal */

    $other_shops $other_shops+3;
    $yay $zone_customers/$other_shops;

    /* Work out what fraction of the customers the players store is gonna get */
    $int_one $yay;

    $int_two $zone_customers;

    $int_three $int_two/100;

    $int_one_s $int_one/$int_three;
    $int_one_s round($int_one_s0);

    $fract $int_one_s;

    /* Check if the players store has any marketing. If so increase the chance of the
    store getting customers. */
    if($marketing 1) {

    $fract $marketing $fract;

    }

    /* Work out the final ammount of customers for the store*/
    $customers $zone_customers/100*$fract;

    $customers round($customers0);

    }
    function 
    customers_buy($price$sell_price){
    global 
    $banter$chance$limit$status,$billy;
    $rrp $price/100*18;
    $rrp $rrp+$price;
    $limit "10.5";

    If (
    $price 1000) {
    $limit $limit-9.5;
    } else if(
    $price 900){
    $limit $limit-9;
    } else if(
    $price 800){
    $limit $limit-8;
    } else if(
    $price 700){
    $limit $limit-7;
    } else if(
    $price 600){
    $limit $limit-6;
    } else if(
    $price 500){
    $limit $limit-5;
    } else if(
    $price 400){
    $limit $limit-4;
    } else if(
    $price 300){
    $limit $limit-3;
    } else if(
    $price 200){
    $limit $limit-2;
    } else if(
    $price 100){
    $limit $limit-1;
    } else if(
    $price 50){
    $limit $limit;
    }


    if(
    $sell_price $rrp) {

    $chance $sell_price-$rrp;
    $chance $chance/$rrp*100;



    If (
    $chance 100) {
    $limit "0";
    } else if(
    $chance 90){
    $limit $limit-9;
    } else if(
    $chance 80){
    $limit $limit-8;
    } else if(
    $chance 70){
    $limit $limit-7;
    } else if(
    $chance 60){
    $limit $limit-6;
    } else if(
    $chance 50){
    $limit $limit-5;
    } else if(
    $chance 40){
    $limit $limit-4;
    } else if(
    $chance 30){
    $limit $limit-3;
    } else if(
    $chance 20){
    $limit $limit-2;
    } else if(
    $chance 10){
    $limit $limit-1;
    } else if(
    $chance 5){
    $limit $limit;
    }
    }

    if(
    $sell_price $rrp) {

    $chance $rrp-$sell_price;

    $chance $chance/$rrp*100;


    If (
    $chance 100) {
    $limit $limit+9.5;
    } else if(
    $chance 90){

    $limit $limit+"9.3";
    } else if(
    $chance 80){
    $limit $limit+9.2;
    } else if(
    $chance 70){
    $limit $limit+8;
    } else if(
    $chance 60){
    $limit $limit+7;
    } else if(
    $chance 50){
    $limit $limit+6;
    } else if(
    $chance 40){
    $limit $limit+5;
    } else if(
    $chance 30){
    $limit $limit+4;
    } else if(
    $chance 20){
    $limit $limit+3;
    } else if(
    $chance 10){
    $limit $limit+2;
    } else if(
    $chance 5){
    $limit $limit+1;
    }
    }



    $pussy rand(020);
    $x "0"
    $limit round($limit0);

    while(
    $x $limit) {



    $tither rand(020);


    if (
    $tither == $pussy) {
    return 
    "true";


    } else {
    return 
    "false";
    }
    $x++;
    }

    }


    ////////Start/////////////
    global $poo;
    customers_total(10002);  

    $id "1";
    $result mysql_query("SELECT * FROM player_products WHERE store_id='$id'");

    echo 
    "$customers";
    $bah 0;
    $y 0;  
    while(
    $bah $customers
        {
        
        
        while(
    $row mysql_fetch_array($result)) 
            {
            
    $price $row['price'];
            
    $m_price $row['m_price'];
            
    $id $row['id'];
            
    $names $row['names'];
            
    $banter customers_buy($m_price$price);
    /*
    echo "<hr>Chance : $chance<br> Limit: $limit<br> Status: $status<br>Customers: $customers<br>Billy:$y";
    */

            
    if($banter == "true"){ 

            
    $quan $row['quantity'];
            
    $quan $quan-1;
            
    mysql_query("UPDATE player_products SET quantity='$quan' WHERE id='$id'"
            or die(
    mysql_error()); 
            
    $bee++;
            }
            
    $boo++;
            }

        
    $bah++;

        }

    echo 
    "<br>Bah:$bah<br>Bee:$bee<br>Boo:$boo";


    ?>
    I've made this script but there is a problem with the while loops down the bottom. The loop while($row = mysql_fetch_array($result)) is only beeing carried out 15 times when I want it to be carried out 15 x 1000 times. Can anyone help?

  • #2
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    What is $customers when you echo it out?
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #3
    New Coder
    Join Date
    Sep 2005
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Its a random number but last time I ran it it echoed 18763

  • #4
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    So we need to find out why its breaking out of the loops. Are you testing for one product in particular or are you testing with all products that you have? I think while you're debugging you should focus on one product for right now that way you have more control. Also,try and echo out $bah and $customers within the first loop and see what you get.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #5
    New Coder
    Join Date
    Sep 2005
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I did that and it echoed numbers one to arround seventeen thousand and it also echoed number of customers which was arround 17000

  • #6
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    And how many records does this return?
    PHP Code:
    $id "1";
    $result mysql_query("SELECT * FROM player_products WHERE store_id='$id'"); 
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #7
    New Coder
    Join Date
    Sep 2005
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    15 records

  • #8
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Like this maybe?!

    PHP Code:
    while($row mysql_fetch_array($result)*1000

  • #9
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    Quote Originally Posted by jabbic View Post
    PHP Code:
    <?php
    require("config.php");
    $id "1";
    $result mysql_query("SELECT * FROM player_products WHERE store_id='$id'");

    echo 
    "$customers";
    $bah 0;
    $y 0;  
    while(
    $bah $customers
        {
        
    /* WORKS: So you are saying that this inner loop is successfully carried out and runs 15 times which is correct b/c there are 15 products with an ID of 1.  So its only running the outer while loop once. */    
        
    while($row mysql_fetch_array($result)) 
            {
            
    $price $row['price'];
            
    $m_price $row['m_price'];
            
    $id $row['id'];
            
    $names $row['names'];
            
    $banter customers_buy($m_price$price);
    /*
    echo "<hr>Chance : $chance<br> Limit: $limit<br> Status: $status<br>Customers: $customers<br>Billy:$y";
    */

            
    if($banter == "true"){ 

            
    $quan $row['quantity'];
            
    $quan $quan-1;
            
    mysql_query("UPDATE player_products SET quantity='$quan' WHERE id='$id'"
            or die(
    mysql_error()); 
            
    $bee++;
            }
            
    $boo++;
            }

        
    $bah++;

        }

    echo 
    "<br>Bah:$bah<br>Bee:$bee<br>Boo:$boo";


    ?>
    I've made this script but there is a problem with the while loops down the bottom. The loop while($row = mysql_fetch_array($result)) is only beeing carried out 15 times when I want it to be carried out 15 x 1000 times. Can anyone help?
    I added a comment to your code. Its appears that your inner loop is working correctly and your outer loop runs only once.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #10
    New Coder
    Join Date
    Sep 2005
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    but when I echo something in the outer loop it echos it like 15000 times.

  • #11
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    I just noticed something....you're only running an update query....I think your query is being running 15 * 1000 times. Add this to see how many queries are being ran.
    Add this before your first while statement. (outside any of your loops.
    PHP Code:
    $queries 0
    Add this inside of your inner while statement.
    PHP Code:
    $queries += 1
    Add this to at the very end of your script. (outside of either loop)
    PHP Code:
    echo 'Total amount of queries ran: ' $queries
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #12
    New Coder
    Join Date
    Sep 2005
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Total amount of queries ran: 0

  • #13
    New Coder
    Join Date
    Sep 2005
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Dunno why that is happening though, that is beeing carried out 15 times. The update query is only carried out under certian circumstances

  • #14
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    PHP Code:
    <?php

    $queries 
    0//init queries
    while($bah $customers
        {
        
    /* WORKS: So you are saying that this inner loop is successfully carried out and runs 15 times which is correct b/c there are 15 products with an ID of 1.  So its only running the outer while loop once. */    
        
    while($row mysql_fetch_array($result)) 
            {
            
    $price $row['price'];
            
    $m_price $row['m_price'];
            
    $id $row['id'];
            
    $names $row['names'];
            
    $banter customers_buy($m_price$price);
    /*
    echo "<hr>Chance : $chance<br> Limit: $limit<br> Status: $status<br>Customers: $customers<br>Billy:$y";
    */

            
    if($banter == "true"){ 

            
    $quan $row['quantity'];
            
    $quan $quan-1;
            
    mysql_query("UPDATE player_products SET quantity='$quan' WHERE id='$id'"
            or die(
    mysql_error());
             
    $queries += 1;  //increment the number of queries successfully ran.
            
    $bee++;
            }
            
    $boo++;
            }

        
    $bah++;

        }

    echo 
    "<br>Bah:$bah<br>Bee:$bee<br>Boo:$boo";
    echo 
    "<br />";
    echo 
    'Total amount of queries ran: ' $queries//Display how many queries were ran.

    ?>
    Your code looks something like this? It shouldn't be 0 b/c you said its entering the loop therefore it should increment $queries every time that loop successfully runs.

    Also, can I ask why'd you want to run an update query multiple times? Is there something I am overlooking?
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #15
    New Coder
    Join Date
    Sep 2005
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Its the only way I can think to do it. Ok what the script does is generates a random ammount of customers along the lines of the average ammount. After doing this it runs a function that works out the chance of a customer buying a product depending on the price. The more over the rrp the less chance there is of customer buying. It then trys to run that function for each product and runs it the same ammount of times there are customers.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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