Mennyg Mennyg - 5 months ago 13
PHP Question

continue function is shutting down my PHP forloop

So im building a CSV uploader for my customers to upload orders to my website on Shopify with PHP.
If you look through my code, you'll see 2 instances where I set

$errors
to be
TRUE
. Then, after all the error possibilities, I check if
$errors
is true and if yes,
continue
the loop so that the order is not processed. For some reason, it is stopping the entire forloop like a
break
would do. Any help with this from the community? Here's my code:

$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);

Answer

you never reset $error to false, so once $error becomes true it stays true for all further iterations.

Reset $error in the first line of your loop:

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