Reputation: 187
I've been smashing my head against a wall over this for the last 3 hours.
I have a short script I wrote up a while ago for routine maintenance of a PHP/MySQL application. The problem is not with the script itself. I can call this PHP script by its URL manually and it executes fine and does what it is supposed to. The problem is that I have tried to include the script in the application when a certain event happens to have it run and it won't work.
I get the fatal Cannot use string offset as an array only in function error with the following line: $ingredientID = $prepItems[$i]['ingredientID'];
I understand the error means you assign a string and then try to treat it as an array, but $ingredientID has not been declared anywhere in the function before this line, and $prepItems[$i]['ingredientID'] is itself a string anyway! I checked and $prepItems is returned correctly. When I go back and call the URL the script works, when I try to include it from inside another script in the application it won't work. I don't get it....
The full code is below....
//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)
Upvotes: 0
Views: 95
Reputation: 7035
The problem is with your $prepItems
variable/function. Insert a var_dump
to see what you're actually looking at.
...
$cp = count($prepItems);
var_dump($prepItems);
if ($prepItems)
{
...
Upvotes: 1