Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6743973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:01:41+00:00 2026-05-26T12:01:41+00:00

I am reading some xml which has come back from amazon’s api. Most times

  • 0

I am reading some xml which has come back from amazon’s api. Most times amazon provides a list of similar products in their xml, but not always. If there are similar products I echo our them as a set of links. The problem is that not all products have similar products in the xml. I would like to change this code so that it checks if there are similar products nodes first and then if there are it continues to render them out as links as below but if not it just does nothing. The code below currently gives the following error message if there are no similar products in the xml:

Warning: Invalid argument supplied for foreach() in /public_html/product.php on line 26

if there are no similar product nodes in the xml returned from amazon.

Thanks in advance.

foreach ($result->Items->Item->SimilarProducts->SimilarProduct as $SimilarProduct) {
echo "<li><a href=\"product.php?prod=" . $SimilarProduct->ASIN . "\">" . $SimilarProduct->Title . "</a></li/> \n";
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:01:42+00:00Added an answer on May 26, 2026 at 12:01 pm

    You can do this test before the foreach (note the if):

    if ($result->Items->Item->SimilarProducts->SimilarProduct !== null)
    {
       foreach ($result->Items->Item->SimilarProducts->SimilarProduct as $SimilarProduct) {
          echo "<li><a href=\"product.php?prod=" . $SimilarProduct->ASIN . "\">" . $SimilarProduct->Title . "</a></li/> \n";
       }
    }
    

    Anyway this test works if there’re no errors or null vars before the 'SimilarProduct' (for example if 'Item' is null the script will fail anyway).

    Note: I suggest you to not do this kind of things, I mean this repeated access to vars (that is ->…->…->) because you will have a low control on the situation and on what’s happening.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a questions.xml file which has a list of questions for my quiz,
I have some XML which I am reading by jQuery and printing on screen.
I'm reading some XML with PHP and currently using the DOMDocument class to do
What is the difference between XML and SOAP? I've been reading some introductory articles
i've been reading through the linq to xml documentation in msdn and some other
Reading some posts from Jimmy Boggard and wondering - how exactly is it possible
I'm reading some MPEG Transport Stream protocol over UDP and it has some funky
i want to transform some xml into HTML that has the following format: <TR><TD>
After writing and reading an xml string to and from a stream, it ceases
I'm reading data from XML files into a strong typed DataSet . The data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.