Solved

How can I get rid of this 'PHP Notice: Undefined offset: 0 in' and reduce my lines of code?

Posted on 2009-05-20
8
330 Views
Last Modified: 2012-05-07
I have the following code..

Which produces this error

PHP Notice: Undefined offset: 0 in ....

I'm checking various items in xml, its this line that produces the error.
$RDpurchaseid = $result[0];

As you can see my code is very simplistic, what can I do to get rid of the error and have less lines of code ?
<?php

$conON = "OrderNotification";

$conRD = "RefundDone";

$conFRD = "FraudRefundDone";

$conCBLN = "ChargebackLetter";

$conCBN = "Chargeback";

$conCBR = "ChargebackReversal";

$conPurchaseID = "/Purchase/PurchaseId";

$conDeliveryEmail = "/Purchase/CustomerData/DeliveryContact/Email";

$conBillingEmail = "/Purchase/CustomerData/BillingContact/Email";

$conPayStatus = "/Purchase/PaymentStatus";

$conPurchaseDate = "/Purchase/PurchaseDate";
 

include "shareitxml.php";
 

$xmlstr = utf8_encode($xmlstr);

#$xml = simplexml_load_string($xmlstr, NULL, LIBXML_NOERROR | LIBXML_NOWARNING);

$xml = simplexml_load_string($xmlstr);
 

$result = $xml->xpath($conON . $conPurchaseID);

$ONpurchaseid = $result[0]; }
 

$result = $xml->xpath($conRD . $conPurchaseID);

$RDpurchaseid = $result[0];
 

$result = $xml->xpath($conFRD . $conPurchaseID);

$FRDpurchaseid = $result[0];
 

$result = $xml->xpath($conCBLN . $conPurchaseID);

$CBLNpurchaseid = $result[0];
 

$result = $xml->xpath($conCBN . $conPurchaseID);

$CBNpurchaseid = $result[0];
 

$result = $xml->xpath($conCBR . $conPurchaseID);

$CBRpurchaseid = $result[0];
 

$NotifyType = "";
 

if ($ONpurchaseid != "") {

    $NotifyType = $conON;

} elseif ($RDpurchaseid != "") {

    $NotifyType = $conRD;

} elseif ($FRDpurchaseid != "") {

    $NotifyType = $conFRD;

} elseif ($CBLNpurchaseid != "") {

    $NotifyType = $conCBLN;

} elseif ($CBNpurchaseid != "") {

    $NotifyType = $conCBN;

} elseif ($CBRpurchaseid != "") {

    $NotifyType = $conCBR;

}
 

$result = $xml->xpath($NotifyType . $conDeliveryEmail);

$deliveremail = $result[0];
 

$result = $xml->xpath($NotifyType . $conBillingEmail);

$billingemail = $result[0];
 

$result = $xml->xpath($NotifyType . $conPayStatus);

$paymentstatus = $result[0];
 

$result = $xml->xpath($NotifyType . $conPurchaseID);

$purchaseid = $result[0];
 

$result = $xml->xpath($NotifyType . $conPurchaseDate);

$purchasedate = $result[0];
 

echo "DE=" . $deliveremail . "<BR/>";

echo "BE=" . $billingemail . "<BR/>";

echo "PS=" . $paymentstatus . "<BR/>";

echo "PI=" . $purchaseid . "<BR/>";

echo "PD=" . $purchasedate . "<BR/>";

?>

Open in new window

0
Comment
Question by:mindwarpltd
  • 4
  • 4
8 Comments
 
LVL 6

Expert Comment

by:twocandles
ID: 24432779
You have an extra curly bracket "}" at the end of line 21. Try removing it...
0
 

Author Comment

by:mindwarpltd
ID: 24433001
Yes, sorry, thats a mistake while I've copied it across, but the error still remains etc
0
 

Author Comment

by:mindwarpltd
ID: 24433093
For completeness here is shareitxml.php
<?php

$xmlstr = <<<XML

<?xml version="1.0" encoding="UTF-8"?>

<e5Notification xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:noNamespaceSchemaLocation="http://xml.element5.com/2.0/notification.xsd">

<OrderNotification>

<Purchase>

<PurchaseId>137693001</PurchaseId>

<PurchaseDate>2007-07-24T15:24:40</PurchaseDate>

<PurchaseOrigin>Other</PurchaseOrigin>

<PaymentCompleteDate>2007-07-

24T15:33:42</PaymentCompleteDate>

<PaymentStatus>complete</PaymentStatus>

<SequentialInvoiceNo>e5-DE-2007-

00001091596</SequentialInvoiceNo>

<CustomerData>

<BillingContact>

<LastName>Stange</LastName>

<FirstName>Karsten</FirstName>

<Email>stange@element5.de</Email>

<Address>

<Street1>Vogelsanger Str. 78</Street1>

<City>Köln</City>

<PostalCode>50823</PostalCode>

<CountryId>DE</CountryId>

<Country>Germany</Country>

</Address>

</BillingContact>

<DeliveryContact>

<LastName>Stange</LastName>

<FirstName>Karsten</FirstName>

<Email>stange@element5.de</Email>

<Address>

<Street1>Vogelsanger Str. 78</Street1>

<City>Köln</City>

<PostalCode>50823</PostalCode>

<CountryId>DE</CountryId>

<Country>Germany</Country>

</Address>

</DeliveryContact>

<CustomerPaymentData>

<PaymentMethod>Visa</PaymentMethod>

<Currency>EUR</Currency>

</CustomerPaymentData>

<Language>English</Language>

<SubscribeNewsletter>true</SubscribeNewsletter>

</CustomerData>

<PurchaseItem>

<RunningNo>1</RunningNo>

<ProductId>300170245</ProductId>

<ProductName>QA Product XML mails</ProductName>

<NotificationNo>6</NotificationNo>

<DeliveryType>Electronically</DeliveryType>

<Currency>EUR</Currency>

<Quantity>1</Quantity>

<ProductSinglePrice>10.00</ProductSinglePrice>

<VatPct>19.00</VatPct>

<Discount>0.00</Discount>

<ExtendedDownloadPrice>0.00</ExtendedDownloadPrice>

<ManuelOrderPrice>0.00</ManuelOrderPrice>

<ShippingPrice>0.00</ShippingPrice>

<ShippingVatPct>0.00</ShippingVatPct>

</PurchaseItem>

</Purchase>

</OrderNotification>

</e5Notification>

XML;

?>

Open in new window

0
 
LVL 6

Expert Comment

by:twocandles
ID: 24433145
The notice means an access to an array element that doesn't exist. In fact, in your xml i can't see the RefundDone/Purchase/PurchaseID element you're trying to access in the offending line. Try adding it to the xml.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:mindwarpltd
ID: 24433282
Yes your correct.
However, thats the point, I'm trying to detect which xml data has been used it could be

OrderNotification/Purchase/PurchaseID
or
RefundDone/Purchase/PurchaseID

etc.

A lot of my code is used to determine which xml data is being used and getting the right data.
0
 
LVL 6

Accepted Solution

by:
twocandles earned 500 total points
ID: 24433341
Then check it using:

isset( $result[0] )

after retrieving the value. Thus you'll know whether the data exists.
0
 

Author Comment

by:mindwarpltd
ID: 24433434
Great, that does get rid of the error.

Any ideas about reducing the lines of code I'm using, by some clever code to detect which data is being used etc?
0
 
LVL 6

Expert Comment

by:twocandles
ID: 24438603
When in this situations where you have to "if" for a lot of values, I usually go for this approach: put the values you want to check in an array, and loop through the array. Look at the code I'm attaching (I haven't checked if it works, but I expect you get the idea)

$valuesToCheck = array( "OrderNotification", "RefundDone", "FraudRefundDone" );
 

foreach( $valuesToCheck as $value) 

{

   $valueToUse = $xml->xpath($value . $conPurchaseID);

   if( isset( $valueToUse ) )

       break;

}
 

// Here you'll have the set value

Open in new window

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now