Jump to content

[SOLVED] Quick Question


Sinikka

Recommended Posts

Can anyone see what is wrong with this? Instead of randomly choosing what to stock and dividing the max_stock in half it's stocking everything with one being the amount in stock. I've tried messing around with it but I can't figure it out. Any help would be appreciated!

 

<?php

#!/usr/bin/php


$dbh=mysql_connect ("removed", "removed", "removed") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("removed");

$findItems = mysql_query("SELECT * FROM shop_items2 WHERE game = '1'");
while ($getItems = mysql_fetch_array($findItems))
{
$id = $getItems[id];
$getItems[max_stock] = round($getItems[max_stock] / 2);
$rand[$id] = rand(0,$getItems[max_stock]);
}


$sql2="UPDATE `shop_items2` SET cur_stock=$rand[$id] WHERE game = '1'";

if(mysql_query($sql2))
{
echo $sql2;
}

?>

Link to comment
https://forums.phpfreaks.com/topic/73767-solved-quick-question/
Share on other sites

not really clear what you are doing - perhaps you should print out the results of your query to see what you are actually selecting (is the result just one record or lots? - if its lots then remember that your final query will NOT hold different values for $rand[$id]; that will simply be the last value calculated in your loop!).

Link to comment
https://forums.phpfreaks.com/topic/73767-solved-quick-question/#findComment-372192
Share on other sites

The script is for one of my shop restocking crons. I need to to take the max stock possible and divide that in half but then choose the items that it pulls randomly so it's not stocking all items that can possibly stock each time.

 

When I execute the script this is what I get:  UPDATE `shop_items2` SET cur_stock=1 WHERE game = '1'

 

*edit and it would stock multiple shops at once so it would be more then one record.

Link to comment
https://forums.phpfreaks.com/topic/73767-solved-quick-question/#findComment-372194
Share on other sites

your update sets all the cur_stock values to the last value pulled in the previous loop

 

try

foreach ($rand as $id => $val)
{
    $sql2="UPDATE `shop_items2` SET cur_stock=$val WHERE id = '$id' ";
    mysql_query($sql2)
}

Link to comment
https://forums.phpfreaks.com/topic/73767-solved-quick-question/#findComment-372196
Share on other sites

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.