简体   繁体   中英

My foreach query is not working and i am getting an error

I am getting a Warning: Invalid argument supplied for foreach() message with the code below.

The idea is that I have 2 tables, one is a stock table which has a large amount of data imported to it daily, and is linked. The other table (although not a parent table) is a list of sites, they have a common field called StockName.

The idea of this code, is that, when run, it is supposed to loop through all of the sites in the site table, get the StockName field, then use that to lookup the latest value in the stockdetails table, and then update the Site table with up to date info. Thats the theory, here is the code:

$rstmp = CustomQuery("SELECT * FROM Sites");
$datatmp = db_fetch_array($rstmp);
$SitePk = $datatmp['SitePk'];

foreach ($SitePk as $item)
{
echo $item."<BR>";
$rstmp2 = CustomQuery("SELECT * FROM ImportStockDetails where StockName='".$datatmp['StockName']."' ORDER BY `Date` DESC LIMIT 1");
$datatmp2 = db_fetch_array($rstmp2);

$latestdate = $rstmp2["LastStockDate"];
$latestcylpropane = $rstmp2["PropaneCylinders"];
$latestcylbutane = $rstmp2["ButaneCylinders"];
$latestbulkpropane = $rstmp2["BulkPropane"];
$latestbulkbutane = $rstmp2["BulkButane"];
$latesttotal = $latestcylpropane+$latestcylbutane+$latestbulkpropane+$latestbulkbutane;
$latestratio = $latesttotal/$datatmp['SiteMaxCapacity'];

global $dal;
$dal_table = $dal->Table("Sites"); 
$dal_table->Param["SitePk"] = $item; 
$dal_table->Value["LatestPropaneCylinderStock"] = $latestcylpropane;
$dal_table->Value["LatestButaneCylinderStock"] = $latestcylbutane;
$dal_table->Value["LatestBulkPropaneStock"] = $latestbulkpropane;
$dal_table->Value["LatestBulkButaneStock"] = $latestbulkbutane;
$dal_table->Value["LatestTotalStock"] = $latesttotal;
$dal_table->Value["`Stock/LimitRatio`"] = $latestratio;
$dal_table->Value["LastStockDate"] = $latestdate;

$dal_table->Update();
}

You are getting the warning because $SitePk is not an array, it is just a value. You should try with $datatmp as db_fetch_array($rstmp); return array which is stored in $datatmp.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM