简体   繁体   中英

get a specific row of a table from an XML file in php

So i have a SQL database that stores game information such as the content of a inventory, and i am trying to display this on a web page using php.

The SQL stuff is all working, and i have an XML file that contains all of the names and information for all the items. when i tried to read the xml file to print the names of the items, i get errors for some of the entries.

<?php

$ObjectXML = simpleXML_load_file("xml\objects_types.xml") or die("Error: Cannot Open XML file Object Types");

$servername = "localhost";
$username = -
$password = -

$connection = new mysqli($servername, $username, $password);

if($connection->connect_error) 
{
    die("Connection failed: " . $connection->connect_error);
}
echo("Connected Successfully<br>");

$sql = "SELECT * FROM lif_1.items WHERE ContainerID = 158;";
$result = $connection->query($sql);
echo('<table style="width:100%">');
echo("<tr>");
echo("  <th>ID</th>");
echo("  <th>ContainerID</th>");
echo("  <th>ObjectTypeID</th>");
echo("  <th>Quality</th>");
echo("  <th>Quantity</th>");
echo("  <th>Durability</th>");
echo("  <th>MaxDurability</th>");
echo("</tr>");
foreach($result as $row)
{
    echo("<tr>");
    $rowpos = 0;
    foreach($row as $value)
    {
        $rowpos++;
        echo("<td>");
        echo($value);
        if($rowpos==3)
        {
            echo(" Name:");
            echo($ObjectXML->row[$value-1]->Name);
               //print_r($ObjectXML->xpath('//objects_types[@ID="'+$value+'"]'));
        }
        echo("</td>");
    }
    echo("</tr>");
}
?>

the webpage (and error): http://86.146.184.166/

the XML file: http://86.146.184.166/xml/Objects_types.xml

i tried using xpath based off of a solution on here, but that appeared to return an empty array.

EDIT: the output: (a bit difficult to read)

Connected Successfully
ID ContainerID ObjectTypeID Quality Quantity Durability MaxDurability236158644 Name:Vertato Zonda10020002721581032 Name:
: Trying to get property of non-object in on line :尝试在第行的获取非对象的属性
100100273158361 Name:Iron Bar10060002801581030 Name:
: Trying to get property of non-object in on line :尝试在第行的获取非对象的属性
10028001089158338 Name:Bone Glue523001090158430 Name:Wooden Gatehouse561001091158390 Name:Primitive Crucible and Stick562001092158342 Name:Silk Filaments56500

edit2 - Answered :D What i changed incase anyone finds this useful:

    echo(" Name:");
    //echo($ObjectXML->row[$value-1]->Name);
    $xpathobject = $ObjectXML->xpath("row/ID[.=\"$value\"]/parent::*");
    //print_r($xpathobject);
    echo($xpathobject[0]->Name);

The row tag in your XML only contains 1029 entries. When you call this line

echo($ObjectXML->row[$value-1]->Name);

when $value contains either 1032 or 1030 then of course you're trying to get property of a non-existing object.

To find simplexml node entry by tag value, use xpath

$ObjectXML->xpath("row/ID[.=\"$value\"]/parent::*");

Here is an example of how to use XPath to get the ID node by value, and also example of how to get a parent node:

$value = 236;
$row = $xml->xpath( "//ID[.=\"$value\"]/parent::*" );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM