1

I had some trouble wording the question, but I think I can explain it better once I show the code: This is my xml file (test.xml)

<?xml version="1.0" encoding="utf-8"?>
<root>
<entry id="1">
  <post>05/12/2014 12:00:00</post>
  <page>1</page>
  <part>1</part>
  <body>BODY TEXT 1</body>
</entry>
<entry id="2">
  <post>05/14/2014 12:00:00</post>
  <part>1</part>
  <page>2</page>
  <body>BODY TEXT 2</body>
</entry>
</root>

This is my PHP code (call.php)

<?php

if(isset($_GET['p']))
{
$p=$_GET['p'];
echo $p . "<br>";
$xml=simplexml_load_file("test.xml");
$day=$xml->entry[$p]->post;//<-----------PROBLEM AREA
$post=strtotime("$day");
echo $post . "<br>";
echo time() . "<br>";
if(time() >= $post) 
{
    echo $xml->entry[$_GET['p']]->page . "<br>";
    echo $xml->entry[$_GET['p']]->part . "<br>";
}
}else{
    echo "<p>Main Page Stuff</p>";
}
?> 

The problem I am having is with the $day variable. If I replace [$p] with [1] or [0], it runs perfectly, but I need to have it called with a variable so I can change what part of the XML file is loaded depending on the query string, currently '?p=1'.

The echo $p results prints out 1 or 0 depending on what I put into the URL, so the $_GET is working properly, but if I put either $p or $_GET into entry[] it gives the error

Notice: Trying to get property of non-object in C:\wamp\www\xmltest\call.php on line 20

(line 20 is the one marked 'PROBLEM AREA')

is there any way to fix this problem so I can call either the first or the second depending on the query string?

2
  • What does var_dump($xml); give you? Is there an entry array? Commented May 12, 2014 at 21:26
  • object(SimpleXMLElement)[1] public 'entry' => array (size=2) 0 => object(SimpleXMLElement)[2] public '@attributes' => array (size=1) ... Is the result from var_dump($xml); Commented May 12, 2014 at 22:06

1 Answer 1

1

I could not find a way to make the solution I was hoping for work, so I worked around it by making a new xml file for each entry instead of having each entry in one xml file. Code used to load the xml file:

p="docs/".$_GET['p'].".xml";
if(file_exists($p))
{
  $xml=simplexml_load_file($p);
  $day=$xml->entry->post;
  $value = $xml->entry->body;
  echo "Part ".$xml->entry->part;
  echo "Page ".$xml->entry->page;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.