Can someone assist with this foreach loop?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
danjapro
Forum Commoner
Posts: 72
Joined: Mon Sep 27, 2004 10:56 am

Can someone assist with this foreach loop?

Post by danjapro »

All I am Trying to do is Loop thru my Array fo results and out each one seperate if there is more than 1 data point:

Trying to display each of tracking # as separate line:

Code: Select all

 getIsOrderShipped($_order->getId()); // Is oorder shipped or not

```
$items = $_order->getAllVisibleItems();
$trackingNumbers = [];

$trackNo = $adminHelper->getOrderTrackingNo($_order->getId());
$trackNos = explode(",", $trackNo);
if (count($trackNos)) {
    for ($i=0; $i< count($trackNos); $i++) {
        $trackingURL = $adminHelper->getTrackingUrl(strtolower($_order->getShippingDescription()), $trackNo);
        $trackingNumbers[] = array("track_no" => $trackNo, "url" => $trackingURL);
    }
}

$trackingInfoInEmail = [];
$i = 0;
if (count($trackingNumbers) > 1) {
    foreach($trackingNumbers as $trackingNumber) {
        $i++;
        $trackingInfoInEmail[] = "<a href='" . $trackingNumber[$i]['url'] . "' target='_blank'>" . $trackingNumber[$i]['track_no'] . "</a>";
        return false;
    }

}else{
        $trackingInfoInEmail[] = "<a href='" . $trackingNumbers[]['url'] . "' target='_blank'>" . $trackingNumbers[]['track_no'] . "</a>";
}
$displayShippingMethod = $adminHelper->getTrackingTextInEmail($_order->getShippingDescription()); //Shipping Method Name

if (!$isOrderShipped) {
    return false;
} else {
    if (count($items) > 0) {
        echo  "You may receive multiple shipments and emails. The tracking number for this shipment is " . $displayShippingMethod . " #&nbsp;" . implode(',', $trackingInfoInEmail);
    } else  {
        echo  "The tracking number for this shipment is " . $displayShippingMethod . " #&nbsp;" . implode(',', $trackingInfoInEmail);
    }
}
```

danjapro
Forum Commoner
Posts: 72
Joined: Mon Sep 27, 2004 10:56 am

Re: Can someone assist with this foreach loop?

Post by danjapro »

I changed this, it works to a degree.
Now it is returning duplicated tracking numbers and each tracking number.

I need help, just get each one tracking number seperate.

Code: Select all

    $isOrderShipped = $adminHelper->getIsOrderShipped($_order->getId()); // Is oorder shipped or not

    $items = $_order->getAllVisibleItems();
    $trackingNumbers = [];

    $trackNo = $adminHelper->getOrderTrackingNo($_order->getId());
    $trackNos = explode(",", $trackNo);
    if (count($trackNos)) {
        for ($i=0; $i< count($trackNos); $i++) {
            $trackingURL = $adminHelper->getTrackingUrl(strtolower($_order->getShippingDescription()), $trackNo);
            $trackingNumbers[] = array("track_no" => $trackNo, "url" => $trackingURL);
        }
    }

    $trackingInfoInEmail = [];
    if (count($trackingNumbers) > ) {
        for ($i=0; $i < count($trackingNumbers); $i++) {
            $trackingInfoInEmail[] = "<a href='" . $trackingNumbers[$i]['url'] . "' target='_blank'>" . $trackingNumbers[$i]['track_no'] . "</a>";
        }
    }
    $displayShippingMethod = $adminHelper->getTrackingTextInEmail($_order->getShippingDescription()); //Shipping Method Name

    if (!$isOrderShipped) {
        return false;
    } else {
        if (count($items) > 1) {
            echo  "You may receive multiple shipments and emails. The tracking number for this shipment is " . $displayShippingMethod . " #&nbsp;" . implode(',', $trackingInfoInEmail);
        } else  {
            echo  "The tracking number for this shipment is " . $displayShippingMethod . " #&nbsp;" . implode(',', $trackingInfoInEmail);
        }
    }
danjapro
Forum Commoner
Posts: 72
Joined: Mon Sep 27, 2004 10:56 am

Re: Can someone assist with this foreach loop?

Post by danjapro »

Still Struggling, still getting duplicate tracking #s n my email:

I need help:

Code: Select all

    $isOrderShipped = $adminHelper->getIsOrderShipped($_order->getId()); // Is oorder shipped or not

    $items = $_order->getAllVisibleItems();
    $trackingNumbers = [];

    $trackNo = $adminHelper->getOrderTrackingNo($_order->getId());
    $trackNos = $adminHelper->getOrderTrackingNo($_order->getTrackingNumber());
    //$trackNos = explode(",", $trackNo);
    if (count($trackNos)) {
        for ($i=0; $i< count($trackNos); $i++) {
            $trackingURL = $adminHelper->getTrackingUrl(strtolower($_order->getShippingDescription()), $trackNo);
            $trackingNumbers[] = array("track_no" => $trackNo, "url" => $trackingURL);
        }
    }

    $trackingInfoInEmail = [];
    if (count($trackingNumbers) > 0) {
        //for ($i=0; $i < count($trackingNumbers); $i++) {
            $trackingInfoInEmail[] = "<a href='" . $trackingNumbers[$i]['url'] . "' target='_blank'>" . $trackingNumbers[$i]['track_no'] . "</a>";
        //}
    }
    $displayShippingMethod = $adminHelper->getTrackingTextInEmail($_order->getShippingDescription()); //Shipping Method Name

    if (!$isOrderShipped) {
        return false;
    } else {
    
        $i = 0;
        if (count($items) > 1) {
            echo "You may receive multiple shipments and emails.";
            foreach($items as $item) {
                $i++;
                echo "The tracking number for this shipment is " . $displayShippingMethod . " #&nbsp;" . implode(',', $trackingInfoInEmail);
             }
        } else {
              echo  "The tracking number for this shipment is " . $displayShippingMethod . " #&nbsp;" . implode(',', $trackingInfoInEmail);
        }
        
    }

User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Can someone assist with this foreach loop?

Post by Christopher »

In your first loop, you use $trackNo in every loop? Is this first case when there is only one trackNo? Or do you want to get the trackNo from each element in $trackNos?

Code: Select all

    $trackNo = $adminHelper->getOrderTrackingNo($_order->getId());
    $trackNos = $adminHelper->getOrderTrackingNo($_order->getTrackingNumber());
    //$trackNos = explode(",", $trackNo);
    if (count($trackNos)) {
        for ($i=0; $i< count($trackNos); $i++) {
            $trackingURL = $adminHelper->getTrackingUrl(strtolower($_order->getShippingDescription()), $trackNo);
            $trackingNumbers[] = array("track_no" => $trackNo, "url" => $trackingURL);
        }
    }
(#10850)
danjapro
Forum Commoner
Posts: 72
Joined: Mon Sep 27, 2004 10:56 am

Re: Can someone assist with this foreach loop?

Post by danjapro »

I re-wrote this to just append to the next record, when the emails keep coming thru. I need someone to look at this and tell me if this makes.

Code: Select all


if (!$isOrderShipped) {
    return false;
} else {
    if (count($items) > 1 && count($trackingNumbers) >= 1) {
        echo  "You may receive multiple shipments and emails. The tracking number for this shipment is " . $displayShippingMethod . " #&nbsp;" . next($trackingInfoInEmail);
        next($trackingInfoInEmail);
        echo  "You may receive multiple shipments and E-mails. The tracking N-umber for this shipment is " . $displayShippingMethod . " #&nbsp;" . current($trackingInfoInEmail);
        (($trackingInfoInEmail+1));
    } else if(count($items) == 1) {
        if (count($trackingNumbers) == 1) {
            echo "The tracking number for this shipment is " . $displayShippingMethod . " #&nbsp;" . next($trackingInfoInEmail);
            next($trackingInfoInEmail);
        } else if (count($trackingNumbers) > 1){
            echo  "You may receive multiple shipments and emails. The tracking number for this shipment is " . $displayShippingMethod . " #&nbsp;" . next($trackingInfoInEmail);
            next($trackingInfoInEmail);
        } else if(count($trackingNumbers)==0){
            echo  "Your Order has been Shipped with" . $displayShippingMethod;
        }
    } else {
        echo  "Your Order has been Shipped with" . $displayShippingMethod;
    }
}

User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Can someone assist with this foreach loop?

Post by Christopher »

I don't know the exact logic, but that looks confusing. You only have three unique responses.

And how do you deal with more than two tracking numbers?

And what is this line supposed to do?

Code: Select all

(($trackingInfoInEmail+1));
(#10850)
Post Reply