Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Warning: Variable passed to each()

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Warning: Variable passed to each()

Postby jake » Wed Apr 10, 2013 11:17 am

I get the following message:

Warning: Variable passed to each() is not an array or object in...

and the line it refers to looks like this:

Code: Select all
while(list($key, $val) = each($order_content['products']))



in the file:

Code: Select all
<?php
//TEMPLATES
require_once "../smarty/Smarty.class.php";
$srt = new smarty();
$srt->template_dir = "../templates";
$srt->compile_dir = "../templates_c";
//TEMPLATES

//PDF
require_once "../pdf/fpdf.php";
require_once "../pdf/inc_pdf.php";
$pdf = new PDF();
//PDF

if ($_POST['date'] && $_POST['faktura_nr'] && $_POST['order_id']) $finished_pdf = true;
$srt->assign("user_data", htmlentities($_POST['user_data']));
$srt->assign("content", htmlentities($_POST['content']));

$payment_alternatives = array(
1 => array('name' => 'Faktura', 'price' => 0),
4 => array('name' => 'Kortbetalning', 'price' => 0),
5 => array('name' => 'Delbetalning', 'price' => 0),
6 => array('name' => 'Delbetalning via Klarna', 'price' => 19)
);


$order_content = unserialize(str_replace("\\", "", html_entity_decode($_POST['content'])));
$user_data = unserialize(str_replace("\\", "", html_entity_decode($_POST['user_data'])));
$srt->assign("user_data", $user_data);
$srt->assign("post", $_POST);
$srt->assign("currentDate", date("Y-m-d"));

//print_r($order_content);
//print_r($user_data);

$srt->assign('diff', $_POST['diff']);
$srt->assign('paytype', $_POST['paytype']);

if ($finished_pdf){
$srt->assign("date", $_POST['date']);
$srt->assign("faktura_nr", $_POST['faktura_nr']);
$srt->assign("order_nr", $_POST['order_id']);
$srt->assign("name", $_POST['name']);
$srt->assign("lastname", $_POST['lastname']);
$srt->assign("address", $_POST['address']);
$srt->assign("zipcode", $_POST['zipcode']);
$srt->assign("city", $_POST['city']);
$srt->assign("payment", $payment_alternatives[$_POST['paytype']]['name']);
$pdf->addPage();
$pdf->addData($_POST['date'], $_POST['faktura_nr'], $_POST['order_id'], $payment_alternatives[$_POST['paytype']]['name']);
$pdf->addAddress($_POST['name'], $_POST['lastname'], $_POST['address'], $_POST['zipcode'], $_POST['city']);
}else{
if ($_POST['date']) $srt->assign("date", '<input name ="date" type="text" value="'.$_POST['date'].'" />');
else $srt->assign("date", '<input name ="date" type="text" value="'.date("Y.m.d").'" />');
$srt->assign("faktura_nr", '<input name="faktura_nr" type="text" value="'.$_POST['faktura_nr'].'" />');
$srt->assign("order_nr", '<input name="order_id" type="text" value="'.$_POST['order_id'].'" />');
$srt->assign("submit", '<br /><input name="send" type="submit" value="Faktura-print" /><br />');
$srt->assign("payment", $payment_alternatives[$_POST['paytype']]['name']);

if ($_POST['name']) $srt->assign("name", '<input name ="name" type="text" value="'.$_POST['name'].'">');
else $srt->assign("name", '<input name ="name" type="text" value="'.$user_data['name'].'">');
if ($_POST['lastname']) $srt->assign("lastname", '<input name ="lastname" type="text" value="'.$_POST['lastname'].'">');
else $srt->assign("lastname", '<input name ="lastname" type="text" value="'.$user_data['lastname'].'">');
if ($_POST['address']) $srt->assign("address", '<input name ="address" type="text" value="'.$_POST['address'].'">');
else $srt->assign("address", '<input name ="address" type="text" value="'.$user_data['address'].'">');
if ($_POST['zipcode']) $srt->assign("zipcode", '<input name ="zipcode" type="text" value="'.$_POST['zipcode'].'">');
else $srt->assign("zipcode", '<input name ="zipcode" type="text" value="'.$user_data['zipcode'].'">');
if ($_POST['city']) $srt->assign("city", '<input name ="city" type="text" value="'.$_POST['city'].'">');
else $srt->assign("city", '<input name ="city" type="text" value="'.$user_data['city'].'">');
}

$srt->assign("action", $_SERVER['PHP_SELF']);
if ($finished_pdf){
$pdf->SetXY(10, 65);
$pdf->addPosition("Artnr", 'Antal', 'Benämning', 'Pris/enhet', 'Summa', 1);
}else{
$srt->assign("var", 0);
$srt->display("invoice.tpl");
}

while(list($key, $val) = each($order_content['products'])){
$srt->assign("art_code", "");
$total += $val['value_netto'];
$total_brutto += $val['value_brutto'];
$total_income += $val['income'];
$old_total += $val['old_value'];
$old_total_brutto += $val['old_value_brutto'];

$srt->assign("amount", $val['amount']);
$srt->assign("prod_name", ((($val['name'] != "") && ($val['towar'] != 0)) ? ($val['name']):("Frakt")));
$srt->assign("price", sprintf("%0.2f", $val['price_netto']));
$srt->assign("summa", sprintf("%0.2f", $val['value_netto']));
if ($val['art_code'] != 'DELIVERY') $srt->assign("art_code", $val['art_code']);
if ($finished_pdf){
$pdf->addPosition(($val['art_code'] == 'DELIVERY' ? '' : secureCode($val['art_code'])), $val['amount'], ((($val['name'] != "") && ($val['towar'] != 0)) ? ($val['name']):("Frakt")), $val['price_netto'], $val['value_netto'], 0);
}else{
$srt->assign("var", 1);
$srt->display("invoice.tpl");
$srt->assign("art_code", "");
}
}

////////////////////////////////////
// REBATE
////////////////////////////////////
if (false && $order_content['rebate']['value'] > 0){
$total = $total - $order_content['rebate']['value'] ;
$rebate_u_moms = $order_content['rebate']['value'] * 0.8;

$total = $total - $rebate_u_moms;
$srt->assign("amount", 1);
$srt->assign("prod_name", 'Rabatt');
$srt->assign("price", '');
$srt->assign("summa", sprintf("-%0.2f", $order_content['rebate']['value']));
$srt->assign("var", 1);
$srt->display("invoice.tpl");
}
////////////////////////////////////
// END REBATE
////////////////////////////////////

$srt->assign("art_code", "Betalning");
$srt->assign("amount", "1");
$srt->assign("prod_name", $payment_alternatives[$_POST['paytype']]['name']);
$srt->assign("price", sprintf("%0.2f", $payment_alternatives[$_POST['paytype']]['price']));
$srt->assign("summa", sprintf("%0.2f", $payment_alternatives[$_POST['paytype']]['price']));
$total += $payment_alternatives[$_POST['paytype']]["price"];
if ($finished_pdf){
$pdf->addPosition('', '', $payment_alternatives[$_POST['paytype']]['name'], $payment_alternatives[$_POST['paytype']]['price'], $payment_alternatives[$_POST['paytype']]['price'], 0);
}else{
$srt->assign("var", 1);
$srt->display("invoice.tpl");
}
// }
//}

$srt->assign("total", sprintf("%0.2f", $total));
$srt->assign("moms", sprintf("%0.2f", ($total*0.25)));
$srt->assign("sum", sprintf("%0.2f", $total+($total*0.25)));
if ($finished_pdf){
$pdf->addCounting($total, ($total*0.25), $total+($total*0.25));
$pdf->Output('Faktura_'.$_POST['faktura_nr'].'.pdf', true);
}else{
$srt->assign("var", 2);
$srt->display("invoice.tpl");
$srt->assign("var", 3);
$srt->display("invoice.tpl");
}

?>


what can be wrong?
jake
New php-forum User
New php-forum User
 
Posts: 1
Joined: Wed Apr 10, 2013 11:09 am

Re: Warning: Variable passed to each()

Postby seandisanti » Wed Apr 10, 2013 2:26 pm

The error is telling you what's wrong; you're calling a foreach() or each() with a variable that contains a scalar value, rather than collection like an array, or an object. I could tell you what line number too, but you left that part off of your error text.
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm


Return to PHP coding => General

Who is online

Users browsing this forum: asuspete and 11 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.