Query XML file and show pages based on the values

Hi all, using some code from an older thread if anybody was involved, thanks.

Ok, with the code below I’m basically selecting all pages and their child pages from the navigation database. What I need, and trying now is only show a selection of these pages based on the values from a xml file.

 // get all pages
  $pages = new TFW_Navigation($pageNav);
  $pages = $pages->getNavigation();
    foreach ($pages as $index => $topPage)
  {
  // get all child pages
  $children = $topPage->getChildren(null,true);
  if (!empty($children)) {
    foreach($children as $child) {

I was thinking something like:

$xml = simplexml_load_file($docRoot.'article/data/'.$pageID);

Each xml file ($pageID) I reference will have elements similar to:

<item name="related_articles">
  <value>22</value>
  <value>29</value>
</item>

I need to update my initial php code somehow to only show the pages with the IDs from related_articles. So instead of displaying all the pages from the database, only show the ones in the xml file.

Can anybody advise or offer suggestions how I could do this?

Cheers,
Barry

I now have this xml file loaded and ready.
Can anybody help with this?

As mentioned above, I need to somehow feed in the values from related_articles.
So get $pages only where ? ?

Thanks, Barry

So, what you want exactly, you want to get articles from database based on the xml file with the close values?
So, if in the xml file are values as 22 and 29, you want to only get articles from database based on these values?

   $xml = simplexml_load_file($docRoot.'article/data/'.$pageID);

This is the way you access the value tag getting the value from it, from the xml file.

 $xml->value;

If you want to get all the values, use the line above in a foreach loop and after that, do what you want.

Exactly that Pocsan :smile:
Do you know how we can achieve this integrating into our existing code?

Just seen your update…
Ideally, something like

$xml->related_articles->value;

Could you give a example of how we could code this?

Thanks, Barry

<?php

 $xml = simplexml_load_file($docRoot.'article/data/'.$pageID); or trigger_error("Error: Cannot create object");

foreach($xml->value as $items) {
     // do what you want to do here.
	echo $items.'<br>';
}

?>

I want to load only the pages from $pages based on the values inside related_articles. The xml file is full of data and has many values for different elements. I need to reference the relate_articles. How do I fit this into the original pages and child pages logic above?

Appreciate the information Pocsan.

Barry

Simply, you have to include that part of code in this foreach loop, so for every item, the code will get the required page.

A question,

$pages = new TFW_Navigation($pageNav);

The $pageNav value is an integer( a number ) ? if it is, all you have to do is to replace here the $pageNav with $items.

foreach($xml->value as $items) {
   // get all pages
  $pages = new TFW_Navigation($items);

  $pages = $pages->getNavigation();
    foreach ($pages as $index => $topPage)
  {
  // get all child pages
  $children = $topPage->getChildren(null,true);

  if (!empty($children)) {
    foreach($children as $child) { }
       }
   }

}

I just started with xml, I am not that good, just give me a little time to search for what you need.
If the value field is just part of the parent related_articles, then there is no problem, but if this field is used in other parts of that xml file, then there is a problem, have you found a solution?

Been trying everything, no errors, but no articles no content is showing?
Yes I think its an integer, I’ve used your code and seems to run ok, though as above, nothing is showing.

How does it know to access the related_articles element from the XML file?
We don’t mention it.

foreach($xml->value as $items)

Thanks, Barry

Have you included the line with loading the xml file? that line has to be the first line and above the foreach loop with the rest of the code.Yeah, I am not sure, I am quite new to xml ( SimpleXML).I am thinking at a solution.

Not yet.
As above in my first post, each article has different related articles, this is just one element of about 30 from the xml file, thats why if we don’t mention related_articles, we could be calling any value, if that makes sense.

<item name="related_articles">
  <value>22</value>
  <value>29</value>
</item>

The related_articles element is unique and is only used for what we’re doing here.

Just seeing if I can figure out what we need.

Update: Yes the xml file is there at the top.

Thanks, Barry

Here is the full code, maybe give you a better idea:

<div class="article-grid">
  <?php
    $xml = simplexml_load_file($docRoot.'article/data/'.$langID.'_'.$pageID);

    foreach($xml->value as $items) {
    // get all pages
    $pages = new TFW_Navigation($items);

    $pages = $pages->getNavigation();
      foreach ($pages as $index => $topPage)
    {
    // get all child pages
    $children = $topPage->getChildren(null,true);

    if (!empty($children)) {
      foreach($children as $child) { 
      
  ?>

  <article class="<?=$child->ext_column_1 ?>">
     <a href="<?=$child->url ?>" class='article-border'>
        <img src="<?=$child->ext_column_2 ?>" alt="">
          <header>
              <h3>
                  <?=$child->ext_column_3 ?>
              </h3>
          </header>
      </a>
  </article>
	
  <?php
    }
      }
        }
          }
  ?>

</div>
<item name="related_articles">
  <value>22</value>
  <value>29</value>
</item>

So the code above is unique( with all the value tags), I mean, the value tag is only in related_articles, if it is, then there is no problem, if we use $xml->value, only all the tags with the value name are used, no other tags.I think there is a problem somewhere else, try something simple, see if this works:

<?php $xml = simplexml_load_file($docRoot.'article/data/'.$pageID); or trigger_error("Error: Cannot create object"); foreach($xml->value as $items) { // do what you want to do here. echo $items.'
'; } Test just this code.

It might be easier if I show you some data from the xml file:

<item name="article_category">
  <value>red</value>
</item>
  <item name="related_categories">
  <value>category_one</value>
  <value>category_two</value>
</item>
<item name="related_articles">
  <value>42</value>
  <value>29</value>
  <value>37</value>
  <value>51</value>
</item>

Now do you see what I mean?

Thanks, Barry

I see, we have to specify the parent name, I am thinking at a solution.

Cool!
Thanks Pocsan, I appreciate your time on this.

Yes, parent, thats why I thought something like:

$xml->related_articles->value as $items

Though I did try this and gave me any error.

Barry

So, using the print_r() function we can see how the data is stored inside the $xml object:

SimpleXMLElement Object ( [@attributes] => Array ( [name] => related_articles ) [value] => Array ( [0] => 22 [1] => 29 ) )

I am thinking.

Are you saying to use print_r() ?
If so, print what ?

Barry

I just wanted to see how the SimpleXMLElement Object is built, I am using the SimpleXml manual for more information.

Yes looking myself, lots of diffrent ways of doing stuff, though still, surely there is an easy why to add related_articles into, even then, no guarantee this will work:

foreach($xml->value as $items) {

I’ve just tried:

foreach($xml->related_articles as $itemsA) {
   foreach($itemsA->value as $items) {

Still stays blank.

Barry