-1

I've got problem with retreving records from XML database by PHP.

There are many elements called "" in the XML file that each other has attribute called "id" that I'm calling from PHP. It is not working when I type any number except number "1" which is working.

Here is the error message that I'm getting.

Notice: Undefined variable: quest2 in C:\xampp\htdocs\game\data\quest_query.php on line 58 Notice: Undefined variable: info_quest in C:\xampp\htdocs\game\data\quest_query.php on line 58 Notice: Undefined variable: time_end in C:\xampp\htdocs\game\data\quest_query.php on line 58

These errors I'm getting just because of the "if" statement is not being accepted. Here is the code:

<?php

$xmlStringFile = '<?xml version="1.0" standalone="yes"?>
<quests>
 <quest id="1">
        <ID>1</ID>
        <QUEST>Katakomby pod městem</QUEST>
        <INFO>Asi nevíte že pod naším krásným městem je spousta tajných chodem, které pořádně nikdo neproskoumal. Jsou tu již dlouhá staletí a údajně naši předci v nich ukryli zlatý poklad.</INFO>
        <TIME>780</TIME><!-- 780 - 13 minut -->
        <ITEMS1></ITEMS1>
        <XPMIN>180</XPMIN>
        <MONEY>800</MONEY>
        <LVL>1</LVL>
    </quest>

    <quest id="2">
        <ID>2</ID>
        <QUEST>Oprava zříceného mostu</QUEST>
        <INFO>Před několika dny strhla záplava starý dřevěný most u radnice. Pomoz jej opravit.</INFO>
        <TIME>7200</TIME>
        <ITEMS1></ITEMS1>
        <XPMIN>250</XPMIN>
        <MONEY>250</MONEY>
        <LVL>4</LVL>
    </quest>
</quests>';

  $quests = new SimpleXMLElement($xmlStringFile);

  if($_GET['quest'] == $quests->quest["id"]){
        $quest2 = $quests->quest->QUEST;
        $info_quest = $quests->quest->INFO;
        $time_end = $quests->quest->TIME;
  }
  // echo $idquest . $quest2 . $info_quest . $time_end;
  echo $_GET['quest'] . $quest2 . $info_quest . $time_end;

?>

Thanks in advance, all help will be very appreciated.

Peter Bielak
  • 643
  • 2
  • 12
  • 24
  • First: Notice != Error. Then, var_dump or print_r $quests to see the structure of your xml (or the object/array), without trying i would say you are missing a loop as $quests->quest-> cant exist, but $quests->quest[0]-> may. – Rufinus Dec 24 '13 at 16:59

1 Answers1

0

Use xpath to select the <quest> you need:

$quests = simplexml_load_string($x); // assume XML in $x

$id = 2;
$results = $quests->xpath("/quests/quest[@id='$id']");

if (isset($results[0])) $results = $results[0];
else exit("no match");

echo $results->QUEST;

see it working: http://codepad.viper-7.com/KJMIAl

michi
  • 6,565
  • 4
  • 33
  • 56