цикл for — продолжить функция выключает мой PHP forloop

Поэтому я создаю загрузчик CSV для своих клиентов, чтобы загружать заказы на мой сайт на Shopify с помощью PHP.
Если вы посмотрите мой код, вы увидите 2 случая, когда я установил $errors быть TRUE, Затем, после всех возможных ошибок, я проверяю, $errors верно и если да, continue цикл, так что заказ не обрабатывается. По какой-то причине он останавливает весь forloop как break сделал бы. Любая помощь в этом от сообщества? Вот мой код:

$url = 'https://xxxxxxxxxxx:xxxxxxxxxxxx@mystore.myshopify.com/admin/orders.json';
$rows = explode(PHP_EOL, $_POST['CSV']);
$customer = $_POST['customer'];
$thisID = explode(',', $_POST['IDs']);
$lines = count($rows);
$IDs = count($thisID);
for ($x = 0; $x < $lines; $x++) {
$ordtot = 0;
$cells = str_getcsv($rows[$x]);
$line_items = array();
$POs = array();
if ($x > 0){
$csmpo = $cells[0];
$orddte = $cells[1];
$shipvia = $cells[2];
$shipdte = $cells[3];
$cncldte = $cells[4];
$ohsname = $cells[5];
$ohsaddr1 = $cells[6];
$ohsaddr2 = $cells[7];
$ohscity = $cells[8];
$ohsstate = $cells[9];
$ohszip = $cells[10];
$ohsphone = $cells[11];
$oddpart = $cells[12];
$oddupc = $cells[13];
$oddprice = $cells[14];
$oddordqty = $cells[15];
$ohdnote = $cells[16];
if ($ohsphone == ""){
$ohsphone = "1111111111";
}
for($a = 0; $a <= $IDs; $a++){
if ($a == $IDs){
$result .= "Order " . $csmpo . " NOT processed, item " . $oddpart . " not found. Please contact the webmaster.<br>";
$error = true;
break 1;
}
$myID = explode('/', $thisID[$a]);
$mySKU = explode('_', $thisID[$a]);
if ($mySKU[0] == $oddpart){
$line_items[] = array('variant_id' => $myID[1], 'quantity' => $oddordqty);
$ordtot = $ordtot + $oddordqty;
break 1;
}
}
$lines2 = $lines - $x;
$w = $x + 1;
for ($y = $w; $y < $lines2; $y++){
$cells2 = str_getcsv($rows[$y]);
if ($cells2[0] == $csmpo){
for($b = 0; $b <= $IDs; $b++){
if ($b == $IDs){
$result .= "Order " . $csmpo . " NOT processed, item " . $oddpart . " not found. Please contact the webmaster.<br>";
$error = true;
break 1;
}
$myID = explode('/', $thisID);
$mySKU = explode('_', $thisID);
if ($mySKU[0] == $cells2[12]){
$line_items[] = array('variant_id' => $myID[1], 'quantity' => $cells2[15]);
$ordtot = $ordtot + $cells2[15];
break 1;
}
}
$x = $x + 1;
} else {
break 1;
}
}
if ($error){continue;}
$order = array(
"order" => array(
"customer" => array(
"id" => $customer
),
"financial_status" => "pending",
"shipping_address" => array(
"first_name" => '',
"address1" => $ohsaddr1,
"addres2" => $ohsaddr2,
"phone" => $ohsphone,
"city" => $ohscity,
"zip" => $ohszip,
"province" => $ohsstate,
"country" => "United States",
"last_name" => $ohsname,
"name" => $ohsname,
"country_code" => "US",
"province_code" => $ohsstate
),
/*"billing_address" => array(
"first_name" => '',
"address1" => $_POST["bill_address1"],
"phone" => $_POST["bill_phone"],
"city" => $_POST["bill_city"],
"zip" => $_POST["bill_zip"],
"province" => $_POST["bill_state"],
"country" => "United States",
"last_name" => $_POST["bill_name"],
"name" => $_POST["bill_name"],
"country_code" => "US",
"province_code" => $_POST["bill_state"]
),*/
"line_items" => $line_items,
"note_attributes" => array(
array("name" => "POnumber",
"value" => $csmpo),
array("name" => "shipdate",
"value" => $shipdte),
array("name" => "canceldate",
"value" => $cncldte)
),
"gateway" => "Achim Credit Terms"));
$session = curl_init();
curl_setopt($session, CURLOPT_URL, $url);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8'));
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_VERBOSE, 0);
curl_setopt($session, CURLOPT_HEADER, 1);
curl_setopt($session, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($session, CURLOPT_POST, 1);
curl_setopt($session, CURLOPT_POSTFIELDS, json_encode($order));
curl_setopt($session, CURLOPT_SSL_VERIFYPEER,false);
$APIresult = curl_exec($session);
curl_close($session);
usleep(500000);
$result .= "Processed Order " . $csmpo . " for a total of " . $ordtot . " items, shipping to " . $ohdcity . ", " . $ohdstate . "<br>";
}
}
print_r($result);

0

Решение

ты никогда не сбрасываешь $error в false, поэтому, когда $ error становится истинным, он остается верным для всех последующих итераций.

Сброс ошибки $ в первой строке вашего цикла:

for ($x = 0; $x < $lines; $x++) {
$error = false;
....
0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector