false, 'ERROR' => 'not_logged_in', 'MESSAGE' => 'Authentication required']); } try { $rows = queryTimed(" SELECT a.ID, a.IsDefaultDelivery, a.Line1, a.Line2, a.City, a.StateID, s.Abbreviation AS StateAbbreviation, s.Name AS StateName, a.ZIPCode FROM Addresses a LEFT JOIN tt_States s ON a.StateID = s.ID WHERE a.UserID = ? AND (a.BusinessID = 0 OR a.BusinessID IS NULL) AND a.AddressTypeID = 2 AND a.IsDeleted = 0 ORDER BY a.IsDefaultDelivery DESC, a.ID DESC ", [$userId]); $addresses = []; foreach ($rows as $r) { $line2 = $r['Line2'] ?? ''; $stateAbbr = $r['StateAbbreviation'] ?? ''; $displayText = $r['Line1'] . ($line2 !== '' ? ', ' . $line2 : '') . ', ' . $r['City'] . ', ' . $stateAbbr . ' ' . $r['ZIPCode']; $addresses[] = [ 'AddressID' => (int) $r['ID'], 'IsDefault' => (int) $r['IsDefaultDelivery'] === 1, 'Line1' => $r['Line1'], 'Line2' => $line2, 'City' => $r['City'], 'StateID' => (int) $r['StateID'], 'StateAbbr' => $stateAbbr, 'ZIPCode' => $r['ZIPCode'], 'DisplayText' => $displayText, ]; } jsonResponse(['OK' => true, 'ADDRESSES' => $addresses]); } catch (Exception $e) { apiAbort(['OK' => false, 'ERROR' => 'server_error', 'MESSAGE' => $e->getMessage()]); }