Jump to content

fluctuation of prices


katarra

Recommended Posts

I'm trying to write a script (cronjob) that will fluctuate the prices of all the items at a random level (+/- .03% or so), but when I ran this, it changed all the prices to match row #1 and wiped out everything else. (in other words, everything is now the same price as row #1)

 

Thanks for any help!

 

$query = $db->execute("select `price` from `items`");
$item = $query->fetchrow();
$price1 = ceil($item['price']*1.1);
$price2 = floor($item['price']*.9);
$newprice = mt_rand($price1, $price2);
$q = $db->execute("update `items` SET `price`=$newprice");

Link to comment
https://forums.phpfreaks.com/topic/196678-fluctuation-of-prices/
Share on other sites

Hopefully, there is some unique ID for each row of the table.  You need to include that ID in the SELECT statement, loop through each row that is returned, and add a WHERE clause to the UPDATE.  Your current UPDATE has no WHERE clause so it is updating every row in the table with the same price.

Hmm... close.

 

It'll change the 1st row, but it doesn't change any of the following rows.

 

$query=$db->execute("select `id`,`price` from `items`");
$item = $query->fetchrow();
$id=$item["id"];
$price1 = ceil($item['price']*1.03);
$price2 = floor($item['price']*.97);
$newprice = mt_rand($price1, $price2);
$q=$db->execute("update `items` SET `price`=$newprice WHERE `id`=$id");

$query=$db->execute("select `id`,`price` from `items`");
while($item = $query->fetchrow())
{
$id=$item["id"];
$price1 = ceil($item['price']*1.03);
$price2 = floor($item['price']*.97);
$newprice = mt_rand($price1, $price2);
$q=$db->execute("update `items` SET `price`=$newprice WHERE `id`=$id");
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.