功能仅在手动调用URL时有效?

时间:2012-05-18 13:09:17

标签: php function include scope

在过去的3个小时里,我一直把头撞在墙上。

我刚才写了一个简短的脚本,用于PHP / MySQL应用程序的日常维护。问题不在于脚本本身。我可以通过它的URL手动调用这个PHP脚本,它执行正常并完成它应该做的事情。问题是,当某个事件碰巧让它运行时,我试图将该脚本包含在应用程序中,但它不起作用。

我得到致命的不能使用字符串偏移作为数组仅在函数错误中使用以下行: $ ingredientID = $ prepItems [$ i] ['ingredientID'];

我理解错误意味着你分配了一个字符串,然后尝试把它当作一个数组,但是在这行之前函数中没有声明$ ingredientID,$ prepItems [$ i] ['ingredientID']是反正本身就是一个字符串!我检查并正确返回$ prepItems。当我返回并调用URL脚本时,当我尝试将其包含在应用程序中的另一个脚本中时,它将无法工作。我不明白......

完整的代码如下......

//UPDATE PRODUCTION NUMBERS

function fixOrders(){

$k = 0;
$date = date('Y-m-d',strtotime('yesterday'));

$qry = "SELECT a.orderID,a.qty,a.date,a.itemID,b.itemName,b.itemType FROM orders a join items b on a.itemID = b.itemID WHERE a.date > '$date' AND a.itemID = '$itemID'";
$res = mysql_query($qry);
$qry = "INSERT INTO prep (orderID,dateProd,qty,itemID,dept) VALUES ";
while ($row = mysql_fetch_array($res))
{
$itemID = $row['itemID'];
$qty = $row['qty'];
$itemType = $row['itemType'];
$date = $row['date'];
$orderID = $row['orderID'];
$prepItems = prepItems($itemID);
if ($prepItems) removePrepItems($orderID);
$cp = count($prepItems);

if ($prepItems)
{
    for ($i = 0; $i < $cp; $i++)
    {               
        $ingredientID = $prepItems[$i]['ingredientID'];
        $ingredientWeight = $prepItems[$i]['ingredientWeight'];
        $ingredientUnit = $prepItems[$i]['ingredientUnit'];
        $productionOffset = $prepItems[$i]['productionOffset']; 
        $qty = ($ingredientWeight * $qty);
        if ($ingredientUnit == 'lbs') $qty = ($qty * 16);
        $date = date('Y-m-d',strtotime($date.' +'.$productionOffset.' days'));
        echo $date.' '.$productionOffset.'<br>';
        $qry .= "('$orderID','$date','$qty','$ingredientID','$itemType'),";
    }   
    $k++;
}   
}
$qry = substr($qry,0,-1);
mysql_query($qry);
echo $qry;
}

fixOrders($itemID)

1 个答案:

答案 0 :(得分:1)

问题在于您的$prepItems变量/功能。插入var_dump即可查看您实际查看的内容。

...
$cp = count($prepItems);

var_dump($prepItems);

if ($prepItems)
{
...