...

View Full Version : While loop not working



jabbic
07-08-2007, 09:14 PM
<?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_s, 0);

$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($customers, 0);

}
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(0, 20);
$x = "0";
$limit = round($limit, 0);

while($x < $limit) {



$tither = rand(0, 20);


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


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

}


////////Start/////////////
global $poo;
customers_total(1000, 2);

$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?

StupidRalph
07-08-2007, 09:34 PM
What is $customers when you echo it out?

jabbic
07-08-2007, 09:45 PM
Its a random number but last time I ran it it echoed 18763

StupidRalph
07-08-2007, 09:55 PM
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.

jabbic
07-08-2007, 10:00 PM
I did that and it echoed numbers one to arround seventeen thousand and it also echoed number of customers which was arround 17000

StupidRalph
07-08-2007, 10:13 PM
And how many records does this return?

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

jabbic
07-08-2007, 10:16 PM
15 records

matak
07-08-2007, 10:20 PM
Like this maybe?!


while($row = mysql_fetch_array($result)*1000)

StupidRalph
07-08-2007, 10:29 PM
<?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.

jabbic
07-08-2007, 10:31 PM
but when I echo something in the outer loop it echos it like 15000 times.

StupidRalph
07-08-2007, 10:43 PM
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.

$queries = 0;

Add this inside of your inner while statement.

$queries += 1;

Add this to at the very end of your script. (outside of either loop)

echo 'Total amount of queries ran: ' . $queries;

jabbic
07-08-2007, 10:52 PM
Total amount of queries ran: 0

jabbic
07-08-2007, 11:00 PM
Dunno why that is happening though, that is beeing carried out 15 times. The update query is only carried out under certian circumstances

StupidRalph
07-08-2007, 11:04 PM
<?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?

jabbic
07-08-2007, 11:11 PM
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.

StupidRalph
07-08-2007, 11:30 PM
Well as you've stated. If you were to echo something out in the first loop it will print out "like 15000 times". So that will say that your first loop is working.

We know your second loop is working b/c you say that its works 15 times (the amount of products with that ID). There are only 15 products with that ID so it updates the $quan for that product. then recurses through the loop and updates (and overwrites) the previous iteration through the loop. I realize that you have a condition whether or not the query will be ran but I don't see the logic where it will change throughout the loop. If its true on the first pass, it seems that it would be true on each successive pass.

jabbic
07-08-2007, 11:41 PM
But i tried doing an echo whithin the wwhile and it only echoed 15 times. If you look inside the customers_buy function then you see it will be different every time. Well is there an alternative way to do this?

StupidRalph
07-09-2007, 12:01 AM
Your inner loop will only run 15 times b/c thats what your condition suggests. There are only 15 records with an ID of 1.

It makes no difference if your outer loop runs 1 or 1,000,000 times it will still reflect in the DB the same way. The only thing you are doing is continually updating the values of the same fields over and over with the same values.

Are you sure you have that $queries variable set up the way I have in that example? It should be in 3 different places.



while($bah < $customers)

^ - I think this is where your logic goes wrong. Other than wasting system resources this isn't doing anything.

Give me a second to check your code out again. Can you post your most recent version of the code?

StupidRalph
07-09-2007, 12:11 AM
How are you coming to the conclusion that this is only being ran 15 times? By the number of records being updated?

jabbic
07-09-2007, 12:16 AM
recent 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_s, 0);

$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($customers, 0);

}
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(0, 20);
$x = "0";
$limit = round($limit, 0);

while($x < $limit) {



$tither = rand(0, 20);


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


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

}


////////Start/////////////
global $poo;
customers_total(100000, 2);
$queries = 0;
$id = "1";


echo "$customers";
$bah = 0;
$y = 0;

while($bah < $customers)
{
echo "<br>Bah1:$bah</br> Customers: $customers";
$result = mysql_query("SELECT * FROM player_products WHERE store_id='$id'") or die(mysql_error());
while($row = @mysql_fetch_assoc($result))
{
$price = $row['price'];
$m_price = $row['m_price'];
$id = $row['id'];
$names = $row['names'];
$banter = customers_buy($m_price, $price);

$queries += 1;


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";
echo 'Total amount of queries ran: ' . $queries;
echo "Bo:$customers";
?>

jabbic
07-09-2007, 12:26 AM
its not letting me edit so i'll double post. The database isn't beeing updated 15 times. The whle has looped 15 times. the database is updated like 1 times.

StupidRalph
07-09-2007, 12:43 AM
You have said that this part of the code would be printed out "like 15000 times" correct? If so, we can conclude that the first while loop is indeed intact.

echo "<br>Bah1:$bah</br> Customers: $customers";

What is the value of $queries (which is really nothing more than a counter) when it prints at the bottom?

jabbic
07-09-2007, 12:45 AM
fithteen

StupidRalph
07-09-2007, 08:40 AM
I really don't know whats going on....
if

echo "<br>Bah1:$bah</br> Customers: $customers";
is being echoed to the browser 15000 times,
then this query

$result = mysql_query("SELECT * FROM player_products WHERE store_id='$id'") or die(mysql_error());
is executed 15000 times as well. So your second while statement will be true and will also be ran 15000 times. Thus the variable $queries should be atleast the same number that your outer loop is ran.

In your inner loop, you are only running an UPDATE query and setting some variables. Every time your condition is met it will run the query and UPDATE your database overwriting whatever previous update. If you condition is not bet you aren't doing anything inside of your loop besides setting the variables. You are printing anything to the browser, inserting anything into a database, or writing to any files. So there is no accurate for you to keep track of how many times that inner loop has ran. The only way you can keep track of that is by how many times the variable $queries (which can be renamed $counter) is incremented. This variable should at least be the same number of times the outer loop is ran barring your initial query doesn't die(). In which case you will be notified by mysql_error().

jabbic
07-09-2007, 10:41 AM
But its still only 15 though. The $queries variable.

StupidRalph
07-09-2007, 09:23 PM
I have no idea why that is...the $queries (which is actually the counter) should be incremented with every successive loop. Maybe someone will come in here and point us in the right direction. Or if you figure it out, post what the problem was.

jabbic
07-09-2007, 09:42 PM
I hope so! Its soo confusing!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum