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

  • SEARCH
  • Home
  • 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 8790607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:42:10+00:00 2026-06-13T22:42:10+00:00

I try the search code below but it is only showing the first child

  • 0

I try the search code below but it is only showing the first child node. Is something missing from my code?

—-catalog.xml—-

<?xml version="1.0" encoding="UTF-8"?>
<Catalog>
    <Category>
        <Name>CAT1</Name>
        <Location>
            <Room>Alpha</Room>
        </Location>
        <Location>
            <Room>Bravo</Room>
        </Location>  
        <Location>
            <Room>Charlie</Room>
        </Location>  
    </Category>
    <Category>
        <Name>CAT2</Name>
        <Location>
            <Room>Delta</Room>
        </Location>  
        <Location>
            <Room>Eagle</Room>
        </Location>  
        <Location>
            <Room>Falcon</Room>
        </Location>  
    </Category>
</Catalog>

—-arr0.php—-

<?php
$catalog = simplexml_load_file("catalog.xml");
$category = $catalog->Category;

foreach($category->Name as $name)
{
    $menu = (string)$name;
    $i = 0;
    if ($menu == "CAT1" )
{
        echo $category->Location->Room[$i];
        $i++;       
}
else 
{
        echo "No result";
}
}
?>

———Output———-

Alpha


Bravo and Charlie is missing from display. Need someone advise where am I missing something?

Thanks

  • 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-06-13T22:42:12+00:00Added an answer on June 13, 2026 at 10:42 pm

    I think this is what you want

    <?php
    $catalog = simplexml_load_file("catalog.xml");
    $category = $catalog->Category;
    $found = false;
    foreach($category as $c)
    {
        $menu = (string) ($c->Name);
        if ($menu == "CAT1" )
        {
            foreach ($c->Location as $loc)
            {
                echo $loc->Room;
            }
            $found = true; break;
        }
    }
    if (!$found)
    {
        echo "No result";
    }
    ?>
    

    Remove the break; statement if you can have more than one Category with the searched Name element.

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

Sidebar

Related Questions

When I try to search something like C&A the only query I get in
I am using the below code to search a mysql database but cannot get
Please help, I got the code below from a book but it doesnt seem
I use the code below to fetch the first two pages of google search
I have an XML document that I load in and try to search with
I'm trying to find out why this entire code below is producing only ONE
I'm using the code below for my search logic, basically, it evaluates a field
I have the method below to parse data from the twitter search, strange thing
this question on google authentication when i try to run below code page successfully
I'm using the code below to authenticate a user in Active Directory, but the

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.